Extreme primes: Difference between revisions
m (just 30) |
(Added Easylang) |
||
(27 intermediate revisions by 11 users not shown) | |||
Line 1: | Line 1: | ||
{{draft task}} |
|||
<big>Definition</big> |
|||
;Definition |
|||
<br><br> |
|||
Write down the first prime number, add the next prime number and if it is prime, add it to the series and so on. These primes are called '''extreme primes''' |
Write down the first prime number, add the next prime number and if it is prime, add it to the series and so on. These primes are called '''extreme primes'''. |
||
<br> |
|||
;Task |
|||
Find and display the first '''30 extreme primes''' on this page. |
|||
<br><br> |
|||
<br> |
|||
Find and display the first '''30 p extreme prime''' number on this page. |
|||
;Stretch |
|||
Find and display the '''1,000th''', '''2,000th''', '''3,000th''', '''4,000th''' and '''5,000th''' extreme primes and for each one show the prime number '''p''' up to and including which primes need to be summed to obtain it. |
|||
<br> |
|||
;Related (and near duplicate) tasks |
|||
* [[Prime numbers p for which the sum of primes less than or equal to p is prime]] |
|||
* [[Summarize primes]] |
|||
;Reference |
|||
[[oeis:A013918|OEIS sequence A013918]] |
|||
<br><br> |
<br><br> |
||
=={{header|ALGOL 68}}== |
|||
{{libheader|ALGOL 68-primes}} |
|||
<syntaxhighlight lang="algol68"> |
|||
BEGIN # sum the primes below n and report the sums that are prime # |
|||
PR read "primes.incl.a68" PR # include prime utilities # |
|||
[]BOOL prime = PRIMESIEVE 2 000 000; # sieve the primes to 2 000 000 # |
|||
# returns TRUE if n is prime, FALSE otherwise # |
|||
PROC is prime = ( LONG INT n )BOOL: |
|||
IF n <= UPB prime THEN prime[ SHORTEN n ] |
|||
ELIF NOT ODD n THEN FALSE |
|||
ELSE |
|||
LONG INT f := 3; |
|||
LONG INT f2 := 9; |
|||
LONG INT to next := 16; |
|||
BOOL is a prime := TRUE; |
|||
WHILE f2 <= n AND is a prime DO |
|||
is a prime := n MOD f /= 0; |
|||
f +:= 2; |
|||
f2 +:= to next; |
|||
to next +:= 8 |
|||
OD; |
|||
is a prime |
|||
FI # is prime # ; |
|||
# sum the primes and test the sums # |
|||
print( ( "The first 30 extreme primes:", newline ) ); |
|||
print( ( whole( 2, -6 ), " " ) ); # 2 is the first prime so is "extreme" # |
|||
INT prime sum count := 1; |
|||
LONG INT prime sum := 2; |
|||
LONG INT candidate := 1; |
|||
WHILE prime sum count < 5 000 DO |
|||
IF is prime( candidate +:= 2 ) THEN |
|||
prime sum +:= candidate; # have another prime # |
|||
IF is prime( prime sum ) THEN # the prime sum is also prime # |
|||
prime sum count +:= 1; |
|||
IF prime sum count <= 30 THEN |
|||
print( ( whole( prime sum, -6 ) |
|||
, IF prime sum count MOD 10 = 0 THEN newline ELSE " " FI |
|||
) |
|||
) |
|||
ELIF prime sum count MOD 1000 = 0 THEN |
|||
print( ( "Extreme prime ", whole( prime sum count, -5 ) |
|||
, " is ", whole( candidate, -12 ) |
|||
, ", sum: ", whole( prime sum, -18 ) |
|||
, newline |
|||
) |
|||
) |
|||
FI |
|||
FI |
|||
FI |
|||
OD |
|||
END |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The first 30 extreme primes: |
|||
2 5 17 41 197 281 7699 8893 22039 24133 |
|||
25237 28697 32353 37561 38921 43201 44683 55837 61027 66463 |
|||
70241 86453 102001 109147 116533 119069 121631 129419 132059 263171 |
|||
Extreme prime 1000 is 196831, sum: 1657620079 |
|||
Extreme prime 2000 is 495571, sum: 9744982591 |
|||
Extreme prime 3000 is 808837, sum: 24984473177 |
|||
Extreme prime 4000 is 1152763, sum: 49394034691 |
|||
Extreme prime 5000 is 1500973, sum: 82195983953 |
|||
</pre> |
|||
=={{header|C}}== |
|||
{{trans|Wren}} |
|||
{{libheader|GMP}} |
|||
<syntaxhighlight lang="c">#include <stdio.h> |
|||
#include <locale.h> |
|||
#include <gmp.h> |
|||
int main() { |
|||
int i, m, extremes[30], count = 1; |
|||
mpz_t sum, p; |
|||
unsigned long usum, up; |
|||
extremes[0] = 2; |
|||
mpz_init_set_ui(sum, 2); |
|||
mpz_init_set_ui(p, 3); |
|||
setlocale(LC_NUMERIC, ""); |
|||
while (1) { |
|||
mpz_add(sum, sum, p); |
|||
if (mpz_probab_prime_p(sum, 5) > 0) { |
|||
++count; |
|||
if (count <= 30) { |
|||
extremes[count-1] = (int)mpz_get_ui(sum); |
|||
} |
|||
if (count == 30) { |
|||
printf("The first 30 extreme primes are:\n"); |
|||
for (i = 0; i < 30; ++i) { |
|||
printf("%'7d ", extremes[i]); |
|||
if (!((i+1)%6)) printf("\n"); |
|||
} |
|||
printf("\n"); |
|||
} else if (!(count % 1000)) { |
|||
m = count / 1000; |
|||
if (m < 6 || m == 30 || m == 40 || m == 50) { |
|||
usum = mpz_get_ui(sum); |
|||
up = mpz_get_ui(p); |
|||
printf("The %'6dth extreme prime is: %'18ld for p <= %'10ld\n", count, usum, up); |
|||
if (m == 50) break; |
|||
} |
|||
} |
|||
} |
|||
mpz_nextprime(p, p); |
|||
} |
|||
mpz_clear(sum); |
|||
mpz_clear(p); |
|||
return 0; |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Identical to Wren output. |
|||
</pre> |
|||
=={{header|EasyLang}}== |
|||
<syntaxhighlight> |
|||
fastfunc isprim num . |
|||
i = 2 |
|||
while i <= sqrt num |
|||
if num mod i = 0 |
|||
return 0 |
|||
. |
|||
i += 1 |
|||
. |
|||
return 1 |
|||
. |
|||
prim = 2 |
|||
proc nextprim . . |
|||
repeat |
|||
prim += 1 |
|||
until isprim prim = 1 |
|||
. |
|||
. |
|||
while cnt < 30 |
|||
n += prim |
|||
if isprim n = 1 |
|||
cnt += 1 |
|||
write n & " " |
|||
. |
|||
nextprim |
|||
. |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
2 5 17 41 197 281 7699 8893 22039 24133 25237 28697 32353 37561 38921 43201 44683 55837 61027 66463 70241 86453 102001 109147 116533 119069 121631 129419 132059 263171 |
|||
</pre> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
Line 36: | Line 193: | ||
{{out}} |
{{out}} |
||
<pre>Similar to Ring entry.</pre> |
<pre>Similar to Ring entry.</pre> |
||
=={{header|Go}}== |
|||
{{trans|Wren}} |
|||
{{libheader|GMP(Go wrapper)}} |
|||
{{libheader|Go-rcu}} |
|||
Based on Wren's GMP version but about 4 times slower as our GMP wrapper doesn't expose the ''Mpz_nextprime'' function. We're therefore using our own very simple version instead which is much slower. |
|||
<syntaxhighlight lang="go">package main |
|||
import ( |
|||
"fmt" |
|||
big "github.com/ncw/gmp" |
|||
"rcu" |
|||
) |
|||
func nextPrime(n *big.Int) *big.Int { |
|||
m := new(big.Int).Set(n) |
|||
z := new(big.Int) |
|||
zero := new(big.Int) |
|||
one := big.NewInt(1) |
|||
two := big.NewInt(2) |
|||
if z.Rem(m, two).Cmp(zero) == 0 { |
|||
m.Add(m, one) |
|||
} else { |
|||
m.Add(m, two) |
|||
} |
|||
for { |
|||
if m.ProbablyPrime(0) { |
|||
return m |
|||
} |
|||
m.Add(m, two) |
|||
} |
|||
} |
|||
func main() { |
|||
extremes := []int{2} |
|||
sum := big.NewInt(2) |
|||
count := 1 |
|||
p := big.NewInt(3) |
|||
for { |
|||
sum.Add(sum, p) |
|||
if sum.ProbablyPrime(0) { |
|||
count++ |
|||
if count <= 30 { |
|||
extremes = append(extremes, int(sum.Uint64())) |
|||
} |
|||
if count == 30 { |
|||
fmt.Println("The first 30 extreme primes are:") |
|||
rcu.PrintTable(extremes, 6, 7, true) |
|||
fmt.Println() |
|||
} else if count%1000 == 0 { |
|||
m := count / 1000 |
|||
if m < 6 || m == 30 || m == 40 || m == 50 { |
|||
scount := rcu.Commatize(count) |
|||
ssum := rcu.Commatize(sum.Uint64()) |
|||
sp := rcu.Commatize(p.Uint64()) |
|||
fmt.Printf("The %6sth extreme prime is: %18s for p <= %10s\n", scount, ssum, sp) |
|||
if m == 50 { |
|||
return |
|||
} |
|||
} |
|||
} |
|||
} |
|||
p.Set(nextPrime(p)) |
|||
} |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Identical to Wren output. |
|||
</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
Line 46: | Line 273: | ||
This would be more efficient if we had used 3e3 (216 extreme primes) rather than 4e4 (1942 extreme primes) |
This would be more efficient if we had used 3e3 (216 extreme primes) rather than 4e4 (1942 extreme primes) |
||
=={{header|jq}}== |
|||
'''Adapted from [[#Wren|Wren]]''' |
|||
'''Works with both jq and gojq, the C and Go implementations of jq''' ... |
|||
Using the generic utilities given immediately below, one solution to the problem |
|||
of generating the first 30 extreme primes could be written as follows: |
|||
<syntaxhighlight lang=jq> |
|||
def primes: |
|||
2 | recurse(nextprime); |
|||
def extremes: |
|||
foreach primes as $p (0; . + $p) |
|||
| select(is_prime); |
|||
limit(30; extremes) |
|||
</syntaxhighlight> |
|||
A program that solves both the primary and stretch tasks is given below. |
|||
'''Generic Utilities''' |
|||
<syntaxhighlight lang=jq> |
|||
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .; |
|||
def nwise($n): |
|||
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end; |
|||
n; |
|||
def is_prime: |
|||
. as $n |
|||
| if ($n < 2) then false |
|||
elif ($n % 2 == 0) then $n == 2 |
|||
elif ($n % 3 == 0) then $n == 3 |
|||
elif ($n % 5 == 0) then $n == 5 |
|||
elif ($n % 7 == 0) then $n == 7 |
|||
elif ($n % 11 == 0) then $n == 11 |
|||
elif ($n % 13 == 0) then $n == 13 |
|||
elif ($n % 17 == 0) then $n == 17 |
|||
elif ($n % 19 == 0) then $n == 19 |
|||
else |
|||
($n | sqrt) as $rt |
|||
| 23 |
|||
| until( . > $rt or ($n % . == 0); .+2) |
|||
| . > $rt |
|||
end; |
|||
def nextprime: |
|||
(if .%2==0 then 1 else 2 end) as $n |
|||
| first(range(.+$n; infinite;2) | select(is_prime)); |
|||
</syntaxhighlight> |
|||
'''The Tasks''' |
|||
<syntaxhighlight lang=jq> |
|||
{ extremes:[2], |
|||
sum:2, |
|||
p:3, |
|||
count:1 } |
|||
| while (.count <= 5000; |
|||
.emit = null |
|||
| .sum += .p |
|||
| if .sum|is_prime |
|||
then .count += 1 |
|||
| if .count <= 30 |
|||
then .extremes += [.sum] |
|||
| if .count == 30 |
|||
then .emit = "The first 30 extreme primes are:\n" |
|||
| .emit += ([.extremes | nwise(10) | map(lpad(7)) | join(" ") ] | join("\n")) |
|||
| .emit += "\n" |
|||
else . |
|||
end |
|||
elif .count % 1000 == 0 |
|||
then .emit = "The \(.count)th extreme prime is: \(.sum) for p <= \(.p)" |
|||
else . |
|||
end |
|||
else . |
|||
end |
|||
| .p |= nextprime |
|||
) |
|||
| .emit // empty |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
The first 30 extreme primes are: |
|||
2 5 17 41 197 281 7699 8893 22039 24133 |
|||
25237 28697 32353 37561 38921 43201 44683 55837 61027 66463 |
|||
70241 86453 102001 109147 116533 119069 121631 129419 132059 263171 |
|||
The 1000th extreme prime is: 1657620079 for p <= 196831 |
|||
The 2000th extreme prime is: 9744982591 for p <= 495571 |
|||
The 3000th extreme prime is: 24984473177 for p <= 808837 |
|||
The 4000th extreme prime is: 49394034691 for p <= 1152763 |
|||
The 5000th extreme prime is: 82195983953 for p <= 1500973 |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Line 85: | Line 405: | ||
263171 |
263171 |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=== Stretch task === |
|||
{{trans|Python}} |
|||
<syntaxhighlight lang="julia">using Primes |
|||
let |
|||
ecount, p, n = 0, 0, 0 |
|||
while ecount < 50_000 |
|||
p = nextprime(p + 1) |
|||
n += p |
|||
if isprime(n) |
|||
ecount += 1 |
|||
if ecount < 31 |
|||
println("Sum of prime series up to $p: prime $n") |
|||
elseif ecount in [1000, 2000, 3000, 4000, 5000, 30_000, 40_000, 50_000] |
|||
println("Sum of $ecount in prime series up to $p: prime $n") |
|||
end |
|||
end |
|||
end |
|||
end |
|||
</syntaxhighlight>{{out}} |
|||
<pre> |
|||
Sum of prime series up to 2: prime 2 |
|||
Sum of prime series up to 3: prime 5 |
|||
Sum of prime series up to 7: prime 17 |
|||
Sum of prime series up to 13: prime 41 |
|||
Sum of prime series up to 37: prime 197 |
|||
Sum of prime series up to 43: prime 281 |
|||
Sum of prime series up to 281: prime 7699 |
|||
Sum of prime series up to 311: prime 8893 |
|||
Sum of prime series up to 503: prime 22039 |
|||
Sum of prime series up to 541: prime 24133 |
|||
Sum of prime series up to 557: prime 25237 |
|||
Sum of prime series up to 593: prime 28697 |
|||
Sum of prime series up to 619: prime 32353 |
|||
Sum of prime series up to 673: prime 37561 |
|||
Sum of prime series up to 683: prime 38921 |
|||
Sum of prime series up to 733: prime 43201 |
|||
Sum of prime series up to 743: prime 44683 |
|||
Sum of prime series up to 839: prime 55837 |
|||
Sum of prime series up to 881: prime 61027 |
|||
Sum of prime series up to 929: prime 66463 |
|||
Sum of prime series up to 953: prime 70241 |
|||
Sum of prime series up to 1061: prime 86453 |
|||
Sum of prime series up to 1163: prime 102001 |
|||
Sum of prime series up to 1213: prime 109147 |
|||
Sum of prime series up to 1249: prime 116533 |
|||
Sum of prime series up to 1277: prime 119069 |
|||
Sum of prime series up to 1283: prime 121631 |
|||
Sum of prime series up to 1307: prime 129419 |
|||
Sum of prime series up to 1321: prime 132059 |
|||
Sum of prime series up to 1949: prime 263171 |
|||
Sum of 1000 in prime series up to 196831: prime 1657620079 |
|||
Sum of 2000 in prime series up to 495571: prime 9744982591 |
|||
Sum of 3000 in prime series up to 808837: prime 24984473177 |
|||
Sum of 4000 in prime series up to 1152763: prime 49394034691 |
|||
Sum of 5000 in prime series up to 1500973: prime 82195983953 |
|||
Sum of 30000 in prime series up to 12437401: prime 4889328757567 |
|||
Sum of 40000 in prime series up to 17245391: prime 9207632380589 |
|||
Sum of 50000 in prime series up to 22272277: prime 15118097491121 |
|||
</pre> |
|||
=={{header|Nim}}== |
|||
{{incorrect|Nim|outputting the next prime which was not added to ep (yet)}} |
|||
{{libheader|Nim-Integers}} |
|||
We use third party package “integers” to get Miller-Rabin primality test. |
|||
<syntaxhighlight lang="Nim">import std/[strformat, strutils] |
|||
import integers |
|||
echo "First 30 extreme primes:" |
|||
var ep = newInteger(0) |
|||
var count = 0 |
|||
var lim = 1000 |
|||
var p = newInteger(2) |
|||
while true: |
|||
ep += p |
|||
p = p.nextPrime |
|||
if ep.isPrime: |
|||
inc count |
|||
if count <= 30: |
|||
stdout.write &"{ep:>6}" |
|||
stdout.write if count mod 6 == 0: '\n' else: ' ' |
|||
if count == 30: echo() |
|||
elif count == lim: |
|||
echo &"Sum of {count} in prime series up to {insertSep($p):>9}: prime {insertSep($ep):>14}" |
|||
inc lim, 1000 |
|||
if lim > 5000: break |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>First 30 extreme primes: |
|||
2 5 17 41 197 281 |
|||
7699 8893 22039 24133 25237 28697 |
|||
32353 37561 38921 43201 44683 55837 |
|||
61027 66463 70241 86453 102001 109147 |
|||
116533 119069 121631 129419 132059 263171 |
|||
Sum of 1000 in prime series up to 196_837: prime 1_657_620_079 |
|||
Sum of 2000 in prime series up to 495_587: prime 9_744_982_591 |
|||
Sum of 3000 in prime series up to 808_853: prime 24_984_473_177 |
|||
Sum of 4000 in prime series up to 1_152_773: prime 49_394_034_691 |
|||
Sum of 5000 in prime series up to 1_500_991: prime 82_195_983_953 |
|||
</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">constant</span> <span style="color: #000000;">lim</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">30 |
<span style="color: #008080;">constant</span> <span style="color: #000000;">lim</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">30</span> |
||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">extremes</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">extremes</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> |
||
<span style="color: #004080;">integer</span> <span style="color: #000000;">ep</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;"> |
<span style="color: #004080;">integer</span> <span style="color: #000000;">ep</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span> |
||
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">extremes</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">lim</span> <span style="color: #008080;">do</span> |
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">extremes</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">lim</span> <span style="color: #008080;">do</span> |
||
<span style="color: #000000;"> |
<span style="color: #000000;">np</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">ep</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">np</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ep</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">extremes</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">ep</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
<span style="color: #008080;">if</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ep</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">extremes</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">ep</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first %d extreme primes are:\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">extremes</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%,7d"</span><span style="color: #0000FF;">)})</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first %d extreme primes are:\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">extremes</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%,7d"</span><span style="color: #0000FF;">)})</span> |
||
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
|||
<span style="color: #004080;">mpz</span> <span style="color: #000000;">z</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ep</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">found</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">30</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p</span> |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">tgt</span> <span style="color: #008080;">in</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1e3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2e3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3e3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4e3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5e3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3e4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4e4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5e4</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">do</span> |
|||
<span style="color: #008080;">while</span> <span style="color: #000000;">found</span><span style="color: #0000FF;"><</span><span style="color: #000000;">tgt</span> <span style="color: #008080;">do</span> |
|||
<span style="color: #000000;">np</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">np</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #7060A8;">mpz_add_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">np</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">found</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
|||
<span style="color: #004080;">string</span> <span style="color: #000000;">zs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The %,dth extreme prime is %s (primes[1..%,d]<=%,d)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">tgt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">np</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">})</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
<!--</syntaxhighlight>--> |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
Line 106: | Line 543: | ||
61,027 66,463 70,241 86,453 102,001 109,147 |
61,027 66,463 70,241 86,453 102,001 109,147 |
||
116,533 119,069 121,631 129,419 132,059 263,171 |
116,533 119,069 121,631 129,419 132,059 263,171 |
||
The 1,000th extreme prime is 1,657,620,079 (primes[1..17,722]<=196,831) |
|||
The 2,000th extreme prime is 9,744,982,591 (primes[1..41,198]<=495,571) |
|||
The 3,000th extreme prime is 24,984,473,177 (primes[1..64,596]<=808,837) |
|||
The 4,000th extreme prime is 49,394,034,691 (primes[1..89,504]<=1,152,763) |
|||
The 5,000th extreme prime is 82,195,983,953 (primes[1..114,236]<=1,500,973) |
|||
The 30,000th extreme prime is 4,889,328,757,567 (primes[1..814,900]<=12,437,401) |
|||
The 40,000th extreme prime is 9,207,632,380,589 (primes[1..1,106,004]<=17,245,391) |
|||
The 50,000th extreme prime is 15,118,097,491,121 (primes[1..1,405,244]<=22,272,277) |
|||
</pre> |
</pre> |
||
=={{header|Python}}== |
|||
<syntaxhighlight lang="python">""" rosettacode.org/wiki/Extreme_primes """ |
|||
from sympy import isprime, nextprime |
|||
ecount, p, n = 0, 0, 0 |
|||
while ecount < 50_000: |
|||
p = nextprime(p) |
|||
n += p |
|||
if isprime(n): |
|||
ecount += 1 |
|||
if ecount < 31: |
|||
print(f'Sum of prime series up to {p}: prime {n}') |
|||
if ecount in [1000, 2000, 3000, 4000, 5000, 30_000, 40_000, 50_000]: |
|||
print( |
|||
f'Sum of {ecount :,} in prime series up to {p :,}: prime {n :,}') |
|||
</syntaxhighlight>{{out}} |
|||
<pre> |
|||
Sum of prime series up to 2: prime 2 |
|||
Sum of prime series up to 3: prime 5 |
|||
Sum of prime series up to 7: prime 17 |
|||
Sum of prime series up to 13: prime 41 |
|||
Sum of prime series up to 37: prime 197 |
|||
Sum of prime series up to 43: prime 281 |
|||
Sum of prime series up to 281: prime 7699 |
|||
Sum of prime series up to 311: prime 8893 |
|||
Sum of prime series up to 503: prime 22039 |
|||
Sum of prime series up to 541: prime 24133 |
|||
Sum of prime series up to 557: prime 25237 |
|||
Sum of prime series up to 593: prime 28697 |
|||
Sum of prime series up to 619: prime 32353 |
|||
Sum of prime series up to 673: prime 37561 |
|||
Sum of prime series up to 683: prime 38921 |
|||
Sum of prime series up to 733: prime 43201 |
|||
Sum of prime series up to 743: prime 44683 |
|||
Sum of prime series up to 839: prime 55837 |
|||
Sum of prime series up to 881: prime 61027 |
|||
Sum of prime series up to 929: prime 66463 |
|||
Sum of prime series up to 953: prime 70241 |
|||
Sum of prime series up to 1061: prime 86453 |
|||
Sum of prime series up to 1163: prime 102001 |
|||
Sum of prime series up to 1213: prime 109147 |
|||
Sum of prime series up to 1249: prime 116533 |
|||
Sum of prime series up to 1277: prime 119069 |
|||
Sum of prime series up to 1283: prime 121631 |
|||
Sum of prime series up to 1307: prime 129419 |
|||
Sum of prime series up to 1321: prime 132059 |
|||
Sum of prime series up to 1949: prime 263171 |
|||
Sum of 1,000 in prime series up to 196,831: prime 1,657,620,079 |
|||
Sum of 2,000 in prime series up to 495,571: prime 9,744,982,591 |
|||
Sum of 3,000 in prime series up to 808,837: prime 24,984,473,177 |
|||
Sum of 4,000 in prime series up to 1,152,763: prime 49,394,034,691 |
|||
Sum of 5,000 in prime series up to 1,500,973: prime 82,195,983,953 |
|||
Sum of 30,000 in prime series up to 12,437,401: prime 4,889,328,757,567 |
|||
Sum of 40,000 in prime series up to 17,245,391: prime 9,207,632,380,589 |
|||
Sum of 50,000 in prime series up to 22,272,277: prime 15,118,097,491,121 |
|||
</pre> |
|||
=={{header|Raku}}== |
|||
So we're just gonna keep doing the same task over and over with slightly different names? |
|||
Primes equal to the sum of the first k primes for some k. |
|||
<syntaxhighlight lang="raku" line>use Lingua::EN::Numbers; |
|||
say $_».&comma».fmt("%7s").batch(10).join: "\n" for |
|||
(([\+] (^∞).grep: &is-prime).grep: &is-prime)[^30,999,1999,2999,3999,4999];</syntaxhighlight> |
|||
{{out}} |
|||
<pre> 2 5 17 41 197 281 7,699 8,893 22,039 24,133 |
|||
25,237 28,697 32,353 37,561 38,921 43,201 44,683 55,837 61,027 66,463 |
|||
70,241 86,453 102,001 109,147 116,533 119,069 121,631 129,419 132,059 263,171 |
|||
1,657,620,079 |
|||
9,744,982,591 |
|||
24,984,473,177 |
|||
49,394,034,691 |
|||
82,195,983,953</pre> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
Line 150: | Line 673: | ||
done... |
done... |
||
</pre> |
</pre> |
||
=={{header|RPL}}== |
|||
{{works with|HP|49}} |
|||
« 0 → max sum |
|||
« { } 1 |
|||
'''DO''' |
|||
NEXTPRIME |
|||
DUP 'sum' STO+ |
|||
'''IF''' sum ISPRIME? '''THEN''' SWAP sum + SWAP '''END''' |
|||
'''UNTIL''' OVER SIZE max ≥ '''END''' |
|||
DROP2 |
|||
» » ‘<span style="color:blue">XPRIM</span>’ STO |
|||
30 <span style="color:blue">XPRIM</span> |
|||
{{out}} |
|||
<pre> |
|||
1: { 2 5 17 41 197 281 7699 8893 22039 24133 25237 28697 32353 37561 38921 43201 44683 55837 61027 66463 70241 86453 102001 109147 116533 119069 121631 129419 132059 263171 } |
|||
</pre> |
|||
=={{header|Ruby}}== |
|||
<syntaxhighlight lang="ruby">require 'prime' |
|||
sum, n = 0, 30 |
|||
extreme_primes = Prime.lazy.filter_map{|pr|sum += pr; [pr, sum] if sum.prime?} |
|||
puts "The first #{n} extreme primes are:" |
|||
extreme_primes.first(30).map(&:last).each_slice(10){|slice| puts "%8d"*slice.size % slice} |
|||
sum = 0 |
|||
puts |
|||
extreme_primes.first(5000).each_slice(1000).with_index(1) do|slice, i| |
|||
puts "The %dth extreme prime is sum of primes upto %10d: %12d" % [i*1000, *slice.last] |
|||
end |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>The first 30 extreme primes are: |
|||
2 5 17 41 197 281 7699 8893 22039 24133 |
|||
25237 28697 32353 37561 38921 43201 44683 55837 61027 66463 |
|||
70241 86453 102001 109147 116533 119069 121631 129419 132059 263171 |
|||
The 1000th extreme prime is sum of primes upto 196831: 1657620079 |
|||
The 2000th extreme prime is sum of primes upto 495571: 9744982591 |
|||
The 3000th extreme prime is sum of primes upto 808837: 24984473177 |
|||
The 4000th extreme prime is sum of primes upto 1152763: 49394034691 |
|||
The 5000th extreme prime is sum of primes upto 1500973: 82195983953 |
|||
</pre> |
|||
=={{header|Sidef}}== |
|||
{{trans|Julia}} |
|||
<syntaxhighlight lang="ruby">var(c,p,n) = (0,0,0) |
|||
while (c < 50_000) { |
|||
p.next_prime! |
|||
n += p |
|||
if (n.is_prime) { |
|||
++c |
|||
if (c <= 30) { |
|||
say "Sum of prime series up to #{p}: prime #{n}" |
|||
} |
|||
elsif (c ~~ [1000, 2000, 3000, 4000, 5000, 30_000, 40_000, 50_000]) { |
|||
say ("Sum of #{c.commify} in prime series up to #{p}: prime #{n}") |
|||
} |
|||
} |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Sum of prime series up to 2: prime 2 |
|||
Sum of prime series up to 3: prime 5 |
|||
Sum of prime series up to 7: prime 17 |
|||
Sum of prime series up to 13: prime 41 |
|||
Sum of prime series up to 37: prime 197 |
|||
Sum of prime series up to 43: prime 281 |
|||
Sum of prime series up to 281: prime 7699 |
|||
Sum of prime series up to 311: prime 8893 |
|||
Sum of prime series up to 503: prime 22039 |
|||
Sum of prime series up to 541: prime 24133 |
|||
Sum of prime series up to 557: prime 25237 |
|||
Sum of prime series up to 593: prime 28697 |
|||
Sum of prime series up to 619: prime 32353 |
|||
Sum of prime series up to 673: prime 37561 |
|||
Sum of prime series up to 683: prime 38921 |
|||
Sum of prime series up to 733: prime 43201 |
|||
Sum of prime series up to 743: prime 44683 |
|||
Sum of prime series up to 839: prime 55837 |
|||
Sum of prime series up to 881: prime 61027 |
|||
Sum of prime series up to 929: prime 66463 |
|||
Sum of prime series up to 953: prime 70241 |
|||
Sum of prime series up to 1061: prime 86453 |
|||
Sum of prime series up to 1163: prime 102001 |
|||
Sum of prime series up to 1213: prime 109147 |
|||
Sum of prime series up to 1249: prime 116533 |
|||
Sum of prime series up to 1277: prime 119069 |
|||
Sum of prime series up to 1283: prime 121631 |
|||
Sum of prime series up to 1307: prime 129419 |
|||
Sum of prime series up to 1321: prime 132059 |
|||
Sum of prime series up to 1949: prime 263171 |
|||
Sum of 1,000 in prime series up to 196831: prime 1657620079 |
|||
Sum of 2,000 in prime series up to 495571: prime 9744982591 |
|||
Sum of 3,000 in prime series up to 808837: prime 24984473177 |
|||
Sum of 4,000 in prime series up to 1152763: prime 49394034691 |
|||
Sum of 5,000 in prime series up to 1500973: prime 82195983953 |
|||
Sum of 30,000 in prime series up to 12437401: prime 4889328757567 |
|||
Sum of 40,000 in prime series up to 17245391: prime 9207632380589 |
|||
Sum of 50,000 in prime series up to 22272277: prime 15118097491121</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
===Wren-CLI=== |
|||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
<syntaxhighlight lang="wren">import "./math" for Int |
|||
This is very similar to the [[Prime numbers p for which the sum of primes less than or equal to p is prime]] task which itself was a near duplicate of the [[Summarize primes]] task so I'm highly dubious about converting it to a separate draft task. I also found it at [[oeis:A013918|OEIS-A013918]] though it doesn't appear to have a recognized name. |
|||
--- Based on the above reasons, please delete this task. Thanks in advance. --- CalmoSoft |
|||
<syntaxhighlight lang="ecmascript">import "./math" for Int |
|||
import "./fmt" for Fmt |
import "./fmt" for Fmt |
||
var primes = Int.primeSieve(2000) // say |
|||
var extremes = [2] |
var extremes = [2] |
||
var sum = 2 |
var sum = 2 |
||
var p = 3 |
|||
for (p in primes.skip(1)) { |
|||
var count = 1 |
|||
while (true) { |
|||
sum = sum + p |
sum = sum + p |
||
if (Int.isPrime(sum)) { |
if (Int.isPrime(sum)) { |
||
count = count + 1 |
|||
if ( |
if (count <= 30) { |
||
extremes.add(sum) |
|||
} |
|||
if (count == 30) { |
|||
System.print("The first 30 extreme primes are:") |
|||
Fmt.tprint("$,7d ", extremes, 6) |
|||
System.print() |
|||
} else if (count % 1000 == 0) { |
|||
Fmt.print("The $,r extreme prime is: $,14d for p <= $,9d", count, sum, p) |
|||
if (count == 5000) return |
|||
} |
|||
} |
} |
||
p = Int.nextPrime(p) |
|||
} |
|||
}</syntaxhighlight> |
|||
System.print("The first 30 extreme primes are:") |
|||
Fmt.tprint("$,7d ", extremes, 6)</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
The first 30 extreme primes are: |
The first 30 extreme primes are: |
||
2 5 17 41 197 281 |
2 5 17 41 197 281 |
||
7,699 8,893 22,039 24,133 25,237 28,697 |
7,699 8,893 22,039 24,133 25,237 28,697 |
||
32,353 37,561 38,921 43,201 44,683 55,837 |
32,353 37,561 38,921 43,201 44,683 55,837 |
||
61,027 66,463 70,241 86,453 102,001 109,147 |
61,027 66,463 70,241 86,453 102,001 109,147 |
||
116,533 119,069 121,631 129,419 132,059 263,171 |
116,533 119,069 121,631 129,419 132,059 263,171 |
||
The 1,000th extreme prime is: 1,657,620,079 for p <= 196,831 |
|||
The 2,000th extreme prime is: 9,744,982,591 for p <= 495,571 |
|||
The 3,000th extreme prime is: 24,984,473,177 for p <= 808,837 |
|||
The 4,000th extreme prime is: 49,394,034,691 for p <= 1,152,763 |
|||
The 5,000th extreme prime is: 82,195,983,953 for p <= 1,500,973 |
|||
</pre> |
|||
===Embedded (GMP)=== |
|||
{{libheader|Wren-gmp}} |
|||
<syntaxhighlight lang="wren">import "./gmp" for Mpz |
|||
import "./fmt" for Fmt |
|||
var extremes = [2] |
|||
var sum = Mpz.two |
|||
var count = 1 |
|||
var p = Mpz.three |
|||
while (true) { |
|||
sum.add(p) |
|||
if (sum.probPrime(5) > 0) { |
|||
count = count + 1 |
|||
if (count <= 30) { |
|||
extremes.add(sum.toNum) |
|||
} |
|||
if (count == 30) { |
|||
System.print("The first 30 extreme primes are:") |
|||
Fmt.tprint("$,7i ", extremes, 6) |
|||
System.print() |
|||
} else if (count % 1000 == 0) { |
|||
var m = count / 1000 |
|||
if (m < 6 || m == 30 || m == 40 || m == 50) { |
|||
Fmt.print("The $,8r extreme prime is: $,18i for p <= $,10i", count, sum, p) |
|||
if (m == 50) return |
|||
} |
|||
} |
|||
} |
|||
p.nextPrime |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The first 30 extreme primes are: |
|||
2 5 17 41 197 281 |
|||
7,699 8,893 22,039 24,133 25,237 28,697 |
|||
32,353 37,561 38,921 43,201 44,683 55,837 |
|||
61,027 66,463 70,241 86,453 102,001 109,147 |
|||
116,533 119,069 121,631 129,419 132,059 263,171 |
|||
The 1,000th extreme prime is: 1,657,620,079 for p <= 196,831 |
|||
The 2,000th extreme prime is: 9,744,982,591 for p <= 495,571 |
|||
The 3,000th extreme prime is: 24,984,473,177 for p <= 808,837 |
|||
The 4,000th extreme prime is: 49,394,034,691 for p <= 1,152,763 |
|||
The 5,000th extreme prime is: 82,195,983,953 for p <= 1,500,973 |
|||
The 30,000th extreme prime is: 4,889,328,757,567 for p <= 12,437,401 |
|||
The 40,000th extreme prime is: 9,207,632,380,589 for p <= 17,245,391 |
|||
The 50,000th extreme prime is: 15,118,097,491,121 for p <= 22,272,277 |
|||
</pre> |
</pre> |
||
Latest revision as of 00:19, 13 April 2024
- Definition
Write down the first prime number, add the next prime number and if it is prime, add it to the series and so on. These primes are called extreme primes.
- Task
Find and display the first 30 extreme primes on this page.
- Stretch
Find and display the 1,000th, 2,000th, 3,000th, 4,000th and 5,000th extreme primes and for each one show the prime number p up to and including which primes need to be summed to obtain it.
- Related (and near duplicate) tasks
- Reference
ALGOL 68
BEGIN # sum the primes below n and report the sums that are prime #
PR read "primes.incl.a68" PR # include prime utilities #
[]BOOL prime = PRIMESIEVE 2 000 000; # sieve the primes to 2 000 000 #
# returns TRUE if n is prime, FALSE otherwise #
PROC is prime = ( LONG INT n )BOOL:
IF n <= UPB prime THEN prime[ SHORTEN n ]
ELIF NOT ODD n THEN FALSE
ELSE
LONG INT f := 3;
LONG INT f2 := 9;
LONG INT to next := 16;
BOOL is a prime := TRUE;
WHILE f2 <= n AND is a prime DO
is a prime := n MOD f /= 0;
f +:= 2;
f2 +:= to next;
to next +:= 8
OD;
is a prime
FI # is prime # ;
# sum the primes and test the sums #
print( ( "The first 30 extreme primes:", newline ) );
print( ( whole( 2, -6 ), " " ) ); # 2 is the first prime so is "extreme" #
INT prime sum count := 1;
LONG INT prime sum := 2;
LONG INT candidate := 1;
WHILE prime sum count < 5 000 DO
IF is prime( candidate +:= 2 ) THEN
prime sum +:= candidate; # have another prime #
IF is prime( prime sum ) THEN # the prime sum is also prime #
prime sum count +:= 1;
IF prime sum count <= 30 THEN
print( ( whole( prime sum, -6 )
, IF prime sum count MOD 10 = 0 THEN newline ELSE " " FI
)
)
ELIF prime sum count MOD 1000 = 0 THEN
print( ( "Extreme prime ", whole( prime sum count, -5 )
, " is ", whole( candidate, -12 )
, ", sum: ", whole( prime sum, -18 )
, newline
)
)
FI
FI
FI
OD
END
- Output:
The first 30 extreme primes: 2 5 17 41 197 281 7699 8893 22039 24133 25237 28697 32353 37561 38921 43201 44683 55837 61027 66463 70241 86453 102001 109147 116533 119069 121631 129419 132059 263171 Extreme prime 1000 is 196831, sum: 1657620079 Extreme prime 2000 is 495571, sum: 9744982591 Extreme prime 3000 is 808837, sum: 24984473177 Extreme prime 4000 is 1152763, sum: 49394034691 Extreme prime 5000 is 1500973, sum: 82195983953
C
#include <stdio.h>
#include <locale.h>
#include <gmp.h>
int main() {
int i, m, extremes[30], count = 1;
mpz_t sum, p;
unsigned long usum, up;
extremes[0] = 2;
mpz_init_set_ui(sum, 2);
mpz_init_set_ui(p, 3);
setlocale(LC_NUMERIC, "");
while (1) {
mpz_add(sum, sum, p);
if (mpz_probab_prime_p(sum, 5) > 0) {
++count;
if (count <= 30) {
extremes[count-1] = (int)mpz_get_ui(sum);
}
if (count == 30) {
printf("The first 30 extreme primes are:\n");
for (i = 0; i < 30; ++i) {
printf("%'7d ", extremes[i]);
if (!((i+1)%6)) printf("\n");
}
printf("\n");
} else if (!(count % 1000)) {
m = count / 1000;
if (m < 6 || m == 30 || m == 40 || m == 50) {
usum = mpz_get_ui(sum);
up = mpz_get_ui(p);
printf("The %'6dth extreme prime is: %'18ld for p <= %'10ld\n", count, usum, up);
if (m == 50) break;
}
}
}
mpz_nextprime(p, p);
}
mpz_clear(sum);
mpz_clear(p);
return 0;
}
- Output:
Identical to Wren output.
EasyLang
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
prim = 2
proc nextprim . .
repeat
prim += 1
until isprim prim = 1
.
.
while cnt < 30
n += prim
if isprim n = 1
cnt += 1
write n & " "
.
nextprim
.
- Output:
2 5 17 41 197 281 7699 8893 22039 24133 25237 28697 32353 37561 38921 43201 44683 55837 61027 66463 70241 86453 102001 109147 116533 119069 121631 129419 132059 263171
FreeBASIC
#include "isprime.bas"
Dim As Integer limit = 2000, n, c = 0
Dim As Integer Primes()
For n = 1 To limit
If isPrime(n) Then
c += 1
Redim Preserve Primes(n)
Primes(c) = n
End If
Next n
Print "The first 30 extreme primes are:"
Dim As Integer sum = 0, row = 0
For n = 1 To Ubound(Primes)
sum += Primes(n)
If isPrime(sum) Then
row += 1
Print Using "########"; sum;
If row Mod 10 = 0 Then Print
End If
Next n
Sleep
- Output:
Similar to Ring entry.
Go
Based on Wren's GMP version but about 4 times slower as our GMP wrapper doesn't expose the Mpz_nextprime function. We're therefore using our own very simple version instead which is much slower.
package main
import (
"fmt"
big "github.com/ncw/gmp"
"rcu"
)
func nextPrime(n *big.Int) *big.Int {
m := new(big.Int).Set(n)
z := new(big.Int)
zero := new(big.Int)
one := big.NewInt(1)
two := big.NewInt(2)
if z.Rem(m, two).Cmp(zero) == 0 {
m.Add(m, one)
} else {
m.Add(m, two)
}
for {
if m.ProbablyPrime(0) {
return m
}
m.Add(m, two)
}
}
func main() {
extremes := []int{2}
sum := big.NewInt(2)
count := 1
p := big.NewInt(3)
for {
sum.Add(sum, p)
if sum.ProbablyPrime(0) {
count++
if count <= 30 {
extremes = append(extremes, int(sum.Uint64()))
}
if count == 30 {
fmt.Println("The first 30 extreme primes are:")
rcu.PrintTable(extremes, 6, 7, true)
fmt.Println()
} else if count%1000 == 0 {
m := count / 1000
if m < 6 || m == 30 || m == 40 || m == 50 {
scount := rcu.Commatize(count)
ssum := rcu.Commatize(sum.Uint64())
sp := rcu.Commatize(p.Uint64())
fmt.Printf("The %6sth extreme prime is: %18s for p <= %10s\n", scount, ssum, sp)
if m == 50 {
return
}
}
}
}
p.Set(nextPrime(p))
}
}
- Output:
Identical to Wren output.
J
3 10$(#~ 1&p:)+/\p:i.4e4
2 5 17 41 197 281 7699 8893 22039 24133
25237 28697 32353 37561 38921 43201 44683 55837 61027 66463
70241 86453 102001 109147 116533 119069 121631 129419 132059 263171
This would be more efficient if we had used 3e3 (216 extreme primes) rather than 4e4 (1942 extreme primes)
jq
Adapted from Wren
Works with both jq and gojq, the C and Go implementations of jq ...
Using the generic utilities given immediately below, one solution to the problem of generating the first 30 extreme primes could be written as follows:
def primes:
2 | recurse(nextprime);
def extremes:
foreach primes as $p (0; . + $p)
| select(is_prime);
limit(30; extremes)
A program that solves both the primary and stretch tasks is given below.
Generic Utilities
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
def nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
def is_prime:
. as $n
| if ($n < 2) then false
elif ($n % 2 == 0) then $n == 2
elif ($n % 3 == 0) then $n == 3
elif ($n % 5 == 0) then $n == 5
elif ($n % 7 == 0) then $n == 7
elif ($n % 11 == 0) then $n == 11
elif ($n % 13 == 0) then $n == 13
elif ($n % 17 == 0) then $n == 17
elif ($n % 19 == 0) then $n == 19
else
($n | sqrt) as $rt
| 23
| until( . > $rt or ($n % . == 0); .+2)
| . > $rt
end;
def nextprime:
(if .%2==0 then 1 else 2 end) as $n
| first(range(.+$n; infinite;2) | select(is_prime));
The Tasks
{ extremes:[2],
sum:2,
p:3,
count:1 }
| while (.count <= 5000;
.emit = null
| .sum += .p
| if .sum|is_prime
then .count += 1
| if .count <= 30
then .extremes += [.sum]
| if .count == 30
then .emit = "The first 30 extreme primes are:\n"
| .emit += ([.extremes | nwise(10) | map(lpad(7)) | join(" ") ] | join("\n"))
| .emit += "\n"
else .
end
elif .count % 1000 == 0
then .emit = "The \(.count)th extreme prime is: \(.sum) for p <= \(.p)"
else .
end
else .
end
| .p |= nextprime
)
| .emit // empty
- Output:
The first 30 extreme primes are: 2 5 17 41 197 281 7699 8893 22039 24133 25237 28697 32353 37561 38921 43201 44683 55837 61027 66463 70241 86453 102001 109147 116533 119069 121631 129419 132059 263171 The 1000th extreme prime is: 1657620079 for p <= 196831 The 2000th extreme prime is: 9744982591 for p <= 495571 The 3000th extreme prime is: 24984473177 for p <= 808837 The 4000th extreme prime is: 49394034691 for p <= 1152763 The 5000th extreme prime is: 82195983953 for p <= 1500973
Julia
julia> using Primes
julia> n = 0
0
julia> for p in primes(2000) n += p; isprime(n) && println(n); end
2
5
17
41
197
281
7699
8893
22039
24133
25237
28697
32353
37561
38921
43201
44683
55837
61027
66463
70241
86453
102001
109147
116533
119069
121631
129419
132059
263171
Stretch task
using Primes
let
ecount, p, n = 0, 0, 0
while ecount < 50_000
p = nextprime(p + 1)
n += p
if isprime(n)
ecount += 1
if ecount < 31
println("Sum of prime series up to $p: prime $n")
elseif ecount in [1000, 2000, 3000, 4000, 5000, 30_000, 40_000, 50_000]
println("Sum of $ecount in prime series up to $p: prime $n")
end
end
end
end
- Output:
Sum of prime series up to 2: prime 2 Sum of prime series up to 3: prime 5 Sum of prime series up to 7: prime 17 Sum of prime series up to 13: prime 41 Sum of prime series up to 37: prime 197 Sum of prime series up to 43: prime 281 Sum of prime series up to 281: prime 7699 Sum of prime series up to 311: prime 8893 Sum of prime series up to 503: prime 22039 Sum of prime series up to 541: prime 24133 Sum of prime series up to 557: prime 25237 Sum of prime series up to 593: prime 28697 Sum of prime series up to 619: prime 32353 Sum of prime series up to 673: prime 37561 Sum of prime series up to 683: prime 38921 Sum of prime series up to 733: prime 43201 Sum of prime series up to 743: prime 44683 Sum of prime series up to 839: prime 55837 Sum of prime series up to 881: prime 61027 Sum of prime series up to 929: prime 66463 Sum of prime series up to 953: prime 70241 Sum of prime series up to 1061: prime 86453 Sum of prime series up to 1163: prime 102001 Sum of prime series up to 1213: prime 109147 Sum of prime series up to 1249: prime 116533 Sum of prime series up to 1277: prime 119069 Sum of prime series up to 1283: prime 121631 Sum of prime series up to 1307: prime 129419 Sum of prime series up to 1321: prime 132059 Sum of prime series up to 1949: prime 263171 Sum of 1000 in prime series up to 196831: prime 1657620079 Sum of 2000 in prime series up to 495571: prime 9744982591 Sum of 3000 in prime series up to 808837: prime 24984473177 Sum of 4000 in prime series up to 1152763: prime 49394034691 Sum of 5000 in prime series up to 1500973: prime 82195983953 Sum of 30000 in prime series up to 12437401: prime 4889328757567 Sum of 40000 in prime series up to 17245391: prime 9207632380589 Sum of 50000 in prime series up to 22272277: prime 15118097491121
Nim
We use third party package “integers” to get Miller-Rabin primality test.
import std/[strformat, strutils]
import integers
echo "First 30 extreme primes:"
var ep = newInteger(0)
var count = 0
var lim = 1000
var p = newInteger(2)
while true:
ep += p
p = p.nextPrime
if ep.isPrime:
inc count
if count <= 30:
stdout.write &"{ep:>6}"
stdout.write if count mod 6 == 0: '\n' else: ' '
if count == 30: echo()
elif count == lim:
echo &"Sum of {count} in prime series up to {insertSep($p):>9}: prime {insertSep($ep):>14}"
inc lim, 1000
if lim > 5000: break
- Output:
First 30 extreme primes: 2 5 17 41 197 281 7699 8893 22039 24133 25237 28697 32353 37561 38921 43201 44683 55837 61027 66463 70241 86453 102001 109147 116533 119069 121631 129419 132059 263171 Sum of 1000 in prime series up to 196_837: prime 1_657_620_079 Sum of 2000 in prime series up to 495_587: prime 9_744_982_591 Sum of 3000 in prime series up to 808_853: prime 24_984_473_177 Sum of 4000 in prime series up to 1_152_773: prime 49_394_034_691 Sum of 5000 in prime series up to 1_500_991: prime 82_195_983_953
Phix
with javascript_semantics constant lim = 30 sequence extremes = {} integer ep = 0, np = 0 while length(extremes)<lim do np += 1; ep += get_prime(np) if is_prime(ep) then extremes &= ep end if end while printf(1,"The first %d extreme primes are:\n%s\n",{lim,join_by(extremes,1,6,fmt:="%,7d")}) include mpfr.e mpz z = mpz_init(ep) integer found = 30, p for tgt in {1e3,2e3,3e3,4e3,5e3,3e4,4e4,5e4} do while found<tgt do np += 2; p = get_prime(np) mpz_add_ui(z,z,get_prime(np-1)+p) if mpz_prime(z) then found += 1 end if end while string zs = mpz_get_str(z,10,true) printf(1,"The %,dth extreme prime is %s (primes[1..%,d]<=%,d)\n",{tgt,zs,np,p}) end for
- Output:
The first 30 extreme primes are: 2 5 17 41 197 281 7,699 8,893 22,039 24,133 25,237 28,697 32,353 37,561 38,921 43,201 44,683 55,837 61,027 66,463 70,241 86,453 102,001 109,147 116,533 119,069 121,631 129,419 132,059 263,171 The 1,000th extreme prime is 1,657,620,079 (primes[1..17,722]<=196,831) The 2,000th extreme prime is 9,744,982,591 (primes[1..41,198]<=495,571) The 3,000th extreme prime is 24,984,473,177 (primes[1..64,596]<=808,837) The 4,000th extreme prime is 49,394,034,691 (primes[1..89,504]<=1,152,763) The 5,000th extreme prime is 82,195,983,953 (primes[1..114,236]<=1,500,973) The 30,000th extreme prime is 4,889,328,757,567 (primes[1..814,900]<=12,437,401) The 40,000th extreme prime is 9,207,632,380,589 (primes[1..1,106,004]<=17,245,391) The 50,000th extreme prime is 15,118,097,491,121 (primes[1..1,405,244]<=22,272,277)
Python
""" rosettacode.org/wiki/Extreme_primes """
from sympy import isprime, nextprime
ecount, p, n = 0, 0, 0
while ecount < 50_000:
p = nextprime(p)
n += p
if isprime(n):
ecount += 1
if ecount < 31:
print(f'Sum of prime series up to {p}: prime {n}')
if ecount in [1000, 2000, 3000, 4000, 5000, 30_000, 40_000, 50_000]:
print(
f'Sum of {ecount :,} in prime series up to {p :,}: prime {n :,}')
- Output:
Sum of prime series up to 2: prime 2 Sum of prime series up to 3: prime 5 Sum of prime series up to 7: prime 17 Sum of prime series up to 13: prime 41 Sum of prime series up to 37: prime 197 Sum of prime series up to 43: prime 281 Sum of prime series up to 281: prime 7699 Sum of prime series up to 311: prime 8893 Sum of prime series up to 503: prime 22039 Sum of prime series up to 541: prime 24133 Sum of prime series up to 557: prime 25237 Sum of prime series up to 593: prime 28697 Sum of prime series up to 619: prime 32353 Sum of prime series up to 673: prime 37561 Sum of prime series up to 683: prime 38921 Sum of prime series up to 733: prime 43201 Sum of prime series up to 743: prime 44683 Sum of prime series up to 839: prime 55837 Sum of prime series up to 881: prime 61027 Sum of prime series up to 929: prime 66463 Sum of prime series up to 953: prime 70241 Sum of prime series up to 1061: prime 86453 Sum of prime series up to 1163: prime 102001 Sum of prime series up to 1213: prime 109147 Sum of prime series up to 1249: prime 116533 Sum of prime series up to 1277: prime 119069 Sum of prime series up to 1283: prime 121631 Sum of prime series up to 1307: prime 129419 Sum of prime series up to 1321: prime 132059 Sum of prime series up to 1949: prime 263171 Sum of 1,000 in prime series up to 196,831: prime 1,657,620,079 Sum of 2,000 in prime series up to 495,571: prime 9,744,982,591 Sum of 3,000 in prime series up to 808,837: prime 24,984,473,177 Sum of 4,000 in prime series up to 1,152,763: prime 49,394,034,691 Sum of 5,000 in prime series up to 1,500,973: prime 82,195,983,953 Sum of 30,000 in prime series up to 12,437,401: prime 4,889,328,757,567 Sum of 40,000 in prime series up to 17,245,391: prime 9,207,632,380,589 Sum of 50,000 in prime series up to 22,272,277: prime 15,118,097,491,121
Raku
So we're just gonna keep doing the same task over and over with slightly different names?
Primes equal to the sum of the first k primes for some k.
use Lingua::EN::Numbers;
say $_».&comma».fmt("%7s").batch(10).join: "\n" for
(([\+] (^∞).grep: &is-prime).grep: &is-prime)[^30,999,1999,2999,3999,4999];
- Output:
2 5 17 41 197 281 7,699 8,893 22,039 24,133 25,237 28,697 32,353 37,561 38,921 43,201 44,683 55,837 61,027 66,463 70,241 86,453 102,001 109,147 116,533 119,069 121,631 129,419 132,059 263,171 1,657,620,079 9,744,982,591 24,984,473,177 49,394,034,691 82,195,983,953
Ring
see "working..." + nl
limit = 2000
Primes = []
for n = 1 to limit
if isPrime(n)
add(Primes,n)
ok
next
sum = 0
row = 0
for n = 1 to len(Primes)
sum = sum + Primes[n]
if isPrime(sum)
row++
see "" + sum + " "
if row % 10 = 0
see nl
ok
ok
next
see "done..." + nl
func isPrime num
if (num <= 1) return 0 ok
if (num % 2 = 0 and num != 2) return 0 ok
for i = 3 to floor(num / 2) -1 step 2
if (num % i = 0) return 0 ok
next
return 1
- Output:
working... 2 5 17 41 197 281 7699 8893 22039 24133 25237 28697 32353 37561 38921 43201 44683 55837 61027 66463 70241 86453 102001 109147 116533 119069 121631 129419 132059 263171 done...
RPL
« 0 → max sum « { } 1 DO NEXTPRIME DUP 'sum' STO+ IF sum ISPRIME? THEN SWAP sum + SWAP END UNTIL OVER SIZE max ≥ END DROP2 » » ‘XPRIM’ STO 30 XPRIM
- Output:
1: { 2 5 17 41 197 281 7699 8893 22039 24133 25237 28697 32353 37561 38921 43201 44683 55837 61027 66463 70241 86453 102001 109147 116533 119069 121631 129419 132059 263171 }
Ruby
require 'prime'
sum, n = 0, 30
extreme_primes = Prime.lazy.filter_map{|pr|sum += pr; [pr, sum] if sum.prime?}
puts "The first #{n} extreme primes are:"
extreme_primes.first(30).map(&:last).each_slice(10){|slice| puts "%8d"*slice.size % slice}
sum = 0
puts
extreme_primes.first(5000).each_slice(1000).with_index(1) do|slice, i|
puts "The %dth extreme prime is sum of primes upto %10d: %12d" % [i*1000, *slice.last]
end
- Output:
The first 30 extreme primes are: 2 5 17 41 197 281 7699 8893 22039 24133 25237 28697 32353 37561 38921 43201 44683 55837 61027 66463 70241 86453 102001 109147 116533 119069 121631 129419 132059 263171 The 1000th extreme prime is sum of primes upto 196831: 1657620079 The 2000th extreme prime is sum of primes upto 495571: 9744982591 The 3000th extreme prime is sum of primes upto 808837: 24984473177 The 4000th extreme prime is sum of primes upto 1152763: 49394034691 The 5000th extreme prime is sum of primes upto 1500973: 82195983953
Sidef
var(c,p,n) = (0,0,0)
while (c < 50_000) {
p.next_prime!
n += p
if (n.is_prime) {
++c
if (c <= 30) {
say "Sum of prime series up to #{p}: prime #{n}"
}
elsif (c ~~ [1000, 2000, 3000, 4000, 5000, 30_000, 40_000, 50_000]) {
say ("Sum of #{c.commify} in prime series up to #{p}: prime #{n}")
}
}
}
- Output:
Sum of prime series up to 2: prime 2 Sum of prime series up to 3: prime 5 Sum of prime series up to 7: prime 17 Sum of prime series up to 13: prime 41 Sum of prime series up to 37: prime 197 Sum of prime series up to 43: prime 281 Sum of prime series up to 281: prime 7699 Sum of prime series up to 311: prime 8893 Sum of prime series up to 503: prime 22039 Sum of prime series up to 541: prime 24133 Sum of prime series up to 557: prime 25237 Sum of prime series up to 593: prime 28697 Sum of prime series up to 619: prime 32353 Sum of prime series up to 673: prime 37561 Sum of prime series up to 683: prime 38921 Sum of prime series up to 733: prime 43201 Sum of prime series up to 743: prime 44683 Sum of prime series up to 839: prime 55837 Sum of prime series up to 881: prime 61027 Sum of prime series up to 929: prime 66463 Sum of prime series up to 953: prime 70241 Sum of prime series up to 1061: prime 86453 Sum of prime series up to 1163: prime 102001 Sum of prime series up to 1213: prime 109147 Sum of prime series up to 1249: prime 116533 Sum of prime series up to 1277: prime 119069 Sum of prime series up to 1283: prime 121631 Sum of prime series up to 1307: prime 129419 Sum of prime series up to 1321: prime 132059 Sum of prime series up to 1949: prime 263171 Sum of 1,000 in prime series up to 196831: prime 1657620079 Sum of 2,000 in prime series up to 495571: prime 9744982591 Sum of 3,000 in prime series up to 808837: prime 24984473177 Sum of 4,000 in prime series up to 1152763: prime 49394034691 Sum of 5,000 in prime series up to 1500973: prime 82195983953 Sum of 30,000 in prime series up to 12437401: prime 4889328757567 Sum of 40,000 in prime series up to 17245391: prime 9207632380589 Sum of 50,000 in prime series up to 22272277: prime 15118097491121
Wren
Wren-CLI
import "./math" for Int
import "./fmt" for Fmt
var extremes = [2]
var sum = 2
var p = 3
var count = 1
while (true) {
sum = sum + p
if (Int.isPrime(sum)) {
count = count + 1
if (count <= 30) {
extremes.add(sum)
}
if (count == 30) {
System.print("The first 30 extreme primes are:")
Fmt.tprint("$,7d ", extremes, 6)
System.print()
} else if (count % 1000 == 0) {
Fmt.print("The $,r extreme prime is: $,14d for p <= $,9d", count, sum, p)
if (count == 5000) return
}
}
p = Int.nextPrime(p)
}
- Output:
The first 30 extreme primes are: 2 5 17 41 197 281 7,699 8,893 22,039 24,133 25,237 28,697 32,353 37,561 38,921 43,201 44,683 55,837 61,027 66,463 70,241 86,453 102,001 109,147 116,533 119,069 121,631 129,419 132,059 263,171 The 1,000th extreme prime is: 1,657,620,079 for p <= 196,831 The 2,000th extreme prime is: 9,744,982,591 for p <= 495,571 The 3,000th extreme prime is: 24,984,473,177 for p <= 808,837 The 4,000th extreme prime is: 49,394,034,691 for p <= 1,152,763 The 5,000th extreme prime is: 82,195,983,953 for p <= 1,500,973
Embedded (GMP)
import "./gmp" for Mpz
import "./fmt" for Fmt
var extremes = [2]
var sum = Mpz.two
var count = 1
var p = Mpz.three
while (true) {
sum.add(p)
if (sum.probPrime(5) > 0) {
count = count + 1
if (count <= 30) {
extremes.add(sum.toNum)
}
if (count == 30) {
System.print("The first 30 extreme primes are:")
Fmt.tprint("$,7i ", extremes, 6)
System.print()
} else if (count % 1000 == 0) {
var m = count / 1000
if (m < 6 || m == 30 || m == 40 || m == 50) {
Fmt.print("The $,8r extreme prime is: $,18i for p <= $,10i", count, sum, p)
if (m == 50) return
}
}
}
p.nextPrime
}
- Output:
The first 30 extreme primes are: 2 5 17 41 197 281 7,699 8,893 22,039 24,133 25,237 28,697 32,353 37,561 38,921 43,201 44,683 55,837 61,027 66,463 70,241 86,453 102,001 109,147 116,533 119,069 121,631 129,419 132,059 263,171 The 1,000th extreme prime is: 1,657,620,079 for p <= 196,831 The 2,000th extreme prime is: 9,744,982,591 for p <= 495,571 The 3,000th extreme prime is: 24,984,473,177 for p <= 808,837 The 4,000th extreme prime is: 49,394,034,691 for p <= 1,152,763 The 5,000th extreme prime is: 82,195,983,953 for p <= 1,500,973 The 30,000th extreme prime is: 4,889,328,757,567 for p <= 12,437,401 The 40,000th extreme prime is: 9,207,632,380,589 for p <= 17,245,391 The 50,000th extreme prime is: 15,118,097,491,121 for p <= 22,272,277
XPL0
include xpllib; \for IsPrime and RlOutC
int C, N, S;
[Text(0, "The first 30 extreme primes are:^m^j");
Format(7,0);
C:= 0; N:= 2; S:= 0;
loop [if IsPrime(N) then
[S:= S+N;
if IsPrime(S) then
[C:= C+1;
RlOutC(0, float(S));
if rem(C/6) = 0 then CrLf(0);
if C >= 30 then quit;
];
];
N:= N+1;
];
]
- Output:
The first 30 extreme primes are: 2 5 17 41 197 281 7,699 8,893 22,039 24,133 25,237 28,697 32,353 37,561 38,921 43,201 44,683 55,837 61,027 66,463 70,241 86,453 102,001 109,147 116,533 119,069 121,631 129,419 132,059 263,171