Extreme primes
Extreme primes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
- 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" #
LONG INT prime sum := 2;
INT prime sum count := 1;
LONG INT last prime := 0;
FOR i FROM 3 BY 2 TO UPB prime WHILE prime sum count < 30 DO
IF is prime( i ) THEN
prime sum +:= i; # have another prime #
last prime := i;
IF is prime( prime sum ) THEN # the prime sum is also prime #
print( ( whole( prime sum, -6 )
, IF ( prime sum count +:= 1 ) MOD 10 = 0 THEN newline ELSE " " FI
)
)
FI
FI
OD;
print( ( newline ) );
LONG INT candidate := last prime;
WHILE prime sum count < 5 000 DO
IF is prime( candidate +:= 1 ) THEN
prime sum +:= candidate; # have another prime #
IF is prime( prime sum ) THEN # the prime sum is also prime #
IF ( prime sum count +:= 1 ) 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
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.
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)
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
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...
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