given n integers. It is required to find such a largest integer on which all these N numbers are divided.
I have code for finding a node of two numbers, but how to realize the finding node of several numbers?
Answer 1, Authority 100%
import math math.gcd (2, 6, 12) # 2
math.gcdChanged in Version 3.9: Added Support for An Arbitrary Number of Arguments. Formerly, Only Two Arguments Were Supported.
to version 3.9, but above 3.5 can be combined so:
math.gcd (3, math.gcd (6, 12)) # 3
from math import gcd from Functools Import Reduce Reduce (GCD, [3, 12, 6, 18]) # 3
for version below 3.5:
from functools import reduce DEF GCD (A, B): Return GCD (B, A% B) IF B ELSE A Reduce (GCD, [3, 6, 12]) # 3
take the first two numbers. We are looking for nodes (let’s call it
nod ). We take the third number, looking for a node to it and
Nod . Repeat until the numbers are running out.