Posts

Showing posts with the label GCD in python

GCD in python

Image
Python program to find GCD of two numbers GCD stands for Greatest Common Divisor. GCD of two numbers is the largest positive integer that divides each of the integers. For example, Consider the numbers 15 and 25, Factors of 15 => 5, 3 Factors of 25 => 5, 5 The factors that the two numbers have in common are the gcd of the numbers. The factor 5 is common in both numbers. Hence 5 is the gcd of 15 and 25. GCD of 15, 25 => 5 Now consider another example, numbers = 8 and 24 Factors of 8 => 2, 4, 8 Factors of 24 => 2, 4, 6, 8, 12, 24. The common factors are 2, 4, 8. The greatest of these common factors is 8. So the gcd of 8 and 24 is 8. Method 1 Procedure : 1. Start. 2. Get the first number as input from the user. 3. Get the second number as input from the user. 4. find the smallest of two numbers and store it in a variable. 5. loop from 0 to small number+1     => if (n1 % small = 0) and (n2 % small = 0)          => gcd = i(iterator) 6. print the gcd of two numbers. 7. E