GCD (HCF) of N Integers - Python - Spicy Coders

Recent

Tuesday, September 12, 2017

GCD (HCF) of N Integers - Python


N integers are passed as input to the program. The program must print the GCD (HCF) of these N integers.

Input Format: The first line contains N. The second line contains N integers each separated by a space.

Output Format: The first line contains the HCF of these N numbers.

Boundary Conditions:
  • 2 <= N <= 100

Example Input/Output 1:
Input:
 4
15 20 30 50

Output:
5

Source Code:

from functools import reduce
from fractions import gcd
n=int(input())
l=[int(i) for i in input().split()]

print(reduce(gcd,l))

No comments:

Post a Comment