- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find highest common factor of a list of elements in Python
Suppose we have a list of elements called nums, we have to find the largest positive value that divides each of the integers.
So, if the input is like nums = [15, 81, 78], then the output will be 3, as 3 is the largest integer that divides all 15, 81, and 78.
To solve this, we will follow these steps −
if size of nums is same as 1, then
return nums[0]
div := gcd of nums[0] and nums[1])
if size of nums is same as 2, then
return div
for i in range 1 to size of nums - 2, do
div := gcd of div and nums[i + 1]
if div is same as 1, then
return div
return div
Example
Let us see the following implementation to get better understanding
from math import gcd def solve(nums): if len(nums) == 1: return nums[0] div = gcd(nums[0], nums[1]) if len(nums) == 2: return div for i in range(1, len(nums) - 1): div = gcd(div, nums[i + 1]) if div == 1: return div return div nums = [15, 81, 78] print(solve(nums))
Input
[15, 81, 78]
Output
3
- Related Articles
- Program to find HCF (Highest Common Factor) of 2 Numbers in C++
- C program to find Highest Common Factor (HCF) and Least Common Multiple (LCM)
- Find common elements in list of lists in Python
- Python Program that print elements common at specified index of list elements
- Find the highest common factor of;$7x^2yz^4, 21x^2y^5z^3$
- Python program to find sum of elements in list
- Program to find longest common prefix from list of strings in Python
- Program to find duplicate item from a list of elements in Python
- Find sum of elements in list in Python program
- Program to find the highest altitude of a point in Python
- Program to find largest sum of non-adjacent elements of a list in Python
- Python Program to Find Number of Occurrences of All Elements in a Linked List
- Program to find sum of odd elements from list in Python
- Python program to find Tuples with positive elements in a List of tuples
- Python program to find common elements in three sorted arrays?

Advertisements