Sequence of primorial primes: Difference between revisions

Content added Content deleted
(→‎{{header|Phix}}: replaced with gmp version)
Line 2,545: Line 2,545:


=={{header|Phix}}==
=={{header|Phix}}==
{{libheader|mpfr}}
Horribly slow...
<lang Phix>include primes.e
Uses primes[] and add_block() from [[Extensible_prime_generator#Phix|Extensible_prime_generator]]
include mpfr.e
and Miller_Rabin() from [[Miller–Rabin_primality_test#Phix|Miller–Rabin_primality_test]]
<lang Phix>while length(primes)<100 do add_block() end while


constant limit = 9999
integer primorial = 1

mpz p = mpz_init(1),
p1 = mpz_init()
randstate state = gmp_randinit_mt()
atom t0 = time()
constant integer base = iff(machine_bits()=32?1000:1000000000)
constant integer digits_per = length(sprint(base-1))
integer found = 0, i
for n=1 to limit do
constant string dpfmt = sprintf("%%0%dd",digits_per)
mpz_mul_si(p, p, get_prime(n))
for i=-1 to +1 do
-- start at the back, number grows to the right (like little endian)
mpz_add_si(p1, p, i)
sequence bigint = {primorial}
if mpz_probable_prime_p(p1,state,25) then
atom result
integer l = mpz_sizeinbase(p,10)

string ps = iff(l>20?sprintf("%d digits",l)
procedure bi_mul(integer pn)
:mpz_get_str(p))
integer carry = 0
printf(1,"%d (%s) is a primorial prime\n",{n,ps})
for i=1 to length(bigint) do
result = pn*bigint[i]+carry
carry = floor(result/base)
bigint[i] = result-carry*base
end for
while carry <> 0 do
result = carry
carry = floor(carry/base)
bigint &= result-carry*base
end while
end procedure

procedure bi_add(integer carry)
for i=1 to length(bigint) do
result = bigint[i]+carry
carry = floor(result/base)
bigint[i] = result-carry*base
if carry=0 then return end if
end for
if carry!=0 then
if carry<0
or carry!=floor(carry/base) then
?9/0 -- sanity check
end if
bigint &= carry
end if
end procedure

integer found = 0
for n=1 to 100 do
bi_mul(primes[n])
sequence save_bigint = bigint
for pm1=-1 to 2 by 3 do -- (ie n-1 then n+1)
bi_add(pm1)
string bis = sprint(bigint[$])
for i=length(bigint)-1 to 1 by -1 do
bis &= sprintf(dpfmt,bigint[i])
end for
printf(1,"working [%d]...\r",{n})
if Miller_Rabin(bis,1)=PROBABLY_PRIME then
if length(bis)>20 then
bis = sprintf("[big (%d digits)]",length(bis))
end if
printf(1,"%d (%s) is a primorial prime\n",{n,bis})
found += 1
found += 1
exit
exit
end if
end if
end for
end for
if found>=20 then exit end if
bigint = save_bigint
end for
if found>=12 then exit end if
{p,p1} = mpz_clear({p,p1})
end for</lang>
state = gmp_randclear(state)</lang>
{{out}}
{{out}}
<pre>
<pre>
1 (3) is a primorial prime
1 (2) is a primorial prime
2 (5) is a primorial prime
2 (6) is a primorial prime
3 (29) is a primorial prime
3 (30) is a primorial prime
4 (211) is a primorial prime
4 (210) is a primorial prime
5 (2309) is a primorial prime
5 (2310) is a primorial prime
6 (30029) is a primorial prime
6 (30030) is a primorial prime
11 (200560490131) is a primorial prime
11 (200560490130) is a primorial prime
13 (304250263527209) is a primorial prime
13 (304250263527210) is a primorial prime
24 ([big (35 digits)]) is a primorial prime
24 (35 digits) is a primorial prime
66 ([big (131 digits)]) is a primorial prime
66 (131 digits) is a primorial prime
68 ([big (136 digits)]) is a primorial prime
68 (136 digits) is a primorial prime
75 ([big (154 digits)]) is a primorial prime
75 (154 digits) is a primorial prime
167 (413 digits) is a primorial prime
171 (425 digits) is a primorial prime
172 (428 digits) is a primorial prime
287 (790 digits) is a primorial prime
310 (866 digits) is a primorial prime
352 (1007 digits) is a primorial prime
384 (1116 digits) is a primorial prime
457 (1368 digits) is a primorial prime
</pre>
</pre>