Extreme primes
- 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
Ada
-- Rosetta Code Task written in Ada
-- Extreme primes
-- https://rosettacode.org/wiki/Extreme_primes
-- Inspired by virtually all of the other solutions, not any particular one
-- Output similar to the Perl solution
-- Uses GNAT Big_Integers
-- Shows elapsed time
-- Tested with GNAT 14.1.0, MacOS 14.6.1, M1 chip
-- September 2024, R. B. E.
pragma Ada_2022;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Numerics.Big_Numbers.Big_Integers; use Ada.Numerics.Big_Numbers.Big_Integers;
with Ada.Real_Time; use Ada.Real_Time;
procedure Extreme_Primes is
function Is_Prime (N : Big_Positive) return Boolean is
Big0 : constant Big_Natural := To_Big_Integer (0);
Big2 : constant Big_Positive := To_Big_Integer (2);
Big3 : constant Big_Positive := To_Big_Integer (3);
Big4 : constant Big_Positive := To_Big_Integer (4);
D : Big_Positive := To_Big_Integer (5);
begin
if N < Big2 then
return False;
end if;
if (N = Big2) or (N = Big3) then
return True;
end if;
if N mod Big2 = Big0 then
return False;
end if;
if N mod Big3 = Big0 then
return False;
end if;
while D * D <= N loop
if N mod D = Big0 then
return False;
end if;
D := D + Big2;
if N mod D = Big0 then
return False;
end if;
D := D + Big4;
end loop;
return True;
end Is_Prime;
function Determine_the_Next_Prime (N : Big_Positive) return Big_Positive is
Big1 : constant Big_Natural := To_Big_Integer (1);
M : Big_Positive := N + 1;
begin
while not Is_Prime (M) loop
M := M + Big1;
end loop;
return M;
end Determine_the_Next_Prime;
procedure Display (S : Positive; Last_Prime_Used, EP : Big_Positive) is
begin
Put (S, 7);
Put (To_String (Arg => Last_Prime_Used, Width => 14));
Put (To_String (Arg => EP, Width => 16));
New_Line;
end Display;
Big0 : constant Big_Positive := To_Big_Integer (0);
Big2 : constant Big_Positive := To_Big_Integer (2);
Sequence_Number : Natural := 1;
Previous_Prime_Number : Big_Positive := Big2;
Sum : Big_Positive := Big2;
Start_Time, Stop_Time : Time;
Elapsed_Time : Time_Span;
begin
Start_Time := Clock;
Sum := Big0;
Previous_Prime_Number := Big2;
Sum := Sum + Previous_Prime_Number;
New_Line;
Put_Line ("Sum of the prime series...");
Put_Line ("Sequence # Largest Prime Sum");
Display (Sequence_Number, Previous_Prime_Number, Sum);
loop
Previous_Prime_Number := Determine_the_Next_Prime (Previous_Prime_Number);
Sum := Sum + Previous_Prime_Number;
if Is_Prime (Sum) then
Sequence_Number := Sequence_Number + 1;
case Sequence_Number is
when 2..30 => Display (Sequence_Number, Previous_Prime_Number, Sum);
when 1_000 | 2_000 | 3_000 | 4_000 | 5_000 => Display (Sequence_Number, Previous_Prime_Number, Sum);
when 7_500 | 10_000 => Display (Sequence_Number, Previous_Prime_Number, Sum);
when others => null;
end case;
end if;
exit when Sequence_Number = 10_000;
end loop;
New_Line;
Stop_Time := Clock;
Elapsed_Time := Stop_Time - Start_Time;
Put_Line ("Elapsed time: " & Duration'Image (To_Duration (Elapsed_Time)) & " seconds");
New_Line;
end Extreme_Primes;
- Output:
Sum of the prime series... Sequence # Largest Prime Sum 1 2 2 2 3 5 3 7 17 4 13 41 5 37 197 6 43 281 7 281 7699 8 311 8893 9 503 22039 10 541 24133 11 557 25237 12 593 28697 13 619 32353 14 673 37561 15 683 38921 16 733 43201 17 743 44683 18 839 55837 19 881 61027 20 929 66463 21 953 70241 22 1061 86453 23 1163 102001 24 1213 109147 25 1249 116533 26 1277 119069 27 1283 121631 28 1307 129419 29 1321 132059 30 1949 263171 1000 196831 1657620079 2000 495571 9744982591 3000 808837 24984473177 4000 1152763 49394034691 5000 1500973 82195983953 7500 2429311 207901544003 10000 3420293 402638678093 Elapsed time: 229.718635000 seconds
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.
C++
#include <algorithm>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <vector>
std::vector<uint32_t> primes{ };
void fill_primes(const uint64_t& limit) {
primes.emplace_back(2);
const uint32_t half_limit = ( limit + 1 ) / 2;
std::vector<bool> composite(half_limit, false);
for ( uint32_t i = 1, p = 3; i < half_limit; p += 2, ++i ) {
if ( ! composite[i] ) {
primes.emplace_back(p);
for ( uint32_t a = i + p; a < half_limit; a += p ) {
composite[a] = true;
}
}
}
}
bool is_prime(const uint32_t& candidate) {
return std::binary_search(primes.begin(), primes.end(), candidate);
}
uint32_t next_prime(const uint32_t& prime) {
return *++std::find(primes.begin(), primes.end(), prime);
}
int main() {
fill_primes(300'000);
std::vector<uint32_t> extreme_primes = { 2 };
uint32_t sum = 2;
uint32_t prime = 3;
uint32_t count = 1;
while ( count < 30 ) {
sum += prime;
if ( is_prime(sum) ) {
count += 1;
extreme_primes.emplace_back(sum);
}
prime = next_prime(prime);
}
std::cout << "The first 30 extreme primes are:" << std::endl;
for ( uint32_t i = 0; i < extreme_primes.size(); ++i ) {
std::cout << std::setw(7) << extreme_primes[i] << ( i % 6 == 5 ? "\n" : " " );
}
std::cout << std::endl;
}
- 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
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)
Java
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public final class ExtremePrimes {
public static void main(String[] args) {
List<Long> extremePrimes = Stream.of( 2L ).collect(Collectors.toList());
long sum = 2;
int prime = 3;
int count = 1;
while ( count < 5_000 ) {
sum += prime;
if ( isPrime(sum) ) {
count += 1;
if ( count <= 30 ) {
extremePrimes.addLast(sum);
}
if ( count == 30 ) {
System.out.println("The first 30 extreme primes are:");
for ( int i = 0; i < extremePrimes.size(); i++ ) {
System.out.print(String.format("%7d%s",
extremePrimes.get(i), ( i % 6 == 5 ? "\n" : " " )));
}
System.out.println();
} else if ( count % 1_000 == 0 ) {
System.out.println(String.format("%s%12d%s%8d",
"The " + count + "th extreme prime is", sum, " for prime <=", prime));
}
}
prime = nextPrime(prime);
}
}
private static boolean isPrime(long candidiate) {
return BigInteger.valueOf(candidiate).isProbablePrime(15);
}
private static int nextPrime(int prime) {
return BigInteger.valueOf(prime).nextProbablePrime().intValueExact();
}
}
- 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 prime <= 196831 The 2000th extreme prime is 9744982591 for prime <= 495571 The 3000th extreme prime is 24984473177 for prime <= 808837 The 4000th extreme prime is 49394034691 for prime <= 1152763 The 5000th extreme prime is 82195983953 for prime <= 1500973
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
Perl
# 20240910 Perl programming solution
use strict;
use warnings;
use Math::Prime::Util qw(is_prime next_prime);
for ( my ($ecount,$p,$n) = (0,0,0); $ecount < 50_000; ) {
if ( is_prime($n += $p = next_prime($p)) ) {
$ecount++;
if ($ecount < 31) { print "Sum of prime series up to $p: prime $n\n" }
if ($ecount =~ /^(1000|2000|3000|4000|5000|30000|40000|50000)$/) {
print "Sum of $ecount in prime series up to $p: prime $n\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 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
Quackery
isprime
is defined at Primality by trial division#Quackery.
2 1 ' [ 2 ]
[ unrot dup isprime if
[ tuck + swap unrot
dup isprime if
[ dup dip join ]
rot ]
2 +
rot dup size 30 = until ]
unrot 2drop
echo
- 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 ]
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
You may Attempt This Online!
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
Scala
import scala.math.BigInt
object ExtremePrimesApp extends App {
private def isPrime(num: BigInt): Boolean =
require(num >= 0)
num.isProbablePrime(certainty = 100)
private def calcExtremePrimes(max: Int) =
require(max > 0)
LazyList
.iterate(BigInt(1))(_ + 1)
.filter(isPrime)
.scanLeft((BigInt(0), BigInt(0))) { case ((_, sum), prime) =>
(prime, sum + prime)
}
.tail // Skip the initial (0, 0)
.filter { case (_, sum) => isPrime(sum) }
.take(max)
@main def main(): Unit =
val firstN = 30
val milestones = List(1_000, 2_000, 3_000, 4_000, 5_000, 10_000, 30_000, 40_000, 50_000)
val extremePrimes = calcExtremePrimes(milestones.max).toList
def printFirstNExtremePrimes(n: Int): Unit =
println(s"The first $n extreme primes are:")
extremePrimes
.take(n)
.map(_._2)
.grouped(10)
.foreach { group =>
println(group.map("%8d".format(_)).mkString)
}
def printMilestoneExtremePrimes(milestones: List[Int]): Unit =
println()
milestones.foreach { milestone =>
val (prime, sum) = extremePrimes.drop(milestone - 1).head
println(
f"The $milestone%5dth extreme prime is sum of primes upto $prime%10d: $sum%14d"
)
}
def test(): Unit =
printFirstNExtremePrimes(firstN)
printMilestoneExtremePrimes(milestones)
test()
main()
}
- 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 The 10000th extreme prime is sum of primes upto 3420293: 402638678093 The 30000th extreme prime is sum of primes upto 12437401: 4889328757567 The 40000th extreme prime is sum of primes upto 17245391: 9207632380589 The 50000th extreme prime is sum of primes upto 22272277: 15118097491121
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