Prime numbers p for which the sum of primes less than or equal to p is prime: Difference between revisions

m
syntax highlighting fixup automation
(→‎{{header|jq}}: simplify)
m (syntax highlighting fixup automation)
Line 7:
=={{header|ALGOL 68}}==
Same as the [[Summarize primes#ALGOL_68]] solution.
<langsyntaxhighlight lang="algol68">BEGIN # sum the primes below n and report the sums that are prime #
INT max prime = 999; # largest prime to consider #
# sieve the primes to max prime #
Line 55:
)
)
END</langsyntaxhighlight>
{{out}}
<pre>
Line 85:
</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PRIME_NUMBERS_P_WHICH_SUM_OF_PRIME_NUMBERS_LESS_OR_EQUAL_TO_P_IS_PRIME.AWK
BEGIN {
Line 112:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 123:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Primes (+)2..p is prime. Nigel Galloway: July 7th., 2021
primes32()|>Seq.takeWhile((>)1000)|>Seq.scan(fun(n,_) g->(n+g,g))(0,0)|>Seq.filter(fun(n,_)->isPrime n)|>Seq.iter(fun(_,n)->printf "%d " n); printfn ""
</syntaxhighlight>
</lang>
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: assocs assocs.extras kernel math.primes math.statistics
prettyprint ;
 
1000 primes-upto dup cum-sum zip [ prime? ] filter-values .</langsyntaxhighlight>
{{out}}
<pre>
Line 162:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
Dim As Integer column = 0, sum = 0, limit = 1000
 
Line 182:
Color 10 : Print !"\n\nEncontrados "; column; " n£meros."
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 198:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 228:
}
fmt.Println("\nFound", len(results), "such primes")
}</langsyntaxhighlight>
 
{{out}}
Line 241:
 
=={{header|J}}==
<langsyntaxhighlight lang="j">(+#~1:p:+/\)@(i.&.(p:^:_1)) 1000</langsyntaxhighlight>
{{out}}
<pre>2 3 7 13 37 43 281 311 503 541 557 593 619 673 683 733 743 839 881 929 953</pre>
Line 255:
used here.
 
<langsyntaxhighlight lang="jq">def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
# Output: a stream of primes in range(0;$n)
Line 273:
"\nFound \(length) such primes." );
 
task(1000)</langsyntaxhighlight>
{{out}}
<pre>
Line 285:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
primesumto(N) = begin s = 0; [i => s for i in 1:N if isprime(i) && isprime(s += i)] end
Line 296:
end
println("\nTotal such primes < 1000: ", length(primesumdict))
</langsyntaxhighlight>{{out}}
<pre>
Prime Prime Sum to Prime
Line 326:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">cands = Most@NestWhileList[NextPrime, 2, # < 1000 &];
Partition[
cands[[Flatten@Position[PrimeQ /@ Accumulate[cands], True]]],
UpTo[5]] // TableForm</langsyntaxhighlight>
 
{{out}}<pre>
Line 340:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils, sugar
 
const
Line 368:
 
echo "Found $# primes:".format(list.len)
echo list.join(" ")</langsyntaxhighlight>
 
{{out}}
Line 375:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Prime_numbers_p_which_sum_of_prime_numbers_less_or_equal_to_p_is_prime
Line 381:
use ntheory qw( is_prime primes vecsum );
 
print "@{[ grep is_prime( vecsum( @{ primes($_) } ) ), @{ primes(1000) } ]}\n";</langsyntaxhighlight>
{{out}}
<pre>
Line 389:
=={{header|Phix}}==
As per Raku, this is pretty much an exact duplicate of [[Summarize_primes#Phix]], bar output of primes instead of their index.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">sump</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">s</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: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</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;">filter</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;">sump</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;">"%d found: %V\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: #000000;">res</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 401:
=={{header|Raku}}==
Trivial variation of [[Summarize_primes#Raku|Summarize primes]] task. Modified to do double duty.
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
 
my @primes = grep *.is-prime, ^Inf;
Line 411:
}
).join("\n")
given grep { @primesums[$_].is-prime }, ^1000;</langsyntaxhighlight>
{{out}}
<pre>76 cumulative prime sums:
Line 492:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds primes in which sum of primes ≤ P is prime, where P < 1.000.*/
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/
Line 536:
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; 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 550:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 574:
see nl + "Found " + row + " numbers" + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 589:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func primes_with_prime_sum(n, callback) {
var s = 0
n.each_prime {|p|
Line 599:
primes_with_prime_sum(1000, {|p,s|
say "prime: #{'%3s' % p} prime sum: #{'%5s' % s}"
})</langsyntaxhighlight>
{{out}}
<pre>
Line 629:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int, Nums
import "/seq" for Lst
import "/fmt" for Fmt
Line 644:
System.print("Primes 'p' under 1000 where the sum of all primes <= p is also prime:")
for (chunk in Lst.chunks(results, 7)) Fmt.print("$4d", chunk)
System.print("\nFound %(results.count) such primes.")</langsyntaxhighlight>
 
{{out}}
Line 657:
 
=={{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 681:
Text(0, " such numbers found below 1000.
");
]</langsyntaxhighlight>
 
{{out}}
10,333

edits