Question: Weird compiler problem

I have tried to compile a simple program looking for counterexamples to a conjecture by Goldbach that every odd number is a sum of a prime and twice a square. Stern gave the counterexamples 5777 and 5993 a long time ago, so this is mainly for instruction purposes.

My program

othergoldbachFCi:=proc(sieve::Array(1..10000,datatype=integer[4]))
   local i,j,M; 
   M:=10000;
   for i from 1 to M do
      if(sieve[i] mod 2=0) then 
         for j from 1 to iquo(M-i,2*i+1) do 
            sieve[i+j*(2*i+1)]:=2*iquo(sieve[i+j*(2*i+1)],2)+1;
         end do; 
         for j from 1 to floor(sqrt(M-i)) do 
            sieve[i+j*j]:=2+sieve[i+j*j] mod 2;  
         end do;
      else 
         if(sieve[i]<2) then 
            printf("%d is a counterexample\n",2*i+1); 
         end if; 
      end if; 
   end do; 
end proc;

works fine and compiles without problems. (I have to pass the array for the sieve because compiled program cannot declare memory, and it has to be 4 bytes even though I only need to use the lower 2 bit).  But the compiled version breaks with

   Error, (in compiledOGi) array index out of bounds

 

already in the first instance of the for loop. After a lot of trial and error I found out that if I replaced the line 

   sieve[i+j*j]:=2+sieve[i+j*j] mod 2;  

by

   temp:=iquo(sieve[i+j*j],2);
   sieve[i+j*j]:=2+(sieve[i+j*j]-2*temp);  

 

then the problem goes away and the compiled version works beautifully. It is not enough to do the mod 2 computation in temp, and not enough to replace mod by iquo in one line. 

This makes very little sense to me. Can anybody explain? I'm still using Maple 2016 and do not know if the issue has been resolved in Maple 2017.

 

Best,

Soren

 

Please Wait...