Sum of primes in odd positions is prime: Difference between revisions
Content deleted Content added
added Easylang |
|||
(18 intermediate revisions by 10 users not shown) | |||
Line 2: | Line 2: | ||
;Task: |
;Task: |
||
<br>Let '''p(i)''' be a sequence of prime numbers. |
<br>Let '''p(i)''' be a sequence of prime numbers. such that 2=p(1), 3=p(2), 5=p(3), ... |
||
<br>Consider the '''p(1),p(3),p(5), ... ,p(i)''', for each '''p(i) < 1,000''' and '''i''' is odd. |
<br>Consider the '''p(1),p(3),p(5), ... ,p(i)''', for each '''p(i) < 1,000''' and '''i''' is odd. |
||
<br>Let '''sum''' be |
<br>Let '''sum''' be any [[wikipedia:Prefix_sum|prefix sum]] of these primesa. |
||
<br>If '''sum''' is prime then print '''i''', '''p(i)''' and '''sum'''. |
<br>If '''sum''' is prime then print '''i''', '''p(i)''' and '''sum'''. |
||
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 146: | Line 146: | ||
143 823 26879 |
143 823 26879 |
||
</pre> |
</pre> |
||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="arturo">print " i | p(i) sum" |
|||
print repeat "-" 17 |
|||
idx: 0 |
|||
sm: 0 |
|||
p: 1 |
|||
while [p < 1000][ |
|||
inc 'p |
|||
if prime? p [ |
|||
inc 'idx |
|||
if odd? idx [ |
|||
sm: sm + p |
|||
if prime? sm -> |
|||
print (pad to :string idx 4) ++ " | " ++ (pad to :string p 3) ++ (pad to :string sm 6) |
|||
] |
|||
] |
|||
]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> i | p(i) sum |
|||
----------------- |
|||
1 | 2 2 |
|||
3 | 5 7 |
|||
11 | 31 89 |
|||
27 | 103 659 |
|||
35 | 149 1181 |
|||
67 | 331 5021 |
|||
91 | 467 9923 |
|||
95 | 499 10909 |
|||
99 | 523 11941 |
|||
119 | 653 17959 |
|||
143 | 823 26879</pre> |
|||
=={{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 216: | ||
return(1) |
return(1) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 201: | Line 236: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include<stdio.h> |
||
#include<stdlib.h> |
#include<stdlib.h> |
||
Line 226: | Line 261: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Delphi}}== |
|||
{{works with|Delphi|6.0}} |
|||
{{libheader|SysUtils,StdCtrls}} |
|||
Uses the [[Extensible_prime_generator#Delphi|Delphi Prime-Generator Object]] |
|||
<syntaxhighlight lang="Delphi"> |
|||
procedure SumOddPrimes(Memo: TMemo); |
|||
var Sieve: TPrimeSieve; |
|||
var I,Inx, Sum: integer; |
|||
begin |
|||
Sieve:=TPrimeSieve.Create; |
|||
try |
|||
Sieve.Intialize(100000); |
|||
Memo.Lines.Add(' I P(I) Sum'); |
|||
Memo.Lines.Add('---------------'); |
|||
I:=0; |
|||
Sum:=0; |
|||
while Sieve.Primes[I]<1000 do |
|||
begin |
|||
Sum:=Sum+Sieve.Primes[I]; |
|||
if Sieve.Flags[Sum] then |
|||
begin |
|||
Memo.Lines.Add(Format('%3d %4d %6d',[I,Sieve.Primes[I],Sum])); |
|||
end; |
|||
Inc(I,2); |
|||
end; |
|||
finally Sieve.Free; end; |
|||
end; |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
I P(I) Sum |
|||
--------------- |
|||
0 2 2 |
|||
2 5 7 |
|||
10 31 89 |
|||
26 103 659 |
|||
34 149 1181 |
|||
66 331 5021 |
|||
90 467 9923 |
|||
94 499 10909 |
|||
98 523 11941 |
|||
118 653 17959 |
|||
142 823 26879 |
|||
Elapsed Time: 16.077 ms. |
|||
</pre> |
|||
=={{header|EasyLang}}== |
|||
{{trans|11l}} |
|||
<syntaxhighlight> |
|||
fastfunc isprim num . |
|||
i = 2 |
|||
while i <= sqrt num |
|||
if num mod i = 0 |
|||
return 0 |
|||
. |
|||
i += 1 |
|||
. |
|||
return 1 |
|||
. |
|||
for p = 2 to 999 |
|||
if isprim p = 1 |
|||
idx += 1 |
|||
if idx mod 2 <> 0 |
|||
s += p |
|||
if isprim s = 1 |
|||
write "(" & idx & " " & p & " " & s & ") " |
|||
. |
|||
. |
|||
. |
|||
. |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
(1 2 2) (3 5 7) (11 31 89) (27 103 659) (35 149 1181) (67 331 5021) (91 467 9923) (95 499 10909) (99 523 11941) (119 653 17959) (143 823 26879) |
|||
</pre> |
|||
=={{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 248: | Line 366: | ||
i=143 p[i]=823 sum=26879 |
i=143 p[i]=823 sum=26879 |
||
</pre> |
</pre> |
||
=={{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 391: | ||
=={{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 405: | ||
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 428: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 329: | Line 448: | ||
=={{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 474: | ||
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|J}}== |
|||
<syntaxhighlight lang=J> (i,.p,.sum) #~ 1 p: sum=. +/\p=. p:<:i=. (#~ 2|])i. 1+p:inv 1000 |
|||
1 2 2 |
|||
3 5 7 |
|||
11 31 89 |
|||
27 103 659 |
|||
35 149 1181 |
|||
67 331 5021 |
|||
91 467 9923 |
|||
95 499 10909 |
|||
99 523 11941 |
|||
119 653 17959 |
|||
143 823 26879</syntaxhighlight> |
|||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 362: | Line 496: | ||
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 508: | ||
| "\(.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 527: | ||
=={{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 547: | ||
=={{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 583: | ||
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 464: | Line 598: | ||
119 653 17959 |
119 653 17959 |
||
143 823 26879</pre> |
143 823 26879</pre> |
||
=={{header|OCaml}}== |
|||
<syntaxhighlight lang="ocaml">let is_prime n = |
|||
let rec test x = |
|||
let q = n / x in x > q || x * q <> n && n mod (x + 2) <> 0 && test (x + 6) |
|||
in if n < 5 then n lor 1 = 3 else n land 1 <> 0 && n mod 3 <> 0 && test 5 |
|||
let () = Seq.ints 3 |
|||
|> Seq.filter is_prime |
|||
|> Seq.take_while ((>) 1000) |
|||
|> Seq.zip (Seq.ints 2) |
|||
|> Seq.filter (fun (i, _) -> i land 1 = 1) |
|||
|> Seq.scan (fun (_, pi, sum) (i, p) -> i, p, sum + p) (1, 2, 2) |
|||
|> Seq.filter (fun (_, _, sum) -> is_prime sum) |
|||
|> Seq.iter (fun (i, pi, sum) -> Printf.printf "p(%u) = %u, %u\n" i pi sum)</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
p(1) = 2, 2 |
|||
p(3) = 5, 7 |
|||
p(11) = 31, 89 |
|||
p(27) = 103, 659 |
|||
p(35) = 149, 1181 |
|||
p(67) = 331, 5021 |
|||
p(91) = 467, 9923 |
|||
p(95) = 499, 10909 |
|||
p(99) = 523, 11941 |
|||
p(119) = 653, 17959 |
|||
p(143) = 823, 26879 |
|||
</pre> |
|||
=={{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 647: | ||
=={{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 660: | ||
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 675: | ||
=={{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 687: | ||
<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 540: | Line 703: | ||
119 653 17,959 |
119 653 17,959 |
||
143 823 26,879 |
143 823 26,879 |
||
</pre> |
|||
=={{header|Quackery}}== |
|||
<code>isprime</code> is defined at [[Primality by trial division#Quackery]]. |
|||
<syntaxhighlight lang="Quackery"> [ dip number$ |
|||
over size - |
|||
space swap of |
|||
swap join echo$ ] is justify ( n n --> ) |
|||
[] 1000 times |
|||
[ i^ isprime if [ i^ join ] ] |
|||
[] swap witheach |
|||
[ i^ 1 & 0 = iff join else drop ] |
|||
0 swap witheach |
|||
[ tuck + dup isprime if |
|||
[ i^ 2 * 1+ 3 justify |
|||
over 5 justify |
|||
dup 7 justify |
|||
cr ] |
|||
nip ] |
|||
drop</syntaxhighlight> |
|||
{{out}} |
|||
<pre> 1 2 2 |
|||
3 5 7 |
|||
11 31 89 |
|||
27 103 659 |
|||
35 149 1181 |
|||
67 331 5021 |
|||
91 467 9923 |
|||
95 499 10909 |
|||
99 523 11941 |
|||
119 653 17959 |
|||
143 823 26879 |
|||
</pre> |
</pre> |
||
=={{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 761: | ||
=={{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 793: | ||
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 815: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
see "working..." + nl |
see "working..." + nl |
||
Line 637: | Line 837: | ||
see "done..." + nl |
see "done..." + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 654: | Line 854: | ||
143 823 26879 |
143 823 26879 |
||
done... |
done... |
||
</pre> |
|||
=={{header|RPL}}== |
|||
{{works with|HP|49}} |
|||
≪ → max |
|||
≪ { } 0 2 |
|||
1 max '''FOR''' j |
|||
SWAP OVER + SWAP |
|||
'''IF''' OVER ISPRIME? '''THEN''' |
|||
j OVER 4 PICK →V3 |
|||
4 ROLL SWAP + UNROT |
|||
'''END''' |
|||
NEXTPRIME NEXTPRIME |
|||
'''IF''' DUP max ≥ '''THEN''' max 'j' STO '''END''' |
|||
2 '''STEP''' |
|||
DROP2 |
|||
≫ ≫ '<span style="color:blue">TASK</span>' STO |
|||
1000 <span style="color:blue">TASK</span> |
|||
{{out}} |
|||
<pre> |
|||
1:. {[1. 2. 2.] [3. 5. 7.] [11. 31. 89.] [27. 103. 659.] [35. 149. 1181.] [67. 331. 5021.] [91. 467. 9923.] [95. 499. 10909.] [99. 523. 11941.] [119. 653. 17959.] [143. 823. 26879.]} |
|||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">require 'prime' |
||
sum = 0 |
sum = 0 |
||
Line 663: | Line 885: | ||
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 676: | Line 898: | ||
119 653 17959 |
119 653 17959 |
||
143 823 26879 |
143 823 26879 |
||
</pre> |
|||
=={{header|Sidef}}== |
|||
<syntaxhighlight lang="ruby">var sum = 0 |
|||
1e3.primes.each_kv {|k,v| |
|||
if (k+1 -> is_odd) { |
|||
sum += v |
|||
say "#{k+1} #{v} #{sum}" if sum.is_prime |
|||
} |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
1 2 2 |
|||
3 5 7 |
|||
11 31 89 |
|||
27 103 659 |
|||
35 149 1181 |
|||
67 331 5021 |
|||
91 467 9923 |
|||
95 499 10909 |
|||
99 523 11941 |
|||
119 653 17959 |
|||
143 823 26879 |
|||
</pre> |
</pre> |
||
=={{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 707: | Line 952: | ||
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 724: | Line 969: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
{{libheader|Wren- |
{{libheader|Wren-iterate}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="wren">import "./math" for Int |
||
import "/ |
import "./iterate" for Indexed |
||
import "/fmt" for Fmt |
import "./fmt" for Fmt |
||
var primes = Int.primeSieve(999) |
var primes = Int.primeSieve(999) |
||
Line 737: | Line 982: | ||
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 757: | Line 1,002: | ||
=={{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 778: | Line 1,023: | ||
]; |
]; |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 798: | Line 1,043: | ||
=={{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 811: | Line 1,056: | ||
end sub |
end sub |
||
print " |
print "i\tp(n)\tsum\n----\t-----\t------" |
||
for n = |
for n = 1 to 1000 |
||
if isPrime(n) then |
if isPrime(n) then |
||
i = i + 1 |
i = i + 1 |
||
if mod(i, 2) then |
if mod(i, 2) then |
||
sum = sum + n |
sum = sum + n |
||
if isPrime(sum) print n, "\t", sum |
if isPrime(sum) print i, "\t", n, "\t", sum |
||
end if |
end if |
||
end if |
end if |
||
next</ |
next</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>p(n) sum |
<pre>i p(n) sum |
||
---- ------ |
---- ----- ------ |
||
2 2 |
1 2 2 |
||
5 7 |
3 5 7 |
||
31 89 |
11 31 89 |
||
103 659 |
27 103 659 |
||
149 1181 |
35 149 1181 |
||
331 5021 |
67 331 5021 |
||
467 9923 |
91 467 9923 |
||
499 10909 |
95 499 10909 |
||
523 11941 |
99 523 11941 |
||
653 17959 |
119 653 17959 |
||
823 26879 |
143 823 26879 |
||
---Program done, press RETURN--- |
---Program done, press RETURN---</pre> |
||
</pre> |
Latest revision as of 22:11, 17 June 2024
Sum of primes in odd positions is prime is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
- Task
Let p(i) be a sequence of prime numbers. such that 2=p(1), 3=p(2), 5=p(3), ...
Consider the p(1),p(3),p(5), ... ,p(i), for each p(i) < 1,000 and i is odd.
Let sum be any prefix sum of these primesa.
If sum is prime then print i, p(i) and sum.
11l
F is_prime(n)
I n == 2
R 1B
I n < 2 | n % 2 == 0
R 0B
L(i) (3 .. Int(sqrt(n))).step(2)
I n % i == 0
R 0B
R 1B
print(‘ i p(i) sum’)
V idx = 0
V s = 0
V p = 1
L p < 1000
p++
I is_prime(p)
idx++
I idx % 2 != 0
s += p
I is_prime(s)
print(f:‘{idx:3} {p:3} {s:5}’)
- Output:
i p(i) sum 1 2 2 3 5 7 11 31 89 27 103 659 35 149 1181 67 331 5021 91 467 9923 95 499 10909 99 523 11941 119 653 17959 143 823 26879
Action!
INCLUDE "D2:PRINTF.ACT" ;from the Action! Tool Kit
BYTE FUNC IsPrime(CARD x)
CARD i,max
i=2 max=x/2
WHILE i<=max
DO
IF x MOD i=0 THEN
RETURN (0)
FI
i==+1
OD
RETURN (1)
PROC Main()
CARD x,count,sum
CHAR ARRAY s(6)
Put(125) PutE() ;clear the screen
PrintF("%3S%5S%6S%E","i","p(i)","sum")
count=0 sum=0
FOR x=2 TO 999
DO
IF IsPrime(x) THEN
count==+1
IF (count&1)=1 THEN
sum==+x
IF IsPrime(sum) THEN
StrC(count,s) PrintF("%3S",s)
StrC(x,s) PrintF("%5S",s)
StrC(sum,s) PrintF("%6S%E",s)
FI
FI
FI
OD
RETURN
- Output:
Screenshot from Atari 8-bit computer
i p(i) sum 1 2 2 3 5 7 11 31 89 27 103 659 35 149 1181 67 331 5021 91 467 9923 95 499 10909 99 523 11941 119 653 17959 143 823 26879
ALGOL 68
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;
[]BOOL prime = PRIMESIEVE 50 000; # guess that the max sum will be <= 50 000 #
[]INT low prime = EXTRACTPRIMESUPTO max prime FROMPRIMESIEVE prime; # get a list of primes up to max prime #
# find the sums of the odd primes and test for primality #
print( ( " i p[i] sum", newline ) );
INT odd prime sum := 0;
FOR i BY 2 TO UPB low prime DO
IF odd prime sum +:= low prime[ i ];
IF odd prime sum <= UPB prime
THEN
prime[ odd prime sum ]
ELSE
print( ( "Need more primes: ", whole( odd prime sum, 0 ), newline ) );
FALSE
FI
THEN
print( ( whole( i, -3 ), " ", whole( low prime[ i ], -4 ), " ", whole( odd prime sum, -6 ), newline ) )
FI
OD
END
- Output:
i p[i] sum 1 2 2 3 5 7 11 31 89 27 103 659 35 149 1181 67 331 5021 91 467 9923 95 499 10909 99 523 11941 119 653 17959 143 823 26879
Arturo
print " i | p(i) sum"
print repeat "-" 17
idx: 0
sm: 0
p: 1
while [p < 1000][
inc 'p
if prime? p [
inc 'idx
if odd? idx [
sm: sm + p
if prime? sm ->
print (pad to :string idx 4) ++ " | " ++ (pad to :string p 3) ++ (pad to :string sm 6)
]
]
]
- Output:
i | p(i) sum ----------------- 1 | 2 2 3 | 5 7 11 | 31 89 27 | 103 659 35 | 149 1181 67 | 331 5021 91 | 467 9923 95 | 499 10909 99 | 523 11941 119 | 653 17959 143 | 823 26879
AWK
# syntax: GAWK -f SUM_OF_PRIMES_IN_ODD_POSITIONS_IS_PRIME.AWK
# converted from Ring
BEGIN {
print(" i p sum")
print("------ ------ ------")
start = 2
stop = 999
for (i=start; i<=stop; i++) {
if (is_prime(i)) {
if (++nr % 2 == 1) {
sum += i
if (is_prime(sum)) {
count++
printf("%6d %6d %6d\n",nr,i,sum)
}
}
}
}
printf("Odd indexed primes %d-%d: %d\n",start,stop,count)
exit(0)
}
function is_prime(x, i) {
if (x <= 1) {
return(0)
}
for (i=2; i<=int(sqrt(x)); i++) {
if (x % i == 0) {
return(0)
}
}
return(1)
}
- Output:
i p sum ------ ------ ------ 1 2 2 3 5 7 11 31 89 27 103 659 35 149 1181 67 331 5021 91 467 9923 95 499 10909 99 523 11941 119 653 17959 143 823 26879 Odd indexed primes 2-999: 11
C
#include<stdio.h>
#include<stdlib.h>
int isprime( int p ) {
int i;
if(p==2) return 1;
if(!(p%2)) return 0;
for(i=3; i*i<=p; i+=2) {
if(!(p%i)) return 0;
}
return 1;
}
int main( void ) {
int s=0, p, i=1;
for(p=2;p<=999;p++) {
if(isprime(p)) {
if(i%2) {
s+=p;
if(isprime(s)) printf( "%d %d %d\n", i, p, s );
}
i+=1;
}
}
return 0;
}
Delphi
Uses the Delphi Prime-Generator Object
procedure SumOddPrimes(Memo: TMemo);
var Sieve: TPrimeSieve;
var I,Inx, Sum: integer;
begin
Sieve:=TPrimeSieve.Create;
try
Sieve.Intialize(100000);
Memo.Lines.Add(' I P(I) Sum');
Memo.Lines.Add('---------------');
I:=0;
Sum:=0;
while Sieve.Primes[I]<1000 do
begin
Sum:=Sum+Sieve.Primes[I];
if Sieve.Flags[Sum] then
begin
Memo.Lines.Add(Format('%3d %4d %6d',[I,Sieve.Primes[I],Sum]));
end;
Inc(I,2);
end;
finally Sieve.Free; end;
end;
- Output:
I P(I) Sum --------------- 0 2 2 2 5 7 10 31 89 26 103 659 34 149 1181 66 331 5021 90 467 9923 94 499 10909 98 523 11941 118 653 17959 142 823 26879 Elapsed Time: 16.077 ms.
EasyLang
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
for p = 2 to 999
if isprim p = 1
idx += 1
if idx mod 2 <> 0
s += p
if isprim s = 1
write "(" & idx & " " & p & " " & s & ") "
.
.
.
.
- Output:
(1 2 2) (3 5 7) (11 31 89) (27 103 659) (35 149 1181) (67 331 5021) (91 467 9923) (95 499 10909) (99 523 11941) (119 653 17959) (143 823 26879)
F#
This task uses Extensible Prime Generator (F#)
// 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}")
- Output:
i= 1 p[i]= 2 sum= 2 i= 3 p[i]= 5 sum= 7 i= 11 p[i]= 31 sum= 89 i= 27 p[i]=103 sum= 659 i= 35 p[i]=149 sum= 1181 i= 67 p[i]=331 sum= 5021 i= 91 p[i]=467 sum= 9923 i= 95 p[i]=499 sum=10909 i= 99 p[i]=523 sum=11941 i=119 p[i]=653 sum=17959 i=143 p[i]=823 sum=26879
Factor
USING: assocs assocs.extras kernel math.primes math.statistics
prettyprint sequences.extras ;
1000 primes-upto <evens> dup cum-sum zip [ prime? ] filter-values .
- Output:
{ { 2 2 } { 5 7 } { 31 89 } { 103 659 } { 149 1181 } { 331 5021 } { 467 9923 } { 499 10909 } { 523 11941 } { 653 17959 } { 823 26879 } }
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;
FreeBASIC
#include "isprime.bas"
dim as uinteger i = 1, p, sum = 0
for p = 2 to 999
if isprime(p) then
if i mod 2 = 1 then
sum += p
if isprime(sum) then print i, p, sum
end if
i = i + 1
end if
next p
Go
package main
import (
"fmt"
"rcu"
)
func main() {
primes := rcu.Primes(999)
sum := 0
fmt.Println(" i p[i] Σp[i]")
fmt.Println("----------------")
for i := 0; i < len(primes); i += 2 {
sum += primes[i]
if rcu.IsPrime(sum) {
fmt.Printf("%3d %3d %6s\n", i+1, primes[i], rcu.Commatize(sum))
}
}
}
- Output:
i p[i] Σp[i] ---------------- 1 2 2 3 5 7 11 31 89 27 103 659 35 149 1,181 67 331 5,021 91 467 9,923 95 499 10,909 99 523 11,941 119 653 17,959 143 823 26,879
GW-BASIC
10 S = 2
20 A = 1
30 PRINT 1, 2, 2
40 FOR P = 3 TO 999 STEP 2
50 GOSUB 90
60 IF Q=1 THEN GOSUB 190
70 NEXT P
80 END
90 Q=0
100 IF P=3 THEN Q=1:RETURN
110 IF P = 2 THEN Q = 1: RETURN
120 IF INT(P/2)*2= P THEN Q = 0: RETURN
130 I=1
140 I=I+2
150 IF INT(P/I)*I = P THEN RETURN
160 IF I*I<=P THEN GOTO 140
170 Q = 1
180 RETURN
190 A = A + 1
200 IF A MOD 2 = 0 THEN RETURN
210 S = S + P
220 T = P
230 P = S
240 GOSUB 90
250 IF Q = 1 THEN PRINT A, T, S
260 P = T
270 RETURN
J
(i,.p,.sum) #~ 1 p: sum=. +/\p=. p:<:i=. (#~ 2|])i. 1+p:inv 1000
1 2 2
3 5 7
11 31 89
27 103 659
35 149 1181
67 331 5021
91 467 9923
95 499 10909
99 523 11941
119 653 17959
143 823 26879
jq
Works with gojq, the Go implementation of jq See e.g. Erdős-primes#jq for a suitable implementation of `is_prime`.
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
def task:
[2, (range(3;1000;2)|select(is_prime))]
| [.[range(0;length;2)]]
| . as $oddPositionPrimes
| foreach range(0; length) as $i ({i: -1};
.i += 2
| .sum += $oddPositionPrimes[$i];
select(.sum|is_prime)
| "\(.i|lpad(3)) \($oddPositionPrimes[$i]|lpad(3)) \(.sum|lpad(5))" ) ;
" i p[$i] sum", task
- Output:
i p[$i] sum 1 2 2 3 5 7 11 31 89 27 103 659 35 149 1181 67 331 5021 91 467 9923 95 499 10909 99 523 11941 119 653 17959 143 823 26879
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"))
- Output:
(2, 2) (5, 7) (31, 89) (103, 659) (149, 1181) (331, 5021) (467, 9923) (499, 10909) (523, 11941) (653, 17959) (823, 26879)
Mathematica/Wolfram Language
p = Prime[Range[1, PrimePi[1000], 2]];
p = {p, Accumulate[p]} // Transpose;
Select[p, Last /* PrimeQ]
- Output:
{{2,2},{5,7},{31,89},{103,659},{149,1181},{331,5021},{467,9923},{499,10909},{523,11941},{653,17959},{823,26879}}
Nim
import strformat
template isOdd(n: Natural): bool = (n and 1) != 0
template isEven(n: Natural): bool = (n and 1) == 0
func isPrime(n: Positive): bool =
if n == 1: return false
if n.isEven: return n == 2
if n mod 3 == 0: return n == 3
var d = 5
while d * d <= n:
if n mod d == 0: return false
inc d, 2
if n mod d == 0: return false
inc d, 4
result = true
# Compute the sums of primes at odd position.
echo " i p(i) sum"
var idx = 0
var sum = 0
var p = 1
while p < 1000:
inc p
if p.isPrime:
inc idx
if idx.isOdd:
inc sum, p
if sum.isPrime:
echo &"{idx:3} {p:3} {sum:5}"
- Output:
i p(i) sum 1 2 2 3 5 7 11 31 89 27 103 659 35 149 1181 67 331 5021 91 467 9923 95 499 10909 99 523 11941 119 653 17959 143 823 26879
OCaml
let is_prime n =
let rec test x =
let q = n / x in x > q || x * q <> n && n mod (x + 2) <> 0 && test (x + 6)
in if n < 5 then n lor 1 = 3 else n land 1 <> 0 && n mod 3 <> 0 && test 5
let () = Seq.ints 3
|> Seq.filter is_prime
|> Seq.take_while ((>) 1000)
|> Seq.zip (Seq.ints 2)
|> Seq.filter (fun (i, _) -> i land 1 = 1)
|> Seq.scan (fun (_, pi, sum) (i, p) -> i, p, sum + p) (1, 2, 2)
|> Seq.filter (fun (_, _, sum) -> is_prime sum)
|> Seq.iter (fun (i, pi, sum) -> Printf.printf "p(%u) = %u, %u\n" i pi sum)
- Output:
p(1) = 2, 2 p(3) = 5, 7 p(11) = 31, 89 p(27) = 103, 659 p(35) = 149, 1181 p(67) = 331, 5021 p(91) = 467, 9923 p(95) = 499, 10909 p(99) = 523, 11941 p(119) = 653, 17959 p(143) = 823, 26879
PARI-GP
sm=0;for(ii=0, 83, oi=1+2*ii;sm=sm+prime(oi);if(isprime(sm),print(oi," ", prime(oi)," ",sm)))
- Output:
1 2 2 3 5 7 11 31 89 27 103 659 35 149 1181 67 331 5021 91 467 9923 95 499 10909 99 523 11941 119 653 17959 143 823 26879
Perl
use strict;
use warnings;
use ntheory 'is_prime';
my $c;
my @odd = grep { 0 != ++$c % 2 } grep { is_prime $_ } 2 .. 999;
my @sums = $odd[0];
push @sums, $sums[-1] + $_ for @odd[1..$#odd];
$c = 1;
for (0..$#sums) {
printf "%6d%6d%6d\n", $c, $odd[$_], $sums[$_] if is_prime $sums[$_];
$c += 2;
}
- Output:
1 2 2 3 5 7 11 31 89 27 103 659 35 149 1181 67 331 5021 91 467 9923 95 499 10909 99 523 11941 119 653 17959 143 823 26879
Phix
with javascript_semantics sequence primes = get_primes_le(1000) integer total = 0 printf(1," i p sum\n") printf(1,"----------------\n") for i=1 to length(primes) by 2 do total += primes[i] if is_prime(total) then printf(1,"%3d %3d %,6d\n", {i, primes[i], total}) end if end for
- Output:
i p sum ---------------- 1 2 2 3 5 7 11 31 89 27 103 659 35 149 1,181 67 331 5,021 91 467 9,923 95 499 10,909 99 523 11,941 119 653 17,959 143 823 26,879
Quackery
isprime
is defined at Primality by trial division#Quackery.
[ dip number$
over size -
space swap of
swap join echo$ ] is justify ( n n --> )
[] 1000 times
[ i^ isprime if [ i^ join ] ]
[] swap witheach
[ i^ 1 & 0 = iff join else drop ]
0 swap witheach
[ tuck + dup isprime if
[ i^ 2 * 1+ 3 justify
over 5 justify
dup 7 justify
cr ]
nip ]
drop
- Output:
1 2 2 3 5 7 11 31 89 27 103 659 35 149 1181 67 331 5021 91 467 9923 95 499 10909 99 523 11941 119 653 17959 143 823 26879
Raku
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 );
- Output:
1 2 2 3 5 7 11 31 89 27 103 659 35 149 1181 67 331 5021 91 467 9923 95 499 10909 99 523 11941 119 653 17959 143 823 26879
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.*/
call genP /*build array of semaphores for primes.*/
title= 'odd indexed primes the sum of the odd indexed primes'
say ' index │'center(title, 65)
say '───────┼'center("" , 65, '─')
found= 0 /*initialize # of odd indexed primes···*/
$= 0 /*sum of odd indexed primes (so far). */
do j=1 by 2; p= @.j; if p>hi then leave /*find odd indexed primes, sum = prime.*/
$= $ + p /*add this odd index prime to the sum. */
if \!.$ then iterate /*This sum not prime? Then skip it. */
found= found + 1 /*bump the number of solutions found. */
say center(j, 7)'│' right( commas(p), 13) right( commas($), 33)
end /*j*/
say '───────┴'center("" , 65, '─')
say
say 'Found ' commas(found) ' 'subword(title, 1, 3)
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: @.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */
!.=0; !.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " semaphores. */
#=5; sq.#= @.# ** 2 /*number of primes so far; prime². */
do j=@.#+2 by 2 to hi*33; parse var j '' -1 _ /*obtain the last decimal dig.*/
if _==5 then iterate; if j//3==0 then iterate; if j//7==0 then iterate
do k=5 while sq.k<=j /* [↓] divide by the known odd primes.*/
if j // @.k == 0 then iterate j /*Is J ÷ X? Then not prime. ___ */
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return
- output when using the default inputs:
index │ odd indexed primes the sum of the odd indexed primes ───────┼───────────────────────────────────────────────────────────────── 1 │ 2 2 3 │ 5 7 11 │ 31 89 27 │ 103 659 35 │ 149 1,181 67 │ 331 5,021 91 │ 467 9,923 95 │ 499 10,909 99 │ 523 11,941 119 │ 653 17,959 143 │ 823 26,879 ───────┴───────────────────────────────────────────────────────────────── Found 11 odd indexed primes
Ring
load "stdlib.ring"
see "working..." + nl
see "i p sum" + nl
nr = 0
sum = 0
limit = 1000
for n = 2 to limit
if isprime(n)
nr++
if nr%2 = 1
sum += n
if isprime(sum)
see "" + nr + " " + n + " " + sum + nl
ok
ok
ok
next
see "done..." + nl
- Output:
working... i p sum 1 2 2 3 5 7 11 31 89 27 103 659 35 149 1181 67 331 5021 91 467 9923 95 499 10909 99 523 11941 119 653 17959 143 823 26879 done...
RPL
≪ → max
≪ { } 0 2
1 max FOR j
SWAP OVER + SWAP
IF OVER ISPRIME? THEN
j OVER 4 PICK →V3
4 ROLL SWAP + UNROT
END
NEXTPRIME NEXTPRIME
IF DUP max ≥ THEN max 'j' STO END
2 STEP
DROP2
≫ ≫ 'TASK' STO
1000 TASK
- Output:
1:. {[1. 2. 2.] [3. 5. 7.] [11. 31. 89.] [27. 103. 659.] [35. 149. 1181.] [67. 331. 5021.] [91. 467. 9923.] [95. 499. 10909.] [99. 523. 11941.] [119. 653. 17959.] [143. 823. 26879.]}
Ruby
require 'prime'
sum = 0
Prime.each(1000).with_index(1).each_slice(2) do |(odd_i, i),(_)|
puts "%6d%6d%6d" % [i, odd_i, sum] if (sum += odd_i).prime?
end
- Output:
1 2 2 3 5 7 11 31 89 27 103 659 35 149 1181 67 331 5021 91 467 9923 95 499 10909 99 523 11941 119 653 17959 143 823 26879
Sidef
var sum = 0
1e3.primes.each_kv {|k,v|
if (k+1 -> is_odd) {
sum += v
say "#{k+1} #{v} #{sum}" if sum.is_prime
}
}
- Output:
1 2 2 3 5 7 11 31 89 27 103 659 35 149 1181 67 331 5021 91 467 9923 95 499 10909 99 523 11941 119 653 17959 143 823 26879
Tiny BASIC
LET I = 0
LET S = 0
LET P = 1
10 LET P = P + 1
LET X = P
GOSUB 100
IF Z = 1 THEN LET I = I + 1
IF Z = 0 THEN GOTO 20
IF (I/2)*2<>I THEN GOSUB 200
20 IF P<917 THEN GOTO 10 REM need to cheat a little to avoid overflow
END
100 REM is X a prime? Z=1 for yes, 0 for no
LET Z = 1
IF X = 3 THEN RETURN
IF X = 2 THEN RETURN
LET A = 1
110 LET A = A + 1
IF (X/A)*A = X THEN GOTO 120
IF A*A<=X THEN GOTO 110
RETURN
120 LET Z = 0
RETURN
200 LET S = S + P
LET X = S
GOSUB 100
IF Z = 1 THEN PRINT I," ", P," ", S
RETURN
- Output:
1 2 23 5 7 11 31 89 27 103 659 35 149 1181 67 331 5021 91 467 9923 95 499 10909 99 523 11941 119 653 17959 143 823 26879
Wren
import "./math" for Int
import "./iterate" for Indexed
import "./fmt" for Fmt
var primes = Int.primeSieve(999)
var sum = 0
System.print(" i p[i] Σp[i]")
System.print("----------------")
for (se in Indexed.new(primes, 2)) {
sum = sum + se.value
if (Int.isPrime(sum)) Fmt.print("$3d $3d $,6d", se.index + 1, se.value, sum)
}
- Output:
i p[i] Σp[i] ---------------- 1 2 2 3 5 7 11 31 89 27 103 659 35 149 1,181 67 331 5,021 91 467 9,923 95 499 10,909 99 523 11,941 119 653 17,959 143 823 26,879
XPL0
func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
for I:= 2 to sqrt(N) do
if rem(N/I) = 0 then return false;
return true;
];
int I, Sum, N;
[Text(0, "p(n) sum^m^j");
Sum:= 0; I:= 0;
for N:= 2 to 1000-1 do
[if IsPrime(N) then
[I:= I+1;
if I&1 then \odd
[Sum:= Sum + N;
if IsPrime(Sum) then
[IntOut(0, N); ChOut(0, ^ ); IntOut(0, Sum); CrLf(0)];
];
];
];
]
- Output:
p(n) sum 2 2 5 7 31 89 103 659 149 1181 331 5021 467 9923 499 10909 523 11941 653 17959 823 26879
Yabasic
// Rosetta Code problem: http://rosettacode.org/wiki/Sum_of_primes_in_odd_positions_is_prime
// by Galileo, 04/2022
sub isPrime(n)
local i
if n < 4 return n >= 2
for i = 2 to sqrt(n)
if not mod(n, i) return false
next
return true
end sub
print "i\tp(n)\tsum\n----\t-----\t------"
for n = 1 to 1000
if isPrime(n) then
i = i + 1
if mod(i, 2) then
sum = sum + n
if isPrime(sum) print i, "\t", n, "\t", sum
end if
end if
next
- Output:
i p(n) sum ---- ----- ------ 1 2 2 3 5 7 11 31 89 27 103 659 35 149 1181 67 331 5021 91 467 9923 95 499 10909 99 523 11941 119 653 17959 143 823 26879 ---Program done, press RETURN---
Categories:
- Draft Programming Tasks
- Prime Numbers
- 11l
- Action!
- Action! Tool Kit
- ALGOL 68
- ALGOL 68-primes
- Arturo
- AWK
- C
- Delphi
- SysUtils,StdCtrls
- EasyLang
- F Sharp
- Factor
- Fermat
- FreeBASIC
- Go
- Go-rcu
- GW-BASIC
- J
- Jq
- Julia
- Mathematica
- Wolfram Language
- Nim
- OCaml
- PARI-GP
- Perl
- Ntheory
- Phix
- Quackery
- Raku
- REXX
- Ring
- RPL
- Ruby
- Sidef
- Tiny BASIC
- Wren
- Wren-math
- Wren-iterate
- Wren-fmt
- XPL0
- Yabasic