Summation of primes: Difference between revisions

no edit summary
(Added Quackery.)
No edit summary
 
(11 intermediate revisions by 11 users not shown)
Line 28:
LONG INT sum := 2;
FOR i FROM 3 BY 2 TO UPB prime DO
IF prime[ i ] THEN
sum +:= i
FI
Line 125:
 
sumPrimes below 2000000</syntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">print sum select 2..2000000 => prime?</syntaxhighlight>
 
{{out}}
 
<pre>142913828922</pre>
 
=={{header|AWK}}==
Line 289 ⟶ 297:
<pre>The sum of all primes below 2 million is 142913828923.
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
procedure SummationOfPrimes(Memo: TMemo);
var I: integer;
var Sum: int64;
var Sieve: TPrimeSieve;
begin
Sieve:=TPrimeSieve.Create;
try
Sieve.Intialize(2000000);
Sum:=0;
for I:=0 to Sieve.PrimeCount-1 do
Sum:=Sum+Sieve.Primes[I];
Memo.Lines.Add(Format('Sum of Primes Below 2 million: %.0n',[Sum+0.0]));
finally Sieve.Free; end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
Sum of Primes Below 2 million: 142,913,828,922
 
Elapsed Time: 17.405 ms.
</pre>
 
 
=={{header|F_Sharp|F#}}==
Line 315 ⟶ 355:
!!s;</syntaxhighlight>
{{out}}<pre>142913828922</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn IsPrime( n as NSUInteger ) as BOOL
BOOL isPrime = YES
NSUInteger i
if n < 2 then exit fn = NO
if n = 2 then exit fn = YES
if n mod 2 == 0 then exit fn = NO
for i = 3 to int(n^.5) step 2
if n mod i == 0 then exit fn = NO
next
end fn = isPrime
 
 
local fn SumOfPrimes as long
long sum = 2, i, n = 1
for i = 3 to 2000000 step 2
if ( fn IsPrime(i) )
sum += i
n++
end if
next
end fn = sum
 
print fn SumOfPrimes
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
142913828922
</pre>
 
 
=={{header|Go}}==
Line 351 ⟶ 427:
<pre>142913828922</pre>
 
 
=={{header|J}}==
<syntaxhighlight lang=J>+/p: i. p:inv 2e6
142913828922</syntaxhighlight>
 
Here, p: represents what might be thought of as an array of primes, and its argument represents array indices of those primes. Similarly, the result p:inv represents the smallest index whose prime is no less than its argument.
 
So...
<syntaxhighlight lang=J> p:inv 2e6
148933
p: p:inv 2e6
2000003</syntaxhighlight>
 
Also, <code>i. n</code> returns a list of indices starting at zero and ending at one less than n (assuming n is a positive integer which of course it is, here).
 
And, (for people not familiar with J): <code>+/</code> sums a list (evaluates the hypothetical expression which would result from insert <code>+</code> between each element of that list).
 
=={{header|jq}}==
Line 376 ⟶ 468:
142913828922
</pre>
 
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
block(primes(2,2000000),apply("+",%%));
</syntaxhighlight>
Output
<syntaxhighlight lang="maxima">
/* 142913828922 */
</syntaxhighlight>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">func isPrime(n: Natural): bool =
## Return true if "n" is prime.
## "n" is expected not to be a multiple of 2 or 3.
var k = 5
while k * k <= n:
if n mod k == 0 or n mod (k + 2) == 0: return false
inc k, 6
result = true
 
var sum = 2 + 3
var n = 5
while n < 2_000_000:
if n.isPrime:
inc sum, n
inc n, 2
if n.isPrime:
inc sum, n
inc n, 4
 
echo sum
</syntaxhighlight>
 
{{out}}
<pre>142913828922</pre>
 
=={{header|PARI/GP}}==
Line 387 ⟶ 515:
</pre>
=={{header|Pascal}}==
uses {{libheader|primTrialprimsieve}} [[Extensible_prime_generator#Pascal|Extensible_prime_generator]]
<syntaxhighlight lang="pascal">
program SumPrimes;
{$IFDEF FPC}{$MODE DELPHI}{$OPTIMIZATION ON,ALL}{$ENDIF}
{$ELSEIFDEF Windows} {$APPTYPE CONSOLE}{$ENDIF}
{$ENDIF}
uses
SysUtils,primTrialprimsieve;
var
p,sum : NativeInt;
begin
sum := actPrime0;
p := 0;
repeat inc(sum,p); p := NextPrime until p >= 2*1000*1000;
repeat inc(sum,p);p := Nextprime until p >= 2*1000*1000;
writeln(sum);
{$IFDEF WINDOWS} readln;{$ENDIF}
Line 571 ⟶ 699:
{{Out}}
<pre>142913828922</pre>
 
<code>eratosthenes</code> and <code>isprime</code> are defined at[[Sieve of Eratosthenes#Quackery]].
 
=={{header|Quackery}}==
 
<code>eratosthenes</code> and <code>isprime</code> are defined at[[Sieve of Eratosthenes#Quackery]].
 
<syntaxhighlight lang="Quackery"> 2000000 eratosthenes
Line 620 ⟶ 748:
142,913,828,922
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
≪ 0 1
'''WHILE''' NEXTPRIME DUP 2E6 <
'''REPEAT''' SWAP OVER + SWAP
'''END''' DROP
≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: 142913828922
</pre>
 
Line 626 ⟶ 766:
{{out}}
<pre>142913828922
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="Rust">use primes::is_prime ;
 
fn main() {
println!("{}" , (2u64..2000000u64).filter( | &d | is_prime( d )).sum::<u64>() ) ;
}</syntaxhighlight>
{{out}}
<pre>
142913828922
</pre>
 
Line 677 ⟶ 828:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int, Nums
import "./fmt" for Fmt
 
258

edits