Summation of primes: Difference between revisions
m
syntax highlighting fixup automation
(→{{header|Sidef}}: added a sublinear version) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 9:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<
PR read "primes.incl.a68" PR
# return s space-separated into groups of 3 digits #
Line 33:
OD;
print( ( space separate( whole( sum, 0 ) ), newline ) )
END</
{{out}}
<pre>
Line 43:
This isn't something that's likely to needed more than once — if at all — so you'd probably just throw together code like the following. The result's interesting in that although it's way outside AppleScript's integer range, its class is returned as integer in macOS 10.14 (Mojave)!
<
if ((n < 4) or (n is 5)) then return (n > 1)
if ((n mod 2 = 0) or (n mod 3 = 0) or (n mod 5 = 0)) then return false
Line 67:
end sumPrimes
sumPrimes below 2000000</
{{output}}
<syntaxhighlight lang
The result can be obtained in 4 seconds rather than 14 if the summing's instead combined with an Eratosthenean sieve:
<
set limit to this - 1
-- Is the limit 2 or lower?
Line 124:
end sumPrimes
sumPrimes below 2000000</
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SUMMATION_OF_PRIMES.AWK
BEGIN {
Line 161:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 170:
=={{header|BASIC}}==
==={{header|FreeBASIC}}===
<
dim as integer sum = 2, i, n=1
Line 180:
next i
print sum</
{{out}}<pre>142913828922</pre>
==={{header|GW-BASIC}}===
<
20 FOR P = 3 TO 1999999! STEP 2
30 GOSUB 80
Line 199:
140 Q = 1
150 RETURN
</syntaxhighlight>
{{out}}<pre>142913828922</pre>
=={{header|C}}==
<
#include<stdlib.h>
Line 224:
printf( "%ld\n", s );
return 0;
}</
{{out}}<pre>142913828922</pre>
=={{header|CLU}}==
<
x0: int := s/2
if x0=0 then
Line 263:
start_up = proc ()
stream$putl(stream$primary_output(), int$unparse(sum_primes_to(2000000)))
end start_up </
{{out}}
<pre>142913828922</pre>
=={{header|Crystal}}==
<
return false unless (n | 1 == 3 if n < 5) || (n % 6) | 4 == 5
sqrt = Math.isqrt(n)
Line 285:
puts "The sum of all primes below 2 million is #{(0i64..2000000i64).sum { |n| prime?(n) ? n : 0u64 }}"
</syntaxhighlight>
{{out}}
<pre>The sum of all primes below 2 million is 142913828923.
Line 292:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<
// Summation of primes. Nigel Galloway: November 9th., 2021
printfn $"%d{primes64()|>Seq.takeWhile((>)2000000L)|>Seq.sum}"
</syntaxhighlight>
{{out}}
<pre>
Line 302:
=={{header|Factor}}==
<
2,000,000 primes-upto sum .</
{{out}}
<pre>
Line 311:
=={{header|Fermat}}==
<
for p=3 to 1999999 by 2 do if Isprime(p) then s:=s+p fi od;
!!s;</
{{out}}<pre>142913828922</pre>
=={{header|Go}}==
{{libheader|Go-rcu}}
<
import (
Line 331:
}
fmt.Printf("The sum of all primes below 2 million is %s.\n", rcu.Commatize(sum))
}</
{{out}}
Line 340:
=={{header|Haskell}}==
<
sumOfPrimesBelow :: Integral a => a -> a
Line 347:
main :: IO ()
main = print $ sumOfPrimesBelow 2000000</
{{Out}}
<pre>142913828922</pre>
Line 357:
See [[Erd%C5%91s-primes#jq]] for a suitable definition of `is_prime/1` as used here.
<
sum(2, range(3 ; 2E6; 2) | select(is_prime))</
{{out}}
<pre>
Line 366:
=={{header|Julia}}==
<
@show sum(primes(2_000_000)) # sum(primes(2000000)) = 142913828922
</syntaxhighlight>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
{{out}}<pre>
Line 378:
=={{header|PARI/GP}}==
<
s=2; p=3
while(p<2000000,if(isprime(p),s=s+p);p=p+2)
print(s)
</syntaxhighlight>
{{out}}<pre>
142913828922
Line 388:
=={{header|Pascal}}==
uses {{libheader|primTrial}}
<
program SumPrimes;
{$IFDEF FPC}{$MODE DELPHI}{$OPTIMIZATION ON,ALL}
Line 402:
writeln(sum);
{$IFDEF WINDOWS} readln;{$ENDIF}
end.</
{{out}}
<pre>
Line 408:
=={{header|Perl}}==
<
use strict; # https://rosettacode.org/wiki/Summation_of_primes
Line 415:
use List::Util qw( sum );
print sum( @{ primes( 2e6 ) } ), "\n";</
{{out}}
<pre>
Line 422:
=={{header|Phix}}==
<!--<
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The sum of primes below 2 million is %,d\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2e6</span><span style="color: #0000FF;">)))</span>
<!--</
{{out}}
<pre>
Line 433:
=={{header|Python}}==
===Procedural===
<
def isPrime(n):
Line 448:
suma += i
n+=1
print(suma)</
{{out}}
<pre>142913828922</pre>
===Functional===
<
from functools import reduce
Line 497:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>142913828922</pre>
Line 504:
Or, more efficiently, assuming that we have a generator of primes:
<
from itertools import count, takewhile
Line 568:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>142913828922</pre>
Line 574:
=={{header|Raku}}==
Slow, but only using compiler built-ins (about 5 seconds)
<syntaxhighlight lang="raku"
{{out}}
<pre>142913828922</pre>
Much faster using external libraries (well under half a second)
<syntaxhighlight lang="raku"
my $sieve = Math::Primesieve.new;
say sum $sieve.primes(2e6.Int);</
Same output
=={{header|Ring}}==
<
load "stdlib.ring"
see "working..." + nl
Line 600:
see "" + sum + nl
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 610:
=={{header|Ruby}}==
<syntaxhighlight lang
{{out}}
<pre>142913828922
Line 618:
Built-in:
<
Linear algorithm:
<
var sum = 0
for (var p = 2; p < limit; p.next_prime!) {
Line 629:
}
say sum_primes(2e6)</
Sublinear algorithm:
<
return 0 if (n <= 1)
Line 655:
}
say sum_of_primes(2e6)</
{{out}}
<pre>
Line 664:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<
import "./fmt" for Fmt
Fmt.print("The sum of all primes below 2 million is $,d.", Nums.sum(Int.primeSieve(2e6-1)))</
{{out}}
Line 676:
=={{header|XPL0}}==
Takes 3.7 seconds on Pi4.
<
int N, I;
[if (N&1) = 0 then return false; \N is even
Line 693:
Format(1, 0); \don't show places after decimal point
RlOut(0, Sum);
]</
{{out}}
Line 702:
=={{header|Yabasic}}==
{{trans|Python}}
<
// by Galileo, 04/2022
Line 718:
if isPrime(i) suma = suma + i
next
print str$(suma, "%12.f")</
|