Re: Is short-circuit evaluation possible with parfor() ?
"Ruud H.G. van Tol" <[email protected]> Tue, 10 Feb 2026 07:23:22 +0100
| Newsgroups | gmane.comp.mathematics.pari.user |
|---|---|
| Message-ID | <[email protected]> |
On 2026-02-10 00:45, [email protected] wrote: > Two months ago I got my first sequence approved on oeis.org: > "Numbers k such that no numbers of the form 1 + (product of k distinct > primes of first k+1 primes) are prime." > https://oeis.org/A391020 > > The Pari code I provided uses forsubset (although not needed) which > cannot be parallelized, and vecprod of primes instead of Pari > primorial operator. > > Now I have a parallel isok2() version which works superfast: > > hermann@x3950-X6:~$ gp -q > ? default(nbthreads) > 192 > ? isok2(n) = > {s=0;p=prime(n+1)#;export(s,p);parfor(i=2,n+1,ispseudoprime(1+p/prime(i)),r,s+=r);s==0}; > ? isok2(1993) > 1 > ? ## > *** last result: cpu time 1h, 24min, 36,945 ms, real time 28,186 ms. > ? isok2(1994) > 0 > ? ## > *** last result: cpu time 1h, 25min, 5,259 ms, real time 28,404 ms. > ? > > So for numbers of A391020 the complete loop has to be executed in > order to conform no prime exists. But for 1994 which does not belong > to A391020 computation should be aborted on first prime detection. Is > short-circuit evaluation like available for boolean expressions in eg. > C++ possible for parfor() somehow? See also the docs of parfor: It is allowed for expr2 to exit the loop using break/next/return. isok3(n)= { my( s=0, p=prime(n+1)# ); parfor ( i=2 , n+1 , ispseudoprime(1+p/prime(i)) , r , (s+=r) && break ); !s; } ? isok(313) cpu time = 1,382 ms, real time = 1,389 ms. %25 = 0 ? isok2(313) cpu time = 2,431 ms, real time = 260 ms. %26 = 0 ? isok3(313) cpu time = 1,077 ms, real time = 117 ms. %27 = 0 ? upto(lim)= [ n |n<-[1..lim], isok3(n) ]; ? upto(200) cpu time = 8,714 ms, real time = 1,086 ms. %30 = [18, 23, 24, 32, 50, 78, 86, 88, 115, 118, 131, 133, 137, 143, 145, 146, 149, 152, 153, 157, 159, 162, 165, 190] BTW, for the OEIS-entry, such an "upto" (or "a_upto") is IMO preferred over any code-example that uses print1. -- Ruud