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

m
(J)
m (→‎{{header|Wren}}: Minor tidy)
 
(10 intermediate revisions by 7 users not shown)
Line 146:
143 823 26879
</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}}==
Line 227 ⟶ 262:
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|F_Sharp|F#}}==
Line 359 ⟶ 447:
=={{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
Line 479 ⟶ 567:
119 653 17959
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}}==
Line 555 ⟶ 672:
119 653 17,959
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>
 
Line 669 ⟶ 823:
143 823 26879
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>
 
Line 762 ⟶ 938:
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-traititerate}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./traititerate" for Indexed
import "./fmt" for Fmt
 
var primes = Int.primeSieve(999)
9,485

edits