Search code examples
pythonlistgreatest-common-divisor

How to quickly find GCD for a million numbers


I tried this:

from fractions import gcd
from functools import reduce

def solution(list):

     x = reduce(gcd, list)
     return x

but it's taking a long time


Solution

  • if you take one million random numbers between 800k and 2M, the probability that the GCD of them is greater than 1 is really really really low, try return 1 and see what happens