Home python How to find a node of several numbers?

How to find a node of several numbers?

Author

Date

Category

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.gcd Changed 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

Example:

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

Answer 2

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.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions