Python Program for GCD of more than two (or array) numbers

PythonServer Side ProgrammingProgramming

In this article, we will learn about the solution to the problem statement given below −

Problem statement − We will be given an array of number and we need to find the greatest common divisor.

If we need to find gcd of more than two numbers, gcd is equal to the product of the prime factors common to all the numbers provided as arguments. It can also be calculated by repeatedly taking the GCDs of pairs of numbers of arguments.

Here we will be implementing the latter approach

So now, let’s see the implementation


 Live Demo

def findgcd(x, y):
      x, y = y, x % y
   return x
l = [22, 44, 66, 88, 99]
for i in range(2,len(l)):
print("gcd is: ",gcd)


Gcd is: 11

All the variables and functions are declared in global scope as shown in the image below −


In this article, we learned the approach to find the greatest common divisor of a given array of arguments.

Published on 11-Sep-2019 15:23:55