6
$\begingroup$

Bug introduced in 11.3 or earlier

The program:

 CoprimeTriplets = 
  Compile[{n}, 
   Module[{c = 0}, 
    Do[If[CoprimeQ[i, j, i - j], ++c], {i, 3, n}, {j, 1, IntegerPart[i/2]}];
  c], RuntimeOptions -> "Speed"];CoprimeTriplets[1000] 

with c=0 gives wrong answer, whereas with c=1:

 CoprimeTriplets = 
  Compile[{n}, 
   Module[{c = 1}, 
    Do[If[CoprimeQ[i, j, i - j], ++c], {i, 3, n}, {j, 1, IntegerPart[i/2]}]; 
  c-1], RuntimeOptions -> "Speed"]; CoprimeTriplets[1000]

gives correct answer c-1 = 152095

Check:

c = 0; n = 1000;  
  Do[ If[CoprimeQ[i, j, i - j], ++c], {i, 3, n}, {j, 1,IntegerPart[i/2]}]; c

This happens for all n in the compiled version...

Mathematica 11.3

In the above example, we are checking number of all possible solutions to r+s=t up to t=n, such that {r,s,t} are coprime.

$\endgroup$

Your Answer

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

Browse other questions tagged or ask your own question.