Re: Is short-circuit evaluation possible with parfor() ?

[email protected] Tue, 10 Feb 2026 08:58:10 +0100
Newsgroups gmane.comp.mathematics.pari.user
Message-ID <[email protected]>
On 2026-02-10 07:23, Ruud H.G. van Tol wrote:
> 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;
> }
> 
Thanks, but break did not help:

hermann@x3950-X6:~$ gp -q
? 
isok2(n)={s=0;p=prime(n+1)#;export(s,p);parfor(i=2,n+1,ispseudoprime(1+p/prime(i)),r,if(r,s=1;break));s==0};
? isok2(1994)
0
? ##
   ***   last result: cpu time 1h, 21min, 39,829 ms, real time 26,349 ms.
? isok2(1993)
1
? ##
   ***   last result: cpu time 1h, 24min, 46,507 ms, real time 28,423 ms.
?


So I looked in the doc and there it states that return should abort, but 
I cannot measure that:

? 
isok2(n)={p=prime(n+1)#;export(p);parfor(i=2,n+1,ispseudoprime(1+p/prime(i)),r,if(r,return(0)));1};
? isok2(1994)
0
? ##
   ***   last result: cpu time 1h, 21min, 49,788 ms, real time 27,072 ms.
?


I do the same  if(r,return(...)) as in the doc, what am I missing?


Regards,

Hermann.