1
$\begingroup$

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}
$\endgroup$
  • 5
    $\begingroup$ 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. $\endgroup$ – Daniel Lichtblau Jan 6 '13 at 20:49
  • 1
    $\begingroup$ 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. $\endgroup$ – Mr.Wizard Jan 12 '13 at 10:52
  • $\begingroup$ @Mr.Wizard I've tried it for Timing[GCD[100000! + 1, 1000000!]] and it is 9 times slower than Mathematica $\endgroup$ – Mohsen Afshin Jan 12 '13 at 13:17
  • $\begingroup$ I'm actually happy to hear that, though of course that isn't any help to you. $\endgroup$ – Mr.Wizard Jan 12 '13 at 13:26
2
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.