| Bill Allombert on Tue, 10 Feb 2026 17:34:34 +0100 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
| Re: Is short-circuit evaluation possible with parfor() ? |
On Tue, Feb 10, 2026 at 12:45:28AM +0100, hermann@stamm-wilbrandt.de 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?
Yes, you can use return or break inside the second expression, as with regular for loop.
Note:
There is no reason to have s et p global variable, and you do not have to add export()
for my() variables:
isok2(n) = my(p=prime(n+1)#);parfor(i=2,n+1,ispseudoprime(1+p/prime(i)),r,if(r,return(0)));1;
Cheers,
Bill.