Sum of primes in odd positions is prime: Difference between revisions
Content deleted Content added
Added Sidef |
Thundergnat (talk | contribs) m syntax highlighting fixup automation |
||
Line 12: | Line 12: | ||
{{trans|Nim}} |
{{trans|Nim}} |
||
< |
<syntaxhighlight lang="11l">F is_prime(n) |
||
I n == 2 |
I n == 2 |
||
R 1B |
R 1B |
||
Line 33: | Line 33: | ||
s += p |
s += p |
||
I is_prime(s) |
I is_prime(s) |
||
print(f:‘{idx:3} {p:3} {s:5}’)</ |
print(f:‘{idx:3} {p:3} {s:5}’)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 53: | Line 53: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
{{libheader|Action! Tool Kit}} |
{{libheader|Action! Tool Kit}} |
||
< |
<syntaxhighlight lang="action!">INCLUDE "D2:PRINTF.ACT" ;from the Action! Tool Kit |
||
BYTE FUNC IsPrime(CARD x) |
BYTE FUNC IsPrime(CARD x) |
||
Line 89: | Line 89: | ||
FI |
FI |
||
OD |
OD |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sum_of_primes_in_odd_positions_is_prime.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sum_of_primes_in_odd_positions_is_prime.png Screenshot from Atari 8-bit computer] |
||
Line 109: | Line 109: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{libheader|ALGOL 68-primes}} |
{{libheader|ALGOL 68-primes}} |
||
< |
<syntaxhighlight lang="algol68">BEGIN # find primes (up to 999) p(i) for odd i such that the sum of primes p(j), j = 1, 3, 5, ..., i is prime # |
||
PR read "primes.incl.a68" PR |
PR read "primes.incl.a68" PR |
||
INT max prime = 999; |
INT max prime = 999; |
||
Line 130: | Line 130: | ||
FI |
FI |
||
OD |
OD |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 148: | Line 148: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f SUM_OF_PRIMES_IN_ODD_POSITIONS_IS_PRIME.AWK |
# syntax: GAWK -f SUM_OF_PRIMES_IN_ODD_POSITIONS_IS_PRIME.AWK |
||
# converted from Ring |
# converted from Ring |
||
Line 181: | Line 181: | ||
return(1) |
return(1) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 201: | Line 201: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include<stdio.h> |
||
#include<stdlib.h> |
#include<stdlib.h> |
||
Line 226: | Line 226: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{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#)] |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Sum of primes in odd positions is prime. Nigel Galloway: November 9th., 2021 |
// Sum of primes in odd positions is prime. Nigel Galloway: November 9th., 2021 |
||
primes32()|>Seq.chunkBySize 2|>Seq.mapi(fun n g->(2*n+1,g.[0]))|>Seq.scan(fun(n,i,g)(e,l)->(e,l,g+l))(0,0,0)|>Seq.takeWhile(fun(_,n,_)->n<1000)|>Seq.filter(fun(_,_,n)->isPrime n)|>Seq.iter(fun(n,g,l)->printfn $"i=%3d{n} p[i]=%3d{g} sum=%5d{l}") |
primes32()|>Seq.chunkBySize 2|>Seq.mapi(fun n g->(2*n+1,g.[0]))|>Seq.scan(fun(n,i,g)(e,l)->(e,l,g+l))(0,0,0)|>Seq.takeWhile(fun(_,n,_)->n<1000)|>Seq.filter(fun(_,_,n)->isPrime n)|>Seq.iter(fun(n,g,l)->printfn $"i=%3d{n} p[i]=%3d{g} sum=%5d{l}") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 250: | Line 250: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2021-06-02}} |
{{works with|Factor|0.99 2021-06-02}} |
||
< |
<syntaxhighlight lang="factor">USING: assocs assocs.extras kernel math.primes math.statistics |
||
prettyprint sequences.extras ; |
prettyprint sequences.extras ; |
||
1000 primes-upto <evens> dup cum-sum zip [ prime? ] filter-values .</ |
1000 primes-upto <evens> dup cum-sum zip [ prime? ] filter-values .</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 272: | Line 272: | ||
=={{header|Fermat}}== |
=={{header|Fermat}}== |
||
< |
<syntaxhighlight lang="fermat">s:=0; |
||
for ii=0 to 83 do oi:=1+2*ii;s:=s+Prime(oi);if Isprime(s)=1 then !!(oi, Prime(oi), s) fi od;</ |
for ii=0 to 83 do oi:=1+2*ii;s:=s+Prime(oi);if Isprime(s)=1 then !!(oi, Prime(oi), s) fi od;</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">#include "isprime.bas" |
||
dim as uinteger i = 1, p, sum = 0 |
dim as uinteger i = 1, p, sum = 0 |
||
for p = 2 to 999 |
for p = 2 to 999 |
||
Line 286: | Line 286: | ||
i = i + 1 |
i = i + 1 |
||
end if |
end if |
||
next p</ |
next p</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
{{libheader|Go-rcu}} |
{{libheader|Go-rcu}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 309: | Line 309: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 329: | Line 329: | ||
=={{header|GW-BASIC}}== |
=={{header|GW-BASIC}}== |
||
< |
<syntaxhighlight lang="gwbasic">10 S = 2 |
||
20 A = 1 |
20 A = 1 |
||
30 PRINT 1, 2, 2 |
30 PRINT 1, 2, 2 |
||
Line 355: | Line 355: | ||
250 IF Q = 1 THEN PRINT A, T, S |
250 IF Q = 1 THEN PRINT A, T, S |
||
260 P = T |
260 P = T |
||
270 RETURN</ |
270 RETURN</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 362: | Line 362: | ||
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`. |
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`. |
||
< |
<syntaxhighlight lang="jq">def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .; |
||
def task: |
def task: |
||
Line 374: | Line 374: | ||
| "\(.i|lpad(3)) \($oddPositionPrimes[$i]|lpad(3)) \(.sum|lpad(5))" ) ; |
| "\(.i|lpad(3)) \($oddPositionPrimes[$i]|lpad(3)) \(.sum|lpad(5))" ) ; |
||
" i p[$i] sum", task</ |
" i p[$i] sum", task</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 393: | Line 393: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|Factor}} |
{{trans|Factor}} |
||
< |
<syntaxhighlight lang="julia">using Primes |
||
p = primes(1000) |
p = primes(1000) |
||
arr = filter(n -> isprime(n[2]), accumulate((x, y) -> (y, x[2] + y), p[1:2:length(p)], init = (0, 0))) |
arr = filter(n -> isprime(n[2]), accumulate((x, y) -> (y, x[2] + y), p[1:2:length(p)], init = (0, 0))) |
||
println(join(arr, "\n")) |
println(join(arr, "\n")) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
(2, 2) |
(2, 2) |
||
Line 413: | Line 413: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">p = Prime[Range[1, PrimePi[1000], 2]]; |
||
p = {p, Accumulate[p]} // Transpose; |
p = {p, Accumulate[p]} // Transpose; |
||
Select[p, Last /* PrimeQ]</ |
Select[p, Last /* PrimeQ]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{{2,2},{5,7},{31,89},{103,659},{149,1181},{331,5021},{467,9923},{499,10909},{523,11941},{653,17959},{823,26879}}</pre> |
<pre>{{2,2},{5,7},{31,89},{103,659},{149,1181},{331,5021},{467,9923},{499,10909},{523,11941},{653,17959},{823,26879}}</pre> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import strformat |
||
template isOdd(n: Natural): bool = (n and 1) != 0 |
template isOdd(n: Natural): bool = (n and 1) != 0 |
||
Line 449: | Line 449: | ||
inc sum, p |
inc sum, p |
||
if sum.isPrime: |
if sum.isPrime: |
||
echo &"{idx:3} {p:3} {sum:5}"</ |
echo &"{idx:3} {p:3} {sum:5}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 466: | Line 466: | ||
=={{header|PARI-GP}}== |
=={{header|PARI-GP}}== |
||
< |
<syntaxhighlight lang="parigp">sm=0;for(ii=0, 83, oi=1+2*ii;sm=sm+prime(oi);if(isprime(sm),print(oi," ", prime(oi)," ",sm)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 484: | Line 484: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use ntheory 'is_prime'; |
use ntheory 'is_prime'; |
||
Line 497: | Line 497: | ||
printf "%6d%6d%6d\n", $c, $odd[$_], $sums[$_] if is_prime $sums[$_]; |
printf "%6d%6d%6d\n", $c, $odd[$_], $sums[$_] if is_prime $sums[$_]; |
||
$c += 2; |
$c += 2; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 2 2 |
<pre> 1 2 2 |
||
Line 512: | Line 512: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">primes</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: #004080;">sequence</span> <span style="color: #000000;">primes</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> |
||
Line 524: | Line 524: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 543: | Line 543: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>my @odd = grep { ++$ !%% 2 }, grep &is-prime, 2 ..^ 1000; |
||
my @sums = [\+] @odd; |
my @sums = [\+] @odd; |
||
say .fmt('%5d') for grep { .[2].is-prime }, ( (1,3…*) Z @odd Z @sums );</ |
say .fmt('%5d') for grep { .[2].is-prime }, ( (1,3…*) Z @odd Z @sums );</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 2 2 |
<pre> 1 2 2 |
||
Line 561: | Line 561: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/*REXX pgm shows a prime index, the prime, & sum of odd indexed primes when sum is 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 593: | Line 593: | ||
end /*k*/ /* [↑] only process numbers ≤ √ J */ |
end /*k*/ /* [↑] only process numbers ≤ √ J */ |
||
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ |
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ |
||
end /*j*/; return</ |
end /*j*/; return</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 615: | Line 615: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
see "working..." + nl |
see "working..." + nl |
||
Line 637: | Line 637: | ||
see "done..." + nl |
see "done..." + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 657: | Line 657: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">require 'prime' |
||
sum = 0 |
sum = 0 |
||
Line 663: | Line 663: | ||
puts "%6d%6d%6d" % [i, odd_i, sum] if (sum += odd_i).prime? |
puts "%6d%6d%6d" % [i, odd_i, sum] if (sum += odd_i).prime? |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> 1 2 2 |
<pre> 1 2 2 |
||
Line 679: | Line 679: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">var sum = 0 |
||
1e3.primes.each_kv {|k,v| |
1e3.primes.each_kv {|k,v| |
||
if (k+1 -> is_odd) { |
if (k+1 -> is_odd) { |
||
Line 685: | Line 685: | ||
say "#{k+1} #{v} #{sum}" if sum.is_prime |
say "#{k+1} #{v} #{sum}" if sum.is_prime |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 702: | Line 702: | ||
=={{header|Tiny BASIC}}== |
=={{header|Tiny BASIC}}== |
||
< |
<syntaxhighlight lang="tinybasic"> LET I = 0 |
||
LET S = 0 |
LET S = 0 |
||
LET P = 1 |
LET P = 1 |
||
Line 730: | Line 730: | ||
GOSUB 100 |
GOSUB 100 |
||
IF Z = 1 THEN PRINT I," ", P," ", S |
IF Z = 1 THEN PRINT I," ", P," ", S |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}}<pre>1 2 2 |
{{out}}<pre>1 2 2 |
||
Line 749: | Line 749: | ||
{{libheader|Wren-trait}} |
{{libheader|Wren-trait}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/math" for Int |
||
import "/trait" for Indexed |
import "/trait" for Indexed |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 760: | Line 760: | ||
sum = sum + se.value |
sum = sum + se.value |
||
if (Int.isPrime(sum)) Fmt.print("$3d $3d $,6d", se.index + 1, se.value, sum) |
if (Int.isPrime(sum)) Fmt.print("$3d $3d $,6d", se.index + 1, se.value, sum) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 780: | Line 780: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<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 801: | Line 801: | ||
]; |
]; |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 821: | Line 821: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
{{trans|XPL0}} |
{{trans|XPL0}} |
||
< |
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Sum_of_primes_in_odd_positions_is_prime |
||
// by Galileo, 04/2022 |
// by Galileo, 04/2022 |
||
Line 843: | Line 843: | ||
end if |
end if |
||
end if |
end if |
||
next</ |
next</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>i p(n) sum |
<pre>i p(n) sum |