Gcd of 3 numbers in python

Gcd of 3 numbers in python

GCD of three integers (where integers not equal to zero) is largest positive integer that divides each of the three integers. For example GCD of 12 , 16 , 22 is 2 where factors of 12==>1,2,3,4,6,12
factors of 16==>1,2,4,8,16
factors of 22==>1,2,11,22
common factors==>1,2
greatest common factor==>2

Working:

  • Step 1: Import math module
  • Step 2: Read the first number
  • Step 3: Read the second number
  • Step 4: Read the third number
  • Step 5: Print the gcd of three numbers by using in-built gcd function . find gcd of two numbers and then find the gcd of remaing number and gcd of two numbers which gives gcd of three numbers

Python code:

Solution 1:

import math

n1=int(input(“ENTER THE FIRST NUMBER “))

n2=int(input(“ENTER SECOND NUMBER “))

n3=int(input(“ENTER THIRD NUMBER “))

print(“THE GCD OF GIVEN NUMBERS:”,math.gcd(math.gcd(n1,n2),n3))

ENTER THE FIRST NUMBER 12
ENTER SECOND NUMBER 16
ENTER THIRD NUMBER 22
GCD of given two numbers is: 2

Working :

  • Step 1: Create a function gcd which calculates gcd of two numbers using euclidean algorithm
  • Step 2: Read the first number
  • Step 3: Read the second number
  • Step 4: Read the third number
  • Step 5: Call the function gcd to calculate gcd of last two numbers and again call the same function to calculate gcd of first number and gcd of last two numbers which gives gcd of three numbers.

Solution 2:

def gcd(n1,n2):

    if(n1==0):

        return n2

    else:

        return gcd(n2%n1,n1)

n1=int(input(“ENTER THE FIRST NUMBER “))

n2=int(input(“ENTER SECOND NUMBER “))

n3=int(input(“ENTER THIRD NUMBER “))

print(“GCD of given two numbers is:”,gcd(n1,gcd(n2,n3)))

ENTER THE FIRST NUMBER 12
ENTER SECOND NUMBER 16
ENTER THIRD NUMBER 22
GCD of given two numbers is: 2

Very simple.
Write a function, that calculates gcd/lcm of two numbers.
Then do something like this.

gcd(a,b,c) = gcd(a, gcd(b,c))

 >>> def gcd(a,b):
...     if b == 0:
...             return a
...     else:
...             return gcd(b, a%b)
... 
>>> gcd(3,5)
1
>>> gcd(10,5)
5
>>> gcd(10,15)
5
>>> gcd(5,gcd(10,15))
5

You can try by yourself, for lcm.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The GCD of three or more numbers equals the product of the prime factors common to all the numbers, but it can also be calculated by repeatedly taking the GCDs of pairs of numbers.

    gcd(a, b, c) = gcd(a, gcd(b, c)) 
                 = gcd(gcd(a, b), c) 
                 = gcd(gcd(a, c), b)
    

    def find_gcd(x, y):

        while(y):

            x, y = y, x % y

        return x

    l = [2, 4, 6, 8, 16]

    num1=l[0]

    num2=l[1]

    gcd=find_gcd(num1,num2)

    for i in range(2,len(l)):

        gcd=find_gcd(gcd,l[i])

    print(gcd)

    Output:

    2
    

    Please refer complete article on GCD of more than two (or array) numbers for more details!

    How do you find the GCD of 3 numbers in Python?

    Python code:.
    import math..
    n1=int(input(“ENTER THE FIRST NUMBER “)).
    n2=int(input(“ENTER SECOND NUMBER “)).
    n3=int(input(“ENTER THIRD NUMBER “)).
    print(“THE GCD OF GIVEN NUMBERS:”,math.gcd(math.gcd(n1,n2),n3)).

    How do you calculate GCD in Python?

    Using Euclidean Algorithm :.
    Let a, b be the two numbers..
    a mod b = R..
    Let a = b and b = R..
    Repeat Steps 2 and 3 until a mod b is greater than 0..
    GCD = b..
    Finish..

    How do you find the GCD of 3 numbers in C?

    C Program to find the GCD of Three Numbers.
    C. #include <stdio.h> int gcd(int a, int b) if (b == 0) return a; return gcd(b, a % b); } ... .
    C++ #include <iostream> using namespace std; int gcd(int a, int b) if (b == 0) return a; ... .
    Java. import java.util.*; class Main. public static int gcd(int a,int b) if(b==0) return a;.