Ruud H.G. van Tol on Wed, 07 Jan 2026 21:50:42 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: parforstep() available, but parsumstep() is missing



On 2026-01-07 20:31, hermann@stamm-wilbrandt.de wrote:
[...] parsum(k=1,10^9,isprime(k^2+(k+1)^2)) is fast, but tests too much values of k. For values k=1 (mod 5) and k=3 (mod 5) the term k^2+(k+1)^2 is divisible by 5, so no isprime() test needed. The equivalent computation (+1 needed since 5=1^2+2^2 needs to be counted as prime) would be this, and should reduce runtime by 40%:

{
  1
  + parsumstep(k=5,10^9,5,isprime(k^2+(k+1)^2))
  + parsumstep(k=2,10^9,5,isprime(k^2+(k+1)^2))
  + parsumstep(k=4,10^9,5,isprime(k^2+(k+1)^2))
} [...]

Common workarounds indeed are slower:

? 1 + parsum(n=1,10^7, my(k=n\3*5+[0, 2, 4][n%3+1]); isprime(k^2+(k+1)^2))
cpu time = 11,876 ms, real time = 1,345 ms.
%1 = 1439667

? 1 + parsum(k=1,10^8\6, k%5%2 && next; isprime(k^2+(k+1)^2))
cpu time = 9,656 ms, real time = 1,104 ms.
%2 = 1439667

? parsum(k=1,10^8\6, isprime(k^2+(k+1)^2))
cpu time = 8,943 ms, real time = 1,020 ms.
%3 = 1439667

-- Ruud