Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
2
Task/Summarize-primes/00-META.yaml
Normal file
2
Task/Summarize-primes/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Summarize_primes
|
||||
8
Task/Summarize-primes/00-TASK.txt
Normal file
8
Task/Summarize-primes/00-TASK.txt
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
;Task:
|
||||
|
||||
Considering in order of length, n, all sequences of consecutive
|
||||
primes, p, from 2 onwards, where p < 1000 and n>0, select those
|
||||
sequences whose sum is prime, and for these display the length of the
|
||||
sequence, the last item in the sequence, and the sum.
|
||||
<br><br>
|
||||
|
||||
19
Task/Summarize-primes/11l/summarize-primes.11l
Normal file
19
Task/Summarize-primes/11l/summarize-primes.11l
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
F is_prime(a)
|
||||
I a == 2
|
||||
R 1B
|
||||
I a < 2 | a % 2 == 0
|
||||
R 0B
|
||||
L(i) (3 .. Int(sqrt(a))).step(2)
|
||||
I a % i == 0
|
||||
R 0B
|
||||
R 1B
|
||||
|
||||
print(‘index prime prime sum’)
|
||||
V s = 0
|
||||
V idx = 0
|
||||
L(n) 2..999
|
||||
I is_prime(n)
|
||||
idx++
|
||||
s += n
|
||||
I is_prime(s)
|
||||
print(f:‘{idx:3} {n:5} {s:7}’)
|
||||
43
Task/Summarize-primes/ALGOL-68/summarize-primes.alg
Normal file
43
Task/Summarize-primes/ALGOL-68/summarize-primes.alg
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
BEGIN # sum the primes below n and report the sums that are prime #
|
||||
# sieve the primes to 999 #
|
||||
PR read "primes.incl.a68" PR
|
||||
[]BOOL prime = PRIMESIEVE 999;
|
||||
# sum the primes and test the sum #
|
||||
INT prime sum := 0;
|
||||
INT prime count := 0;
|
||||
INT prime sum count := 0;
|
||||
print( ( "prime prime", newline ) );
|
||||
print( ( "count prime sum", newline ) );
|
||||
FOR i TO UPB prime DO
|
||||
IF prime[ i ] THEN
|
||||
# have another prime #
|
||||
prime count +:= 1;
|
||||
prime sum +:= i;
|
||||
# check whether the prime sum is prime or not #
|
||||
BOOL is prime := TRUE;
|
||||
FOR p TO i OVER 2 WHILE is prime DO
|
||||
IF prime[ p ] THEN is prime := prime sum MOD p /= 0 FI
|
||||
OD;
|
||||
IF is prime THEN
|
||||
# the prime sum is also prime #
|
||||
prime sum count +:= 1;
|
||||
print( ( whole( prime count, -5 )
|
||||
, " "
|
||||
, whole( i, -6 )
|
||||
, " "
|
||||
, whole( prime sum, -6 )
|
||||
, newline
|
||||
)
|
||||
)
|
||||
FI
|
||||
FI
|
||||
OD;
|
||||
print( ( newline
|
||||
, "Found "
|
||||
, whole( prime sum count, 0 )
|
||||
, " prime sums of primes below "
|
||||
, whole( UPB prime + 1, 0 )
|
||||
, newline
|
||||
)
|
||||
)
|
||||
END
|
||||
56
Task/Summarize-primes/ALGOL-W/summarize-primes.alg
Normal file
56
Task/Summarize-primes/ALGOL-W/summarize-primes.alg
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
begin % sum the primes below n and report the sums that are prime %
|
||||
integer MAX_NUMBER;
|
||||
MAX_NUMBER := 999;
|
||||
begin
|
||||
logical array prime( 1 :: MAX_NUMBER );
|
||||
integer primeCount, primeSum, primeSumCount;
|
||||
% sieve the primes to MAX_NUMBER %
|
||||
prime( 1 ) := false; prime( 2 ) := true;
|
||||
for i := 3 step 2 until MAX_NUMBER do prime( i ) := true;
|
||||
for i := 4 step 2 until MAX_NUMBER do prime( i ) := false;
|
||||
for i := 3 step 2 until truncate( sqrt( MAX_NUMBER ) ) do begin
|
||||
integer ii; ii := i + i;
|
||||
if prime( i ) then begin
|
||||
for p := i * i step ii until MAX_NUMBER do prime( p ) := false
|
||||
end if_prime_i
|
||||
end for_i ;
|
||||
% find the prime sums that are prime %
|
||||
primeCount := primeSum := primeSumCount := 0;
|
||||
write( "prime prime" );
|
||||
write( "count sum" );
|
||||
for i := 1 until MAX_NUMBER do begin
|
||||
if prime( i ) then begin
|
||||
% have another prime %
|
||||
logical isPrime;
|
||||
primeSum := primeSum + i;
|
||||
primeCount := primeCount + 1;
|
||||
% check whether the prime sum is also prime %
|
||||
isPrime := true;
|
||||
for p := 1 until i div 2 do begin
|
||||
if prime( p ) then begin
|
||||
isPrime := primeSum rem p not = 0;
|
||||
if not isPrime then goto endPrimeCheck
|
||||
end if_prime_p
|
||||
end for_p ;
|
||||
endPrimeCheck:
|
||||
if isPrime then begin
|
||||
% the prime sum is also prime %
|
||||
primeSumCount := primeSumCount + 1;
|
||||
write( i_w := 5, s_w := 0
|
||||
, primeCount
|
||||
, " "
|
||||
, i_w := 6
|
||||
, primeSum
|
||||
)
|
||||
end if_isPrime
|
||||
end if_prime_i
|
||||
end for_i ;
|
||||
write();
|
||||
write( i_w := 1, s_w := 0
|
||||
, "Found "
|
||||
, primeSumCount
|
||||
, " prime sums of primes below "
|
||||
, MAX_NUMBER + 1
|
||||
)
|
||||
end
|
||||
end.
|
||||
28
Task/Summarize-primes/AWK/summarize-primes.awk
Normal file
28
Task/Summarize-primes/AWK/summarize-primes.awk
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
# syntax: GAWK -f SUMMARIZE_PRIMES.AWK
|
||||
BEGIN {
|
||||
start = 1
|
||||
stop = 999
|
||||
for (i=start; i<=stop; i++) {
|
||||
if (is_prime(i)) {
|
||||
count1++
|
||||
sum += i
|
||||
if (is_prime(sum)) {
|
||||
printf("the sum of %3d primes from primes 2-%-3s is %5d which is also prime\n",count1,i,sum)
|
||||
count2++
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("Summarized primes %d-%d: %d\n",start,stop,count2)
|
||||
exit(0)
|
||||
}
|
||||
function is_prime(x, i) {
|
||||
if (x <= 1) {
|
||||
return(0)
|
||||
}
|
||||
for (i=2; i<=int(sqrt(x)); i++) {
|
||||
if (x % i == 0) {
|
||||
return(0)
|
||||
}
|
||||
}
|
||||
return(1)
|
||||
}
|
||||
17
Task/Summarize-primes/Arturo/summarize-primes.arturo
Normal file
17
Task/Summarize-primes/Arturo/summarize-primes.arturo
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
print (pad "index" 6) ++ " | " ++
|
||||
(pad "prime" 6) ++ " | " ++
|
||||
(pad "prime sum" 11)
|
||||
print "------------------------------"
|
||||
|
||||
s: 0
|
||||
idx: 0
|
||||
loop 2..999 'n [
|
||||
if prime? n [
|
||||
idx: idx + 1
|
||||
s: s + n
|
||||
if prime? s ->
|
||||
print (pad to :string idx 6) ++ " | " ++
|
||||
(pad to :string n 6) ++ " | " ++
|
||||
(pad to :string s 11)
|
||||
]
|
||||
]
|
||||
14
Task/Summarize-primes/BASIC256/summarize-primes.basic
Normal file
14
Task/Summarize-primes/BASIC256/summarize-primes.basic
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include "isprime.kbs"
|
||||
|
||||
print 1, 2, 2
|
||||
sum = 2
|
||||
n = 1
|
||||
for i = 3 to 999 step 2
|
||||
if isPrime(i) then
|
||||
sum += i
|
||||
n += 1
|
||||
if isPrime(sum) then
|
||||
print n, i, sum
|
||||
end if
|
||||
end if
|
||||
next i
|
||||
52
Task/Summarize-primes/C++/summarize-primes.cpp
Normal file
52
Task/Summarize-primes/C++/summarize-primes.cpp
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#include <iostream>
|
||||
|
||||
bool is_prime(int n) {
|
||||
if (n < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (n % 2 == 0) {
|
||||
return n == 2;
|
||||
}
|
||||
if (n % 3 == 0) {
|
||||
return n == 3;
|
||||
}
|
||||
|
||||
int i = 5;
|
||||
while (i * i <= n) {
|
||||
if (n % i == 0) {
|
||||
return false;
|
||||
}
|
||||
i += 2;
|
||||
|
||||
if (n % i == 0) {
|
||||
return false;
|
||||
}
|
||||
i += 4;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
const int start = 1;
|
||||
const int stop = 1000;
|
||||
|
||||
int sum = 0;
|
||||
int count = 0;
|
||||
int sc = 0;
|
||||
|
||||
for (int p = start; p < stop; p++) {
|
||||
if (is_prime(p)) {
|
||||
count++;
|
||||
sum += p;
|
||||
if (is_prime(sum)) {
|
||||
printf("The sum of %3d primes in [2, %3d] is %5d which is also prime\n", count, p, sum);
|
||||
sc++;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("There are %d summerized primes in [%d, %d)\n", sc, start, stop);
|
||||
|
||||
return 0;
|
||||
}
|
||||
55
Task/Summarize-primes/C/summarize-primes.c
Normal file
55
Task/Summarize-primes/C/summarize-primes.c
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
bool is_prime(int n) {
|
||||
int i = 5;
|
||||
|
||||
if (n < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (n % 2 == 0) {
|
||||
return n == 2;
|
||||
}
|
||||
if (n % 3 == 0) {
|
||||
return n == 3;
|
||||
}
|
||||
|
||||
while (i * i <= n) {
|
||||
if (n % i == 0) {
|
||||
return false;
|
||||
}
|
||||
i += 2;
|
||||
|
||||
if (n % i == 0) {
|
||||
return false;
|
||||
}
|
||||
i += 4;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
const int start = 1;
|
||||
const int stop = 1000;
|
||||
|
||||
int sum = 0;
|
||||
int count = 0;
|
||||
int sc = 0;
|
||||
int p;
|
||||
|
||||
for (p = start; p < stop; p++) {
|
||||
if (is_prime(p)) {
|
||||
count++;
|
||||
sum += p;
|
||||
if (is_prime(sum)) {
|
||||
printf("The sum of %3d primes in [2, %3d] is %5d which is also prime\n", count, p, sum);
|
||||
sc++;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("There are %d summerized primes in [%d, %d)\n", sc, start, stop);
|
||||
|
||||
return 0;
|
||||
}
|
||||
23
Task/Summarize-primes/Delphi/summarize-primes.delphi
Normal file
23
Task/Summarize-primes/Delphi/summarize-primes.delphi
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
procedure SumOfPrimeSequences(Memo: TMemo);
|
||||
var Sieve: TPrimeSieve;
|
||||
var I,Inx, Sum: integer;
|
||||
begin
|
||||
Sieve:=TPrimeSieve.Create;
|
||||
try
|
||||
Sieve.Intialize(100000);
|
||||
Memo.Lines.Add(' I P(I) Sum');
|
||||
Memo.Lines.Add('---------------');
|
||||
I:=0;
|
||||
Sum:=0;
|
||||
while Sieve.Primes[I]<1000 do
|
||||
begin
|
||||
Sum:=Sum+Sieve.Primes[I];
|
||||
if Sieve.Flags[Sum] then
|
||||
begin
|
||||
Memo.Lines.Add(Format('%3d %4d %6d',[I,Sieve.Primes[I],Sum]));
|
||||
end;
|
||||
Inc(I,1);
|
||||
end;
|
||||
|
||||
finally Sieve.Free; end;
|
||||
end;
|
||||
20
Task/Summarize-primes/EasyLang/summarize-primes.easy
Normal file
20
Task/Summarize-primes/EasyLang/summarize-primes.easy
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
proc prime x . r .
|
||||
for i = 2 to sqrt x
|
||||
if x mod i = 0
|
||||
r = 0
|
||||
break 2
|
||||
.
|
||||
.
|
||||
r = 1
|
||||
.
|
||||
for i = 2 to 999
|
||||
call prime i r
|
||||
if r = 1
|
||||
ind += 1
|
||||
sum += i
|
||||
call prime sum r
|
||||
if r = 1
|
||||
print ind & ": " & sum
|
||||
.
|
||||
.
|
||||
.
|
||||
2
Task/Summarize-primes/F-Sharp/summarize-primes.fs
Normal file
2
Task/Summarize-primes/F-Sharp/summarize-primes.fs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
// Summarize Primes: Nigel Galloway. April 16th., 2021
|
||||
primes32()|>Seq.takeWhile((>)1000)|>Seq.scan(fun(n,g) p->(n+1,g+p))(0,0)|>Seq.filter(snd>>isPrime)|>Seq.iter(fun(n,g)->printfn "%3d->%d" n g)
|
||||
6
Task/Summarize-primes/Factor/summarize-primes.factor
Normal file
6
Task/Summarize-primes/Factor/summarize-primes.factor
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
USING: assocs formatting kernel math.primes math.ranges
|
||||
math.statistics prettyprint ;
|
||||
|
||||
1000 [ [1,b] ] [ primes-upto cum-sum ] bi zip
|
||||
[ nip prime? ] assoc-filter
|
||||
[ "The sum of the first %3d primes is %5d (which is prime).\n" printf ] assoc-each
|
||||
3
Task/Summarize-primes/Fermat/summarize-primes.fermat
Normal file
3
Task/Summarize-primes/Fermat/summarize-primes.fermat
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
n:=0
|
||||
s:=0
|
||||
for i=1, 162 do s:=s+Prime(i);if Isprime(s)=1 then n:=n+1;!!(n,Prime(i),s) fi od
|
||||
30
Task/Summarize-primes/Forth/summarize-primes.fth
Normal file
30
Task/Summarize-primes/Forth/summarize-primes.fth
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
: prime? ( n -- flag )
|
||||
dup 2 < if drop false exit then
|
||||
dup 2 mod 0= if 2 = exit then
|
||||
dup 3 mod 0= if 3 = exit then
|
||||
5
|
||||
begin
|
||||
2dup dup * >=
|
||||
while
|
||||
2dup mod 0= if 2drop false exit then
|
||||
2 +
|
||||
2dup mod 0= if 2drop false exit then
|
||||
4 +
|
||||
repeat
|
||||
2drop true ;
|
||||
|
||||
: main
|
||||
0 0 { count sum }
|
||||
." count prime sum" cr
|
||||
1000 2 do
|
||||
i prime? if
|
||||
count 1+ to count
|
||||
sum i + to sum
|
||||
sum prime? if
|
||||
." " count 3 .r ." " i 3 .r ." " sum 5 .r cr
|
||||
then
|
||||
then
|
||||
loop ;
|
||||
|
||||
main
|
||||
bye
|
||||
13
Task/Summarize-primes/FreeBASIC/summarize-primes.basic
Normal file
13
Task/Summarize-primes/FreeBASIC/summarize-primes.basic
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#include "isprime.bas"
|
||||
|
||||
print 1,2,2
|
||||
dim as integer sum = 2, i, n=1
|
||||
for i = 3 to 999 step 2
|
||||
if isprime(i) then
|
||||
sum += i
|
||||
n+=1
|
||||
if isprime(sum) then
|
||||
print n, i, sum
|
||||
end if
|
||||
end if
|
||||
next i
|
||||
17
Task/Summarize-primes/Gambas/summarize-primes.gambas
Normal file
17
Task/Summarize-primes/Gambas/summarize-primes.gambas
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
Use "isprime.bas"
|
||||
|
||||
Public Sub Main()
|
||||
|
||||
Print 1, 2, 2
|
||||
Dim n As Integer = 1, i As Integer, sum As Integer = 2
|
||||
For i = 3 To 999 Step 2
|
||||
If isPrime(i) Then
|
||||
sum += i
|
||||
n += 1
|
||||
If isPrime(sum) Then
|
||||
Print n, i, sum
|
||||
End If
|
||||
End If
|
||||
Next
|
||||
|
||||
End
|
||||
23
Task/Summarize-primes/Go/summarize-primes.go
Normal file
23
Task/Summarize-primes/Go/summarize-primes.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"rcu"
|
||||
)
|
||||
|
||||
func main() {
|
||||
primes := rcu.Primes(999)
|
||||
sum, n, c := 0, 0, 0
|
||||
fmt.Println("Summing the first n primes (<1,000) where the sum is itself prime:")
|
||||
fmt.Println(" n cumulative sum")
|
||||
for _, p := range primes {
|
||||
n++
|
||||
sum += p
|
||||
if rcu.IsPrime(sum) {
|
||||
c++
|
||||
fmt.Printf("%3d %6s\n", n, rcu.Commatize(sum))
|
||||
}
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println(c, "such prime sums found")
|
||||
}
|
||||
18
Task/Summarize-primes/Haskell/summarize-primes.hs
Normal file
18
Task/Summarize-primes/Haskell/summarize-primes.hs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import Data.List (scanl)
|
||||
import Data.Numbers.Primes (isPrime, primes)
|
||||
|
||||
--------------- PRIME SUMS OF FIRST N PRIMES -------------
|
||||
|
||||
indexedPrimeSums :: [(Integer, Integer, Integer)]
|
||||
indexedPrimeSums =
|
||||
filter (\(_, _, n) -> isPrime n) $
|
||||
scanl
|
||||
(\(i, _, m) p -> (succ i, p, p + m))
|
||||
(0, 0, 0)
|
||||
primes
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_ print $
|
||||
takeWhile (\(_, p, _) -> 1000 > p) indexedPrimeSums
|
||||
11
Task/Summarize-primes/J/summarize-primes.j
Normal file
11
Task/Summarize-primes/J/summarize-primes.j
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
primes=: p: i. _1 p: 1000 NB. all prime numbers below 1000
|
||||
sums=: +/\ primes NB. running sum of those primes
|
||||
mask=: 1 p: sums NB. array of 0s, 1s where sums are primes
|
||||
|
||||
NB. indices of prime sums (incremented for 1-based indexing)
|
||||
NB. "copy" only the final primes in the prime sums
|
||||
NB. "copy" only the sums which are prime
|
||||
results=: (>: I. mask) ,. (mask # primes) ,. (mask # sums)
|
||||
|
||||
NB. pretty-printed "boxed" output
|
||||
output=: 2 1 $ ' n prime sum ' ; < results
|
||||
25
Task/Summarize-primes/Jq/summarize-primes.jq
Normal file
25
Task/Summarize-primes/Jq/summarize-primes.jq
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
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 {i:23}
|
||||
| until( (.i * .i) > $n or ($n % .i == 0); .i += 2)
|
||||
| .i * .i > $n
|
||||
end;
|
||||
|
||||
# primes up to but excluding $n
|
||||
def primes($n): [range(2;$n) | select(is_prime)];
|
||||
|
||||
"Prime sums of primes less than 1000",
|
||||
(primes(1000)
|
||||
| range(1; length) as $n
|
||||
| (.[: $n] | add) as $sum
|
||||
| select($sum | is_prime)
|
||||
| "The sum of the \($n) primes from 2 to \(.[$n-1]) is \($sum)." )
|
||||
11
Task/Summarize-primes/Julia/summarize-primes.julia
Normal file
11
Task/Summarize-primes/Julia/summarize-primes.julia
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
using Primes
|
||||
|
||||
p1000 = primes(1000)
|
||||
|
||||
for n in 1:length(p1000)
|
||||
parray = p1000[1:n]
|
||||
sparray = sum(parray)
|
||||
if isprime(sparray)
|
||||
println("The sum of the $n primes from prime 2 to prime $(p1000[n]) is $sparray, which is prime.")
|
||||
end
|
||||
end
|
||||
5
Task/Summarize-primes/Mathematica/summarize-primes.math
Normal file
5
Task/Summarize-primes/Mathematica/summarize-primes.math
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
p = Prime[Range[PrimePi[1000]]];
|
||||
TableForm[
|
||||
Select[Transpose[{Range[Length[p]], p, Accumulate[p]}], Last /* PrimeQ],
|
||||
TableHeadings -> {None, {"Prime count", "Prime", "Prime sum"}}
|
||||
]
|
||||
23
Task/Summarize-primes/Nim/summarize-primes.nim
Normal file
23
Task/Summarize-primes/Nim/summarize-primes.nim
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import math, strformat
|
||||
|
||||
const N = 999
|
||||
|
||||
func isPrime(n: Positive): bool =
|
||||
if (n and 1) == 0: return n == 2
|
||||
if (n mod 3) == 0: return n == 3
|
||||
var d = 5
|
||||
var delta = 2
|
||||
while d <= sqrt(n.toFloat).int:
|
||||
if n mod d == 0: return false
|
||||
inc d, delta
|
||||
delta = 6 - delta
|
||||
result = true
|
||||
|
||||
echo "index prime prime sum"
|
||||
var s = 0
|
||||
var idx = 0
|
||||
for n in 2..N:
|
||||
if n.isPrime:
|
||||
inc idx
|
||||
s += n
|
||||
if s.isPrime: echo &"{idx:3} {n:5} {s:7}"
|
||||
10
Task/Summarize-primes/Perl/summarize-primes.pl
Normal file
10
Task/Summarize-primes/Perl/summarize-primes.pl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use ntheory <nth_prime is_prime>;
|
||||
|
||||
my($n, $s, $limit, @sums) = (0, 0, 1000);
|
||||
do {
|
||||
push @sums, sprintf '%3d %8d', $n, $s if is_prime($s += nth_prime ++$n)
|
||||
} until $n >= $limit;
|
||||
|
||||
print "Of the first $limit primes: @{[scalar @sums]} cumulative prime sums:\n", join "\n", @sums;
|
||||
5
Task/Summarize-primes/Phix/summarize-primes.phix
Normal file
5
Task/Summarize-primes/Phix/summarize-primes.phix
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">sp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)))</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">))),</span><span style="color: #000000;">sp</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">sprint</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;">"Found %d of em: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span>
|
||||
<!--
|
||||
26
Task/Summarize-primes/Prolog/summarize-primes.pro
Normal file
26
Task/Summarize-primes/Prolog/summarize-primes.pro
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
isPrime(2).
|
||||
isPrime(N):-
|
||||
between(3, inf, N),
|
||||
N /\ 1 > 0, % odd
|
||||
M is floor(sqrt(N)) - 1, % reverse 2*I+1
|
||||
Max is M div 2,
|
||||
forall(between(1, Max, I), N mod (2*I+1) > 0).
|
||||
|
||||
primeSum([], _, _, []).
|
||||
primeSum([P|PList], Index, Acc, [Index|CList]):-
|
||||
Sum is Acc + P,
|
||||
isPrime(Sum),!,
|
||||
format('~|~t~d~3+ ~|~t~d~3+ ~|~t~d~5+', [Index, P, Sum]),nl,
|
||||
Index1 is Index + 1,
|
||||
primeSum(PList, Index1, Sum, CList).
|
||||
primeSum([P|PList], Index, Acc, CntList):-
|
||||
Index1 is Index + 1,
|
||||
Sum is Acc + P,
|
||||
primeSum(PList, Index1, Sum, CntList).
|
||||
|
||||
do:-Limit is 1000,
|
||||
numlist(1, Limit, List),
|
||||
include(isPrime, List, PrimeList),
|
||||
primeSum(PrimeList, 1, 0, CntList),
|
||||
length(CntList, Number),
|
||||
format('~nfound ~d such primes~n', [Number]).
|
||||
19
Task/Summarize-primes/PureBasic/summarize-primes.basic
Normal file
19
Task/Summarize-primes/PureBasic/summarize-primes.basic
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
;XIncludeFile "isprime.pb"
|
||||
|
||||
OpenConsole()
|
||||
Define.i sum, i, n
|
||||
PrintN("1" + #TAB$ + "2" + #TAB$ + "2")
|
||||
sum = 2
|
||||
n = 1
|
||||
For i = 3 To 999 Step 2
|
||||
If isPrime(i):
|
||||
sum + i
|
||||
n + 1
|
||||
If isPrime(sum):
|
||||
PrintN(Str(n) + #TAB$ + Str(i) + #TAB$ + Str(sum))
|
||||
EndIf
|
||||
EndIf
|
||||
Next i
|
||||
|
||||
Input()
|
||||
CloseConsole()
|
||||
74
Task/Summarize-primes/Python/summarize-primes.py
Normal file
74
Task/Summarize-primes/Python/summarize-primes.py
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
'''Prime sums of primes up to 1000'''
|
||||
|
||||
|
||||
from itertools import accumulate, chain, takewhile
|
||||
|
||||
|
||||
# primeSums :: [(Int, (Int, Int))]
|
||||
def primeSums():
|
||||
'''Non finite stream of enumerated tuples,
|
||||
in which the first value is a prime,
|
||||
and the second the sum of that prime and all
|
||||
preceding primes.
|
||||
'''
|
||||
return (
|
||||
x for x in enumerate(
|
||||
accumulate(
|
||||
chain([(0, 0)], primes()),
|
||||
lambda a, p: (p, p + a[1])
|
||||
)
|
||||
) if isPrime(x[1][1])
|
||||
)
|
||||
|
||||
|
||||
# ------------------------- TEST -------------------------
|
||||
# main :: IO ()
|
||||
def main():
|
||||
'''Prime sums of primes below 1000'''
|
||||
for x in takewhile(
|
||||
lambda t: 1000 > t[1][0],
|
||||
primeSums()
|
||||
):
|
||||
print(f'{x[0]} -> {x[1][1]}')
|
||||
|
||||
|
||||
# ----------------------- GENERIC ------------------------
|
||||
|
||||
# isPrime :: Int -> Bool
|
||||
def isPrime(n):
|
||||
'''True if n is prime.'''
|
||||
if n in (2, 3):
|
||||
return True
|
||||
if 2 > n or 0 == n % 2:
|
||||
return False
|
||||
if 9 > n:
|
||||
return True
|
||||
if 0 == n % 3:
|
||||
return False
|
||||
|
||||
def p(x):
|
||||
return 0 == n % x or 0 == n % (2 + x)
|
||||
|
||||
return not any(map(p, range(5, 1 + int(n ** 0.5), 6)))
|
||||
|
||||
|
||||
# primes :: [Int]
|
||||
def primes():
|
||||
''' Non finite sequence of prime numbers.
|
||||
'''
|
||||
n = 2
|
||||
dct = {}
|
||||
while True:
|
||||
if n in dct:
|
||||
for p in dct[n]:
|
||||
dct.setdefault(n + p, []).append(p)
|
||||
del dct[n]
|
||||
else:
|
||||
yield n
|
||||
dct[n * n] = [n]
|
||||
n = 1 + n
|
||||
|
||||
|
||||
# MAIN ---
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
38
Task/Summarize-primes/REXX/summarize-primes.rexx
Normal file
38
Task/Summarize-primes/REXX/summarize-primes.rexx
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/*REXX pgm finds summation primes P, primes which the sum of primes up to P are prime. */
|
||||
parse arg hi . /*obtain optional argument from the CL.*/
|
||||
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/
|
||||
call genP /*build array of semaphores for primes.*/
|
||||
w= 30; w2= w*2%3; pad= left('',w-w2) /*the width of the columns two & three.*/
|
||||
title= ' summation primes which the sum of primes up to P is also prime, P < ' ,
|
||||
commas(hi)
|
||||
say ' index │' center(subword(title, 1, 2), w) center('prime sum', w) /*display title.*/
|
||||
say '───────┼'center("" , 1 + (w+1)*2, '─') /* " sep. */
|
||||
found= 0 /*initialize # of summation primes. */
|
||||
$= 0 /*sum of primes up to the current prime*/
|
||||
do j=1 for hi-1; p= @.j; $= $ + p /*find summation primes within range. */
|
||||
if \!.$ then iterate /*Is sum─of─primes a prime? Then skip.*/
|
||||
found= found + 1 /*bump the number of summation primes. */
|
||||
say right(j, 6) '│'strip( right(commas(p), w2)pad || right(commas($), w2), "T")
|
||||
end /*j*/
|
||||
|
||||
say '───────┴'center("" , 1 + (w+1)*2, '─') /*display foot separator after output. */
|
||||
say
|
||||
say 'Found ' commas(found) title
|
||||
exit 0 /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
genP: !.= 0; sP= 0 /*prime semaphores; sP= sum of primes.*/
|
||||
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */
|
||||
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " flags. */
|
||||
#=5; sq.#= @.# **2 /*number of primes so far; prime². */
|
||||
/* [↓] generate more primes ≤ high.*/
|
||||
do j=@.#+2 by 2 until @.#>=hi & @.#>sP /*find odd primes where P≥hi and P>sP.*/
|
||||
parse var j '' -1 _; if _==5 then iterate /*J ÷ by 5? (right digit)*/
|
||||
if j//3==0 then iterate; if j//7==0 then iterate /*J ÷ by 3?; J ÷ by 7? */
|
||||
do k=5 while sq.k<=j /* [↓] divide by the known odd primes.*/
|
||||
if j // @.k == 0 then iterate j /*Is J ÷ X? Then not prime. ___ */
|
||||
end /*k*/ /* [↑] only process numbers ≤ √ J */
|
||||
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # Ps; assign next P; P square; P*/
|
||||
if @.#<hi then sP= sP + @.# /*maybe add this prime to sum─of─primes*/
|
||||
end /*j*/; return
|
||||
11
Task/Summarize-primes/Raku/summarize-primes.raku
Normal file
11
Task/Summarize-primes/Raku/summarize-primes.raku
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
use Lingua::EN::Numbers;
|
||||
|
||||
my @primes = grep *.is-prime, ^Inf;
|
||||
my @primesums = [\+] @primes;
|
||||
say "{.elems} cumulative prime sums:\n",
|
||||
.map( -> $p {
|
||||
sprintf "The sum of the first %3d (up to {@primes[$p]}) is prime: %s",
|
||||
1 + $p, comma @primesums[$p]
|
||||
}
|
||||
).join("\n")
|
||||
given grep { @primesums[$_].is-prime }, ^1000;
|
||||
25
Task/Summarize-primes/Ring/summarize-primes.ring
Normal file
25
Task/Summarize-primes/Ring/summarize-primes.ring
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
load "stdlib.ring"
|
||||
see "working..." + nl
|
||||
see "Summarize primes:" + nl
|
||||
see "n sum" + nl
|
||||
row = 0
|
||||
sum = 0
|
||||
limit = 1000
|
||||
Primes = []
|
||||
|
||||
for n = 2 to limit
|
||||
if isprime(n)
|
||||
add(Primes,n)
|
||||
ok
|
||||
next
|
||||
|
||||
for n = 1 to len(Primes)
|
||||
sum = sum + Primes[n]
|
||||
if isprime(sum)
|
||||
row = row + 1
|
||||
see "" + n + " " + sum + nl
|
||||
ok
|
||||
next
|
||||
|
||||
see "Found " + row + " numbers" + nl
|
||||
see "done..." + nl
|
||||
45
Task/Summarize-primes/Ruby/summarize-primes.rb
Normal file
45
Task/Summarize-primes/Ruby/summarize-primes.rb
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
def isPrime(n)
|
||||
if n < 2 then
|
||||
return false
|
||||
end
|
||||
|
||||
if n % 2 == 0 then
|
||||
return n == 2
|
||||
end
|
||||
if n % 3 == 0 then
|
||||
return n == 3
|
||||
end
|
||||
|
||||
i = 5
|
||||
while i * i <= n
|
||||
if n % i == 0 then
|
||||
return false
|
||||
end
|
||||
i += 2
|
||||
|
||||
if n % i == 0 then
|
||||
return false
|
||||
end
|
||||
i += 4
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
START = 1
|
||||
STOP = 1000
|
||||
|
||||
sum = 0
|
||||
count = 0
|
||||
sc = 0
|
||||
|
||||
for p in START .. STOP
|
||||
if isPrime(p) then
|
||||
count += 1
|
||||
sum += p
|
||||
if isPrime(sum) then
|
||||
print "The sum of %3d primes in [2, %3d] is %5d which is also prime\n" % [count, p, sum]
|
||||
sc += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
print "There are %d summerized primes in [%d, %d]\n" % [sc, START, STOP]
|
||||
18
Task/Summarize-primes/Rust/summarize-primes.rust
Normal file
18
Task/Summarize-primes/Rust/summarize-primes.rust
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// [dependencies]
|
||||
// primal = "0.3"
|
||||
|
||||
fn main() {
|
||||
let limit = 1000;
|
||||
let mut sum = 0;
|
||||
println!("count prime sum");
|
||||
for (n, p) in primal::Sieve::new(limit)
|
||||
.primes_from(2)
|
||||
.take_while(|x| *x < limit)
|
||||
.enumerate()
|
||||
{
|
||||
sum += p;
|
||||
if primal::is_prime(sum as u64) {
|
||||
println!(" {:>3} {:>3} {:>5}", n + 1, p, sum);
|
||||
}
|
||||
}
|
||||
}
|
||||
23
Task/Summarize-primes/Scala/summarize-primes.scala
Normal file
23
Task/Summarize-primes/Scala/summarize-primes.scala
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
object PrimeSum extends App {
|
||||
|
||||
val oddPrimes: LazyList[Int] = 3 #:: LazyList.from(5, 2)
|
||||
.filter(p => oddPrimes.takeWhile(_ <= math.sqrt(p)).forall(p % _ > 0))
|
||||
val primes = 2 #:: oddPrimes
|
||||
|
||||
def isPrime(n: Int): Boolean = {
|
||||
if (n < 5) (n | 1) == 3
|
||||
else primes.takeWhile(_ <= math.sqrt(n)).forall(n % _ > 0)
|
||||
}
|
||||
|
||||
val limit = primes.takeWhile(_ <= 1000).length
|
||||
|
||||
val number = (1 to limit).filter(index => {
|
||||
val list = primes.take(index)
|
||||
val sum = list.sum
|
||||
val flag = isPrime(sum)
|
||||
if (flag) println(f"$index%3d ${list.last}%3d $sum%5d")
|
||||
flag
|
||||
}).length
|
||||
|
||||
println(s"\nfound $number such primes")
|
||||
}
|
||||
7
Task/Summarize-primes/Sidef/summarize-primes.sidef
Normal file
7
Task/Summarize-primes/Sidef/summarize-primes.sidef
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
1000.primes.map_reduce {|a,b| a + b }.map_kv {|k,v|
|
||||
[k+1, prime(k+1), v]
|
||||
}.grep { .tail.is_prime }.prepend(
|
||||
['count', 'prime', 'sum']
|
||||
).each_2d {|n,p,s|
|
||||
printf("%5s %6s %8s\n", n, p, s)
|
||||
}
|
||||
18
Task/Summarize-primes/Wren/summarize-primes.wren
Normal file
18
Task/Summarize-primes/Wren/summarize-primes.wren
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import "/math" for Int
|
||||
import "/fmt" for Fmt
|
||||
|
||||
var primes = Int.primeSieve(999)
|
||||
var sum = 0
|
||||
var n = 0
|
||||
var c = 0
|
||||
System.print("Summing the first n primes (<1,000) where the sum is itself prime:")
|
||||
System.print(" n cumulative sum")
|
||||
for (p in primes) {
|
||||
n = n + 1
|
||||
sum = sum + p
|
||||
if (Int.isPrime(sum)) {
|
||||
c = c + 1
|
||||
Fmt.print("$3d $,6d", n, sum)
|
||||
}
|
||||
}
|
||||
System.print("\n%(c) such prime sums found")
|
||||
29
Task/Summarize-primes/XPL0/summarize-primes.xpl0
Normal file
29
Task/Summarize-primes/XPL0/summarize-primes.xpl0
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
func IsPrime(N); \Return 'true' if N is a prime number
|
||||
int N, I;
|
||||
[if N <= 1 then return false;
|
||||
for I:= 2 to sqrt(N) do
|
||||
if rem(N/I) = 0 then return false;
|
||||
return true;
|
||||
];
|
||||
|
||||
int Count, N, Sum, Prime;
|
||||
[Text(0, "Prime Prime
|
||||
count sum
|
||||
");
|
||||
Count:= 0; N:= 0; Sum:= 0;
|
||||
for Prime:= 2 to 1000-1 do
|
||||
if IsPrime(Prime) then
|
||||
[N:= N+1;
|
||||
Sum:= Sum + Prime;
|
||||
if IsPrime(Sum) then
|
||||
[Count:= Count+1;
|
||||
IntOut(0, N);
|
||||
ChOut(0, 9\tab\);
|
||||
IntOut(0, Sum);
|
||||
CrLf(0);
|
||||
];
|
||||
];
|
||||
IntOut(0, Count);
|
||||
Text(0, " prime sums of primes found below 1000.
|
||||
");
|
||||
]
|
||||
12
Task/Summarize-primes/Yabasic/summarize-primes.basic
Normal file
12
Task/Summarize-primes/Yabasic/summarize-primes.basic
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
//import isprime
|
||||
print 1, chr$(9), 2, chr$(9), 2
|
||||
sum = 2
|
||||
n = 1
|
||||
for i = 3 to 999 step 2
|
||||
if isPrime(i) then
|
||||
sum = sum + i
|
||||
n = n + 1
|
||||
if isPrime(sum) print n, chr$(9), i, chr$(9), sum
|
||||
fi
|
||||
next i
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue