Extreme primes: Difference between revisions

Added Easylang
(Added C)
(Added Easylang)
 
(11 intermediate revisions by 8 users not shown)
Line 42:
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;
INTLONG INT candidate 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 +:= 12 ) 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;
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 )
Line 138 ⟶ 130:
<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>
 
Line 181 ⟶ 206:
"rcu"
)
 
func commatizeU64(n uint64) string {
s := fmt.Sprintf("%d", n)
if n < 0 {
s = s[1:]
}
le := len(s)
for i := le - 3; i >= 1; i -= 3 {
s = s[0:i] + "," + s[i:]
}
if n >= 0 {
return s
}
return "-" + s
}
 
func nextPrime(n *big.Int) *big.Int {
Line 236 ⟶ 246:
if m < 6 || m == 30 || m == 40 || m == 50 {
scount := rcu.Commatize(count)
ssum := commatizeU64rcu.Commatize(sum.Uint64())
sp := commatizeU64rcu.Commatize(p.Uint64())
fmt.Printf("The %6sth extreme prime is: %18s for p <= %10s\n", scount, ssum, sp)
if m == 50 {
Line 263 ⟶ 273:
 
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}}==
Line 364 ⟶ 467:
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>
 
Line 528 ⟶ 673:
done...
</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}}==
Line 533 ⟶ 779:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
Line 577 ⟶ 823:
===Embedded (GMP)===
{{libheader|Wren-gmp}}
<syntaxhighlight lang="ecmascriptwren">import "./gmp" for Mpz
import "./fmt" for Fmt
 
1,981

edits