Sum of primes in odd positions is prime: Difference between revisions

m
syntax highlighting fixup automation
(Added Sidef)
m (syntax highlighting fixup automation)
Line 12:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F is_prime(n)
I n == 2
R 1B
Line 33:
s += p
I is_prime(s)
print(f:‘{idx:3} {p:3} {s:5}’)</langsyntaxhighlight>
 
{{out}}
Line 53:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "D2:PRINTF.ACT" ;from the Action! Tool Kit
 
BYTE FUNC IsPrime(CARD x)
Line 89:
FI
OD
RETURN</langsyntaxhighlight>
{{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]
Line 109:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight 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
INT max prime = 999;
Line 130:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 148:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SUM_OF_PRIMES_IN_ODD_POSITIONS_IS_PRIME.AWK
# converted from Ring
Line 181:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 201:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include<stdio.h>
#include<stdlib.h>
 
Line 226:
}
return 0;
}</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// 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}")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 250:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: assocs assocs.extras kernel math.primes math.statistics
prettyprint sequences.extras ;
 
1000 primes-upto <evens> dup cum-sum zip [ prime? ] filter-values .</langsyntaxhighlight>
{{out}}
<pre>
Line 272:
 
=={{header|Fermat}}==
<langsyntaxhighlight 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;</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
dim as uinteger i = 1, p, sum = 0
for p = 2 to 999
Line 286:
i = i + 1
end if
next p</langsyntaxhighlight>
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 309:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 329:
 
=={{header|GW-BASIC}}==
<langsyntaxhighlight lang="gwbasic">10 S = 2
20 A = 1
30 PRINT 1, 2, 2
Line 355:
250 IF Q = 1 THEN PRINT A, T, S
260 P = T
270 RETURN</langsyntaxhighlight>
 
=={{header|jq}}==
Line 362:
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`.
 
<langsyntaxhighlight lang="jq">def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def task:
Line 374:
| "\(.i|lpad(3)) \($oddPositionPrimes[$i]|lpad(3)) \(.sum|lpad(5))" ) ;
 
" i p[$i] sum", task</langsyntaxhighlight>
{{out}}
<pre>
Line 393:
=={{header|Julia}}==
{{trans|Factor}}
<langsyntaxhighlight lang="julia">using Primes
p = primes(1000)
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"))
</langsyntaxhighlight>{{out}}
<pre>
(2, 2)
Line 413:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">p = Prime[Range[1, PrimePi[1000], 2]];
p = {p, Accumulate[p]} // Transpose;
Select[p, Last /* PrimeQ]</langsyntaxhighlight>
{{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>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strformat
 
template isOdd(n: Natural): bool = (n and 1) != 0
Line 449:
inc sum, p
if sum.isPrime:
echo &"{idx:3} {p:3} {sum:5}"</langsyntaxhighlight>
 
{{out}}
Line 466:
 
=={{header|PARI-GP}}==
<langsyntaxhighlight lang="parigp">sm=0;for(ii=0, 83, oi=1+2*ii;sm=sm+prime(oi);if(isprime(sm),print(oi," ", prime(oi)," ",sm)))</langsyntaxhighlight>
{{out}}
<pre>
Line 484:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory 'is_prime';
Line 497:
printf "%6d%6d%6d\n", $c, $odd[$_], $sums[$_] if is_prime $sums[$_];
$c += 2;
}</langsyntaxhighlight>
{{out}}
<pre> 1 2 2
Line 512:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
Line 524:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 543:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>my @odd = grep { ++$ !%% 2 }, grep &is-prime, 2 ..^ 1000;
my @sums = [\+] @odd;
 
say .fmt('%5d') for grep { .[2].is-prime }, ( (1,3…*) Z @odd Z @sums );</langsyntaxhighlight>
{{out}}
<pre> 1 2 2
Line 561:
 
=={{header|REXX}}==
<langsyntaxhighlight REXXlang="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.*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/
Line 593:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 615:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 637:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 657:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'prime'
sum = 0
Line 663:
puts "%6d%6d%6d" % [i, odd_i, sum] if (sum += odd_i).prime?
end
</syntaxhighlight>
</lang>
{{out}}
<pre> 1 2 2
Line 679:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var sum = 0
1e3.primes.each_kv {|k,v|
if (k+1 -> is_odd) {
Line 685:
say "#{k+1} #{v} #{sum}" if sum.is_prime
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 702:
 
=={{header|Tiny BASIC}}==
<langsyntaxhighlight lang="tinybasic"> LET I = 0
LET S = 0
LET P = 1
Line 730:
GOSUB 100
IF Z = 1 THEN PRINT I," ", P," ", S
RETURN</langsyntaxhighlight>
 
{{out}}<pre>1 2 2
Line 749:
{{libheader|Wren-trait}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/trait" for Indexed
import "/fmt" for Fmt
Line 760:
sum = sum + se.value
if (Int.isPrime(sum)) Fmt.print("$3d $3d $,6d", se.index + 1, se.value, sum)
}</langsyntaxhighlight>
 
{{out}}
Line 780:
 
=={{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 801:
];
];
]</langsyntaxhighlight>
 
{{out}}
Line 821:
=={{header|Yabasic}}==
{{trans|XPL0}}
<langsyntaxhighlight Yabasiclang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Sum_of_primes_in_odd_positions_is_prime
// by Galileo, 04/2022
 
Line 843:
end if
end if
next</langsyntaxhighlight>
{{out}}
<pre>i p(n) sum
10,333

edits