Summarize primes: Difference between revisions

m
syntax highlighting fixup automation
(added J solution)
m (syntax highlighting fixup automation)
Line 12:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
Line 30:
s += n
I is_prime(s)
print(f:‘{idx:3} {n:5} {s:7}’)</langsyntaxhighlight>
 
{{out}}
Line 60:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # sum the primes below n and report the sums that are prime #
# sieve the primes to 999 #
PR read "primes.incl.a68" PR
Line 102:
)
)
END</langsyntaxhighlight>
{{out}}
<pre>
Line 133:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin % sum the primes below n and report the sums that are prime %
integer MAX_NUMBER;
MAX_NUMBER := 999;
Line 188:
)
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 220:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">print (pad "index" 6) ++ " | " ++
(pad "prime" 6) ++ " | " ++
(pad "prime sum" 11)
Line 236:
(pad to :string s 11)
]
]</langsyntaxhighlight>
 
{{out}}
Line 265:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SUMMARIZE_PRIMES.AWK
BEGIN {
Line 294:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 323:
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
 
Line 377:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>The sum of 1 primes in [2, 2] is 2 which is also prime
Line 403:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
 
bool is_prime(int n) {
Line 454:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>The sum of 1 primes in [2, 2] is 2 which is also prime
Line 481:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// 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)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 513:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">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</langsyntaxhighlight>
{{out}}
<pre>
Line 545:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">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
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 576:
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: prime? ( n -- flag )
dup 2 < if drop false exit then
dup 2 mod 0= if 2 = exit then
Line 605:
 
main
bye</langsyntaxhighlight>
 
{{out}}
Line 634:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
 
print 1,2,2
Line 646:
end if
end if
next i</langsyntaxhighlight>
{{out}}
<pre>
Line 682:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 704:
fmt.Println()
fmt.Println(c, "such prime sums found")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 711:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (scanl)
import Data.Numbers.Primes (isPrime, primes)
 
Line 729:
mapM_ print $
takeWhile (\(_, p, _) -> 1000 > p) indexedPrimeSums
</syntaxhighlight>
</lang>
{{Out}}
<pre>(1,2,2)
Line 754:
 
=={{header|J}}==
<langsyntaxhighlight lang="j">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
Line 764:
 
NB. pretty-printed "boxed" output
output=: 2 1 $ ' n prime sum ' ; < results</langsyntaxhighlight>
{{out}}
<pre> output
Line 796:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<langsyntaxhighlight lang="jq">def is_prime:
. as $n
| if ($n < 2) then false
Line 820:
| (.[: $n] | add) as $sum
| select($sum | is_prime)
| "The sum of the \($n) primes from 2 to \(.[$n-1]) is \($sum)." )</langsyntaxhighlight>
{{out}}
<pre>
Line 848:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
p1000 = primes(1000)
Line 859:
end
end
</langsyntaxhighlight>{{out}}
<pre>
The sum of the 1 primes from prime 2 to prime 2 is 2, which is prime.
Line 885:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">p = Prime[Range[PrimePi[1000]]];
TableForm[
Select[Transpose[{Range[Length[p]], p, Accumulate[p]}], Last /* PrimeQ],
TableHeadings -> {None, {"Prime count", "Prime", "Prime sum"}}
]</langsyntaxhighlight>
{{out}}
<pre>Prime count Prime Prime sum
Line 915:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import math, strformat
 
const N = 999
Line 937:
inc idx
s += n
if s.isPrime: echo &"{idx:3} {n:5} {s:7}"</langsyntaxhighlight>
 
{{out}}
Line 965:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory <nth_prime is_prime>;
Line 974:
} until $n >= $limit;
 
print "Of the first $limit primes: @{[scalar @sums]} cumulative prime sums:\n", join "\n", @sums;</langsyntaxhighlight>
{{out}}
<pre style="height:70ex">Of the first 1000 primes: 76 cumulative prime sums:
Line 1,056:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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: #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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,067:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">'''Prime sums of primes up to 1000'''
 
 
Line 1,141:
if __name__ == '__main__':
main()
</syntaxhighlight>
</lang>
{{Out}}
<pre>1 -> 2
Line 1,166:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
 
my @primes = grep *.is-prime, ^Inf;
Line 1,176:
}
).join("\n")
given grep { @primesums[$_].is-prime }, ^1000;</langsyntaxhighlight>
{{out}}
<pre>76 cumulative prime sums:
Line 1,257:
 
=={{header|REXX}}==
<langsyntaxhighlight 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.*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/
Line 1,294:
#= #+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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,326:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 1,352:
see "Found " + row + " numbers" + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,385:
=={{header|Ruby}}==
{{trans|C++}}
<langsyntaxhighlight lang="ruby">def isPrime(n)
if n < 2 then
return false
Line 1,429:
end
end
print "There are %d summerized primes in [%d, %d]\n" % [sc, START, STOP]</langsyntaxhighlight>
{{out}}
<pre>The sum of 1 primes in [2, 2] is 2 which is also prime
Line 1,455:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
 
Line 1,472:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,501:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">1000.primes.map_reduce {|a,b| a + b }.map_kv {|k,v|
[k+1, prime(k+1), v]
}.grep { .tail.is_prime }.prepend(
Line 1,507:
).each_2d {|n,p,s|
printf("%5s %6s %8s\n", n, p, s)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,537:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/fmt" for Fmt
 
Line 1,554:
}
}
System.print("\n%(c) such prime sums found")</langsyntaxhighlight>
 
{{out}}
Line 1,586:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
Line 1,614:
Text(0, " prime sums of primes found below 1000.
");
]</langsyntaxhighlight>
 
{{out}}
10,339

edits