Summarize primes: Difference between revisions

Content added Content deleted
(added J solution)
m (syntax highlighting fixup automation)
Line 12: Line 12:
{{trans|Nim}}
{{trans|Nim}}


<lang 11l>F is_prime(a)
<syntaxhighlight lang="11l">F is_prime(a)
I a == 2
I a == 2
R 1B
R 1B
Line 30: Line 30:
s += n
s += n
I is_prime(s)
I is_prime(s)
print(f:‘{idx:3} {n:5} {s:7}’)</lang>
print(f:‘{idx:3} {n:5} {s:7}’)</syntaxhighlight>


{{out}}
{{out}}
Line 60: Line 60:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # sum the primes below n and report the sums that are prime #
<syntaxhighlight lang="algol68">BEGIN # sum the primes below n and report the sums that are prime #
# sieve the primes to 999 #
# sieve the primes to 999 #
PR read "primes.incl.a68" PR
PR read "primes.incl.a68" PR
Line 102: Line 102:
)
)
)
)
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 133: Line 133:


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<lang algolw>begin % sum the primes below n and report the sums that are prime %
<syntaxhighlight lang="algolw">begin % sum the primes below n and report the sums that are prime %
integer MAX_NUMBER;
integer MAX_NUMBER;
MAX_NUMBER := 999;
MAX_NUMBER := 999;
Line 188: Line 188:
)
)
end
end
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 220: Line 220:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>print (pad "index" 6) ++ " | " ++
<syntaxhighlight lang="rebol">print (pad "index" 6) ++ " | " ++
(pad "prime" 6) ++ " | " ++
(pad "prime" 6) ++ " | " ++
(pad "prime sum" 11)
(pad "prime sum" 11)
Line 236: Line 236:
(pad to :string s 11)
(pad to :string s 11)
]
]
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 265: Line 265:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SUMMARIZE_PRIMES.AWK
# syntax: GAWK -f SUMMARIZE_PRIMES.AWK
BEGIN {
BEGIN {
Line 294: Line 294:
return(1)
return(1)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 323: Line 323:
=={{header|C}}==
=={{header|C}}==
{{trans|C++}}
{{trans|C++}}
<lang c>#include <stdbool.h>
<syntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdio.h>


Line 377: Line 377:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>The sum of 1 primes in [2, 2] is 2 which is also prime
<pre>The sum of 1 primes in [2, 2] is 2 which is also prime
Line 403: Line 403:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>


bool is_prime(int n) {
bool is_prime(int n) {
Line 454: Line 454:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>The sum of 1 primes in [2, 2] is 2 which is also prime
<pre>The sum of 1 primes in [2, 2] is 2 which is also prime
Line 481: Line 481:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Summarize Primes: Nigel Galloway. April 16th., 2021
// 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)
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)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 513: Line 513:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
{{works with|Factor|0.99 2021-02-05}}
<lang factor>USING: assocs formatting kernel math.primes math.ranges
<syntaxhighlight lang="factor">USING: assocs formatting kernel math.primes math.ranges
math.statistics prettyprint ;
math.statistics prettyprint ;


1000 [ [1,b] ] [ primes-upto cum-sum ] bi zip
1000 [ [1,b] ] [ primes-upto cum-sum ] bi zip
[ nip prime? ] assoc-filter
[ nip prime? ] assoc-filter
[ "The sum of the first %3d primes is %5d (which is prime).\n" printf ] assoc-each</lang>
[ "The sum of the first %3d primes is %5d (which is prime).\n" printf ] assoc-each</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 545: Line 545:


=={{header|Fermat}}==
=={{header|Fermat}}==
<lang fermat>n:=0
<syntaxhighlight lang="fermat">n:=0
s:=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
for i=1, 162 do s:=s+Prime(i);if Isprime(s)=1 then n:=n+1;!!(n,Prime(i),s) fi od
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 576: Line 576:
=={{header|Forth}}==
=={{header|Forth}}==
{{works with|Gforth}}
{{works with|Gforth}}
<lang forth>: prime? ( n -- flag )
<syntaxhighlight lang="forth">: prime? ( n -- flag )
dup 2 < if drop false exit then
dup 2 < if drop false exit then
dup 2 mod 0= if 2 = exit then
dup 2 mod 0= if 2 = exit then
Line 605: Line 605:


main
main
bye</lang>
bye</syntaxhighlight>


{{out}}
{{out}}
Line 634: Line 634:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>#include "isprime.bas"
<syntaxhighlight lang="freebasic">#include "isprime.bas"


print 1,2,2
print 1,2,2
Line 646: Line 646:
end if
end if
end if
end if
next i</lang>
next i</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 682: Line 682:
{{trans|Wren}}
{{trans|Wren}}
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 704: Line 704:
fmt.Println()
fmt.Println()
fmt.Println(c, "such prime sums found")
fmt.Println(c, "such prime sums found")
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 711: Line 711:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.List (scanl)
<syntaxhighlight lang="haskell">import Data.List (scanl)
import Data.Numbers.Primes (isPrime, primes)
import Data.Numbers.Primes (isPrime, primes)


Line 729: Line 729:
mapM_ print $
mapM_ print $
takeWhile (\(_, p, _) -> 1000 > p) indexedPrimeSums
takeWhile (\(_, p, _) -> 1000 > p) indexedPrimeSums
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>(1,2,2)
<pre>(1,2,2)
Line 754: Line 754:


=={{header|J}}==
=={{header|J}}==
<lang j>primes=: p: i. _1 p: 1000 NB. all prime numbers below 1000
<syntaxhighlight lang="j">primes=: p: i. _1 p: 1000 NB. all prime numbers below 1000
sums=: +/\ primes NB. running sum of those primes
sums=: +/\ primes NB. running sum of those primes
mask=: 1 p: sums NB. array of 0s, 1s where sums are primes
mask=: 1 p: sums NB. array of 0s, 1s where sums are primes
Line 764: Line 764:


NB. pretty-printed "boxed" output
NB. pretty-printed "boxed" output
output=: 2 1 $ ' n prime sum ' ; < results</lang>
output=: 2 1 $ ' n prime sum ' ; < results</syntaxhighlight>
{{out}}
{{out}}
<pre> output
<pre> output
Line 796: Line 796:
{{works with|jq}}
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
'''Works with gojq, the Go implementation of jq'''
<lang jq>def is_prime:
<syntaxhighlight lang="jq">def is_prime:
. as $n
. as $n
| if ($n < 2) then false
| if ($n < 2) then false
Line 820: Line 820:
| (.[: $n] | add) as $sum
| (.[: $n] | add) as $sum
| select($sum | is_prime)
| select($sum | is_prime)
| "The sum of the \($n) primes from 2 to \(.[$n-1]) is \($sum)." )</lang>
| "The sum of the \($n) primes from 2 to \(.[$n-1]) is \($sum)." )</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 848: Line 848:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Primes
<syntaxhighlight lang="julia">using Primes


p1000 = primes(1000)
p1000 = primes(1000)
Line 859: Line 859:
end
end
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
The sum of the 1 primes from prime 2 to prime 2 is 2, which is prime.
The sum of the 1 primes from prime 2 to prime 2 is 2, which is prime.
Line 885: Line 885:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>p = Prime[Range[PrimePi[1000]]];
<syntaxhighlight lang="mathematica">p = Prime[Range[PrimePi[1000]]];
TableForm[
TableForm[
Select[Transpose[{Range[Length[p]], p, Accumulate[p]}], Last /* PrimeQ],
Select[Transpose[{Range[Length[p]], p, Accumulate[p]}], Last /* PrimeQ],
TableHeadings -> {None, {"Prime count", "Prime", "Prime sum"}}
TableHeadings -> {None, {"Prime count", "Prime", "Prime sum"}}
]</lang>
]</syntaxhighlight>
{{out}}
{{out}}
<pre>Prime count Prime Prime sum
<pre>Prime count Prime Prime sum
Line 915: Line 915:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import math, strformat
<syntaxhighlight lang="nim">import math, strformat


const N = 999
const N = 999
Line 937: Line 937:
inc idx
inc idx
s += n
s += n
if s.isPrime: echo &"{idx:3} {n:5} {s:7}"</lang>
if s.isPrime: echo &"{idx:3} {n:5} {s:7}"</syntaxhighlight>


{{out}}
{{out}}
Line 965: Line 965:
=={{header|Perl}}==
=={{header|Perl}}==
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use ntheory <nth_prime is_prime>;
use ntheory <nth_prime is_prime>;
Line 974: Line 974:
} until $n >= $limit;
} until $n >= $limit;


print "Of the first $limit primes: @{[scalar @sums]} cumulative prime sums:\n", join "\n", @sums;</lang>
print "Of the first $limit primes: @{[scalar @sums]} cumulative prime sums:\n", join "\n", @sums;</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:70ex">Of the first 1000 primes: 76 cumulative prime sums:
<pre style="height:70ex">Of the first 1000 primes: 76 cumulative prime sums:
Line 1,056: Line 1,056:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(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: #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: #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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,067: Line 1,067:


=={{header|Python}}==
=={{header|Python}}==
<lang python>'''Prime sums of primes up to 1000'''
<syntaxhighlight lang="python">'''Prime sums of primes up to 1000'''




Line 1,141: Line 1,141:
if __name__ == '__main__':
if __name__ == '__main__':
main()
main()
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>1 -> 2
<pre>1 -> 2
Line 1,166: Line 1,166:


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6>use Lingua::EN::Numbers;
<syntaxhighlight lang="raku" line>use Lingua::EN::Numbers;


my @primes = grep *.is-prime, ^Inf;
my @primes = grep *.is-prime, ^Inf;
Line 1,176: Line 1,176:
}
}
).join("\n")
).join("\n")
given grep { @primesums[$_].is-prime }, ^1000;</lang>
given grep { @primesums[$_].is-prime }, ^1000;</syntaxhighlight>
{{out}}
{{out}}
<pre>76 cumulative prime sums:
<pre>76 cumulative prime sums:
Line 1,257: Line 1,257:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX pgm finds summation primes P, primes which the sum of primes up to P are prime. */
<syntaxhighlight lang="rexx">/*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.*/
parse arg hi . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/
Line 1,294: Line 1,294:
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # Ps; assign next P; P square; P*/
#= #+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*/
if @.#<hi then sP= sP + @.# /*maybe add this prime to sum─of─primes*/
end /*j*/; return</lang>
end /*j*/; return</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 1,326: Line 1,326:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"
see "working..." + nl
see "working..." + nl
Line 1,352: Line 1,352:
see "Found " + row + " numbers" + nl
see "Found " + row + " numbers" + nl
see "done..." + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,385: Line 1,385:
=={{header|Ruby}}==
=={{header|Ruby}}==
{{trans|C++}}
{{trans|C++}}
<lang ruby>def isPrime(n)
<syntaxhighlight lang="ruby">def isPrime(n)
if n < 2 then
if n < 2 then
return false
return false
Line 1,429: Line 1,429:
end
end
end
end
print "There are %d summerized primes in [%d, %d]\n" % [sc, START, STOP]</lang>
print "There are %d summerized primes in [%d, %d]\n" % [sc, START, STOP]</syntaxhighlight>
{{out}}
{{out}}
<pre>The sum of 1 primes in [2, 2] is 2 which is also prime
<pre>The sum of 1 primes in [2, 2] is 2 which is also prime
Line 1,455: Line 1,455:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>// [dependencies]
<syntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
// primal = "0.3"


Line 1,472: Line 1,472:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,501: Line 1,501:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>1000.primes.map_reduce {|a,b| a + b }.map_kv {|k,v|
<syntaxhighlight lang="ruby">1000.primes.map_reduce {|a,b| a + b }.map_kv {|k,v|
[k+1, prime(k+1), v]
[k+1, prime(k+1), v]
}.grep { .tail.is_prime }.prepend(
}.grep { .tail.is_prime }.prepend(
Line 1,507: Line 1,507:
).each_2d {|n,p,s|
).each_2d {|n,p,s|
printf("%5s %6s %8s\n", n, p, s)
printf("%5s %6s %8s\n", n, p, s)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,537: Line 1,537:
{{libheader|Wren-math}}
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/math" for Int
<syntaxhighlight lang="ecmascript">import "/math" for Int
import "/fmt" for Fmt
import "/fmt" for Fmt


Line 1,554: Line 1,554:
}
}
}
}
System.print("\n%(c) such prime sums found")</lang>
System.print("\n%(c) such prime sums found")</syntaxhighlight>


{{out}}
{{out}}
Line 1,586: Line 1,586:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func IsPrime(N); \Return 'true' if N is a prime number
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
int N, I;
[if N <= 1 then return false;
[if N <= 1 then return false;
Line 1,614: Line 1,614:
Text(0, " prime sums of primes found below 1000.
Text(0, " prime sums of primes found below 1000.
");
");
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}