1

Is there any chance to write a faster GCD than the built-in one in Mathematica?

@Mr.Wizard has written one in this question (although it's not for this purpose) which is 6 times slower on a 100k digits:

In[1]:= whileGCD=Module[{a=#,b=#2},While[b!=0,{a,b}={b,Mod[a,b]}];a]&;
In[2]:= Primo[n_]:=Product[i,{i,Prime[Range[n]]}]

In[10]:= prod=Primo[500000];
In[17]:= m=22Primo[20500] +1;

In[16]:= Timing[GCD[m,prod]]
Out[16]= {0.25,1}

In[12]:= Timing[whileGCD[m,prod]]
Out[12]= {1.609,1}
Mohsen Afshin
  • 985
  • 1
  • 5
  • 17
  • 5
    The short answer is no. The longer answer is maybe, if you are a GMP developer with access to some fairly low level NTT (number theory transform) code. Even then it will be difficult. – Daniel Lichtblau Jan 06 '13 at 20:49
  • 1
    I don't have it installed at the moment so I cannot compare them but you may wish to try PARI/GP. I have found that to be much faster than Mathematica's built-in functions in at least one instance. – Mr.Wizard Jan 12 '13 at 10:52
  • @Mr.Wizard I've tried it for Timing[GCD[100000! + 1, 1000000!]] and it is 9 times slower than Mathematica – Mohsen Afshin Jan 12 '13 at 13:17
  • I'm actually happy to hear that, though of course that isn't any help to you. – Mr.Wizard Jan 12 '13 at 13:26

1 Answers1

3

Daniel Lichtblau answered this question in the comments

The short answer is no. The longer answer is maybe, if you are a GMP developer with access to some fairly low level NTT (number theory transform) code. Even then it will be difficult.

C. E.
  • 70,533
  • 6
  • 140
  • 264