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.
CoprimeQ[]
is not a compilable function. $\endgroup$ – J. M.'s ennui♦ Apr 3 '19 at 14:53Block
instead ofModule
. $\endgroup$ – Henrik Schumacher Apr 3 '19 at 17:16