Skip to main content

GCD ( Using Euclid's algorithm ) And LCM


The number 54 can be expressed as a product of two integers in several different ways:
 54 \times 1 = 27 \times 2 = 18 \times 3 = 9 \times 6. \,
Thus the divisors of 54 are:
 1, 2, 3, 6, 9, 18, 27, 54. \,
Similarly the divisors of 24 are:
 1, 2, 3, 4, 6, 8, 12, 24. \,
The numbers that these two lists share in common are the common divisors of 54 and 24:
 1, 2, 3, 6. \,
The greatest of these is 6. That is the greatest common divisor of 54 and 24. One writes:
 \gcd(54,24) = 6. \,

Using Euclid's algorithm

\gcd(a,0) = a
\gcd(a,b) = \gcd(b, a \,\mathrm{mod}\, b),
 a \,\mathrm{mod}\, b = a - b \left\lfloor {a \over b} \right\rfloor .
If the arguments are both greater than zero then the algorithm can be written in more elementary terms as follows:
\gcd(a,a) = a
\gcd(a,b) = \gcd(a - b,b)\quad, if a > b
\gcd(a,b) = \gcd(a, b-a)\quad, if b > a

Code to find the Greatest Common Divisor of two numbers.

int GCD(int a,int b)
    return a;

unsigned greatestCommonDivisor(unsigned m, unsigned n)
    if(n == 0) return m;
    return greatestCommonDivisor(n, m % n);
If we know the greatest common divisor (GCD) of integers a and b, we can calculate the LCM using the following formula.
LCM(a,b) =a × b


Popular posts from this blog

ORACLE 9i practice solutions

Created by BCL easyConverter SDK 3 (HTML Version)

Hackerrank > SQL > Basic Select

01-Select All
Given a City table, whose fields are described as +-------------+----------+ | Field       | Type     | +-------------+----------+ | ID          | int(11)  | | Name        | char(35) | | CountryCode | char(3)  | | District    | char(20) | | Population  | int(11)  | +-------------+----------+
write a query that will fetch all columns for every row in the table.

My Solution
02-Select by ID
Given a City table, whose fields are described as

Zoho Puzzle Questions With Answers

Measuring Time Logic Puzzle You are given with two ropes with variable width. However if we start burning both the ropes, they will burn at exactly same time i.e. an hour. The ropes are non-homogeneous in nature. You are asked to measure 45 minutes by using these two ropes.

How can you do it?

Please note that you can’t break the rope in half as it is being clearly stated that the ropes are non-homogeneous in nature.
Answer & Explanation Solution: 45 minutes

Explanation :
All you have to do is burn the first rope from both the ends and the second rope from one end only simultaneously. The first rope will burn in 30 minutes (half of an hour since we burned from both sides) while the other rope would have burnt half. At this moment, light the second rope from the other end as well. Where, the second rope would have taken half an hour more to burn completely, it will take just 15 minutes as we have lit it from the other end too.

Thus you have successfully calculated 30+15 = 45 minutes …