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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.