Extreme primes: Difference between revisions
Line 100: | Line 100: | ||
<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;">1</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;">1</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: # |
<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: #000000;">np</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> |
||
<span style="color: #000000;">ep</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">p</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: #000000;">np</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</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> |
Revision as of 18:20, 24 April 2023
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 p extreme prime number on this page.
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(2300) 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
287137
325019
329401
333821
338279
342761
360979
Phix
with javascript_semantics constant lim = 30 -- (output matches Julia if changed to 37) sequence extremes = {} integer ep = 0, np = 1 while length(extremes)<lim do ep += get_prime(np); np += 1 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")})
- 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
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
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 though it doesn't appear to have a recognized name. --- Based on the above reasons, please delete this task. Thanks in advance. --- CalmoSoft
import "./math" for Int
import "./fmt" for Fmt
var primes = Int.primeSieve(2000) // say
var extremes = [2]
var sum = 2
for (p in primes.skip(1)) {
sum = sum + p
if (Int.isPrime(sum)) {
extremes.add(sum)
if (extremes.count == 30) break
}
}
System.print("The first 30 extreme primes are:")
Fmt.tprint("$,7d ", extremes, 6)
- 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
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