

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
GCD of more than two (or array) numbers in Python Program
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
Example
def findgcd(x, y): while(y): x, y = y, x % y return x l = [22, 44, 66, 88, 99] num1 = l[0] num2 = l[1] gcd = findgcd(num1,num2) for i in range(2,len(l)): gcd = findgcd(gcd,l[i]) print("gcd is: ",gcd)
Output
Gcd is: 11
All the variables and functions are declared in global scope as shown in the image below −
Conclusion
In this article, we learned the approach to find the greatest common divisor of a given array of arguments.
- Related Questions & Answers
- Python Program for GCD of more than two (or array) numbers
- C++ Program for GCD of more than two (or array) numbers?
- Java Program for GCD of more than two (or array) numbers
- C++ Program for GCD 0.of more than two (or array) numbers?
- Finding LCM of more than two (or array) numbers without using GCD in C++
- Program to find GCD or HCF of two numbers in C++
- Java program to find the GCD or HCF of two numbers
- Program to compute gcd of two numbers recursively in Python
- Find GCD of two numbers
- Java Program to Find GCD of two Numbers
- Python program to find Union of two or more Lists?
- Program to find GCD or HCF of two numbers using Middle School Procedure in C++
- GCD and LCM of two numbers in Java
- GCD of an array of numbers in java
- Python - How to Concatenate more than two Pandas DataFrames?
Advertisements