0

I need a very very fast python algorithm that can find max of gcd of 100,000 numbers in shortest time!

and here is my code but it's too slow for huge lists.

for x in mlist:
    for y in mlist[i:]:
        tmp = gcd(x, y)
        if tmp > highest:
            highest = tmp
    i += 1
print(highest)

thank you

  • There's no code there. If your code works and you're looking for improvements you might want to look at [codereview](http://codereview.stackexchange.com/) instead – Farhan.K Oct 07 '16 at 15:39
  • 1
    Most likely you'll end up with `1` during the computation. Stop there. – karakfa Oct 07 '16 at 15:43
  • Take a look at the [Euclidean Algorithm](https://en.wikipedia.org/wiki/Euclidean_algorithm) for calculating the GCD of two numbers efficiently. – pzp Oct 07 '16 at 15:45
  • @pzp Since he didn't post the code of `gcd()` what makes you think he's using something else? – Barmar Oct 07 '16 at 15:54
  • I search for fastest way It's not a duplicate don't answer if you can't @Barmar – mehran rafiee Oct 07 '16 at 15:59
  • @mehranrafiee There are lots of other similar questions. Look in the **Related** sidebar. I'm sure at least one of them will help you. – Barmar Oct 07 '16 at 16:02
  • This is *not* a duplicate of [what is the fastest way to find the GCD of n numbers](https://stackoverflow.com/a/27037888/3789665), let alone `an exact duplicate`. As of 2017/7/20, I can not find any of the ten posts listed under "Related" helpful beyond finding the GCD of *one* pair of numbers. – greybeard Jul 20 '17 at 19:01

0 Answers0