# Possible Compile bug

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.

• Perhaps you should be made aware that CoprimeQ[] is not a compilable function. Apr 3, 2019 at 14:53
• Yes, but how it compiles with c=1? Apr 3, 2019 at 14:59
• Seems to be the same bug as mentioned here: mathematica.stackexchange.com/q/25075/1871 Apr 3, 2019 at 15:21
• @xzczd Not the same (different broken/fixed/broken dates). I reported this new one. Apr 3, 2019 at 17:08
• Until it is fixed, a workaround may be to use Block instead of Module. Apr 3, 2019 at 17:16