greatest common divisor - Proving gcd algorithm with Dafny

I'm trying to prove the gcd algorithm with Dafny and it's apparently not that simple. What I have so far (not much indeed) is a functional specification, and Dafny manages to prove that compute_gcd behaves like it. However, when I remove the comments [1], [2] and [3] Dafny failsto prove that the functional specification has the desired properties:function gcd(a: int, b: int) : (result : int) requires a > 0 requires b > 0 // [1] ensures (exists q1:int :: (q1 * result == a)) // [2] ensures (exists q2:int :: (q2 * result == b)) ...Read more

greatest common divisor - How to find the GCD of some pairs of numbers of a given set?

I can calculate the GCD of two numbers. given a set S = {1,2,3,4,5} and i have to calculate the GCD of each pair like {1,2} = 1, {1,3} =1 , {1,4} = 1, {1,5} = 1, {2,3} = 1, {2,4} = 2, {2,5} = 1 and so on. I know the O(N^2) solution by just simply calculate the GCD of each pair which will give me TLE in case of big set for 2<=n<= 10^9 or more but i want to learn O(N*sqrt(N) ) solution or more better. i want the GCD of each pair separately....Read more

greatest common divisor - "Factor" a number into 1-16 products and sum parts

I have a number N (integer 32 bits) that I need to "factor" into 1-16 product and sum parts. Let me explain:For $N = 256$, I want: $(16 \times 16)$For $N = 257$, I want: $(16 \times 16) + 1$For $N = 512$, I want: $(16 \times 16) \times 2$For $N = 530$, I want: $(16 \times 16 \times 2) + (2 \times 9)$For $N = 531$, I want: $(16\times 11 \times 3) + 3$The minor number of sums are better, i.e., products should be preferred.Is there a algorithm for this?...Read more

greatest common divisor - Old bell rang problem with different starting time

Suppose two bells rang at particular intervals but starting from a different time. First time when it will ring together is ?e.g.,First bell starts at 3, and repeats at regular interval of 5.Second bell starts at 2, and repeats at regular interval of 2.\begin{array}{|c|c|c|c|c|c|}\hlineTime& 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hlineBell1& - & -& \checkmark & - & - & - & - &\checkmark\\ \hline Bell2& -& \checkmark& - & \checkmark & - & \checkmark & - &...Read more

greatest common divisor - Unknown number x, 45•G.C.F(125, x) = L.C.M (125, x)

The least common multiple of 125 and an unknown number x is 45 times their greatest common divisor. Here is what I've tried:Let $\gcd(125,x) = G$ and $\mathrm{lcm}(125,x)=L$.We know$$125 \times x = G \times L \ \text{ and } L = 45G \implies 125x = 45G^2 \implies \frac{25}{9}x = G^2.$$Because $G$ is an integer, so are $x$ and $G^2$, so $9|x$.We know $125\times x = 45G^2$. The two numbers are not relatively prime, because $G \ne 1$. So the number we are looking for should at least be divisible by $5$, and hence by $45$. By trying out mult...Read more

greatest common divisor - Proof that $Z$ is a gcd ring

Recall the general definition of a gcd in a commutative ring $R$.For $a \in R$, $\mathcal D(a)$ is the set of elements that divide $a$ and if $S \subset R$, $\mathcal D(S) = \cap_{s \in S} \mathcal D(s)$We say that $d$ is a gcd of $a$ and $b$ and we write $d \in \gcd(a,b)$ whenever we have $ d \in \mathcal D(a,b) \subset \mathcal D(d) $.I would like to prove that, within the ring $\mathbb Z$ every pair of natural numbers has a gcd, that is, $\forall a,b \in \mathbb{N} \quad \gcd(a,b) \neq \emptyset $.Of course, they all have a gcd for the order...Read more

greatest common divisor - How to find GCD and LCM of a factorial and a large number?

So I was given this question: $n = 2^{16}3^{19}17^{12}$Find $\gcd(n, 40!)$ and $\operatorname{lcm}(n, 40!)$. I understand how to find the GCD and LCM when its two really large numbers (given their prime factorization), But I'm not sure how Id do something like this. Given we're not allowed to use calculators, I assume there is a way to find this.EDITSo I did (floor of each by the way)v2(40!) = 40/2 + 40/4 + 40/8 + 40/16 + 40/32 = 38v3(40!) = 40/3 + 40/9 + 40/27 = 18v17(40!) = 40/17 = 2So what do I do from here?...Read more