Sieve of Eratosthenes: Difference between revisions

Content added Content deleted
m (Sieve of Eratosthenes page fix highlighting...)
m (syntax highlighting fixup automation)
Line 33: Line 33:
{{trans|Python}}
{{trans|Python}}


<syntaxhighlight lang=11l>F primes_upto(limit)
<syntaxhighlight lang="11l">F primes_upto(limit)
V is_prime = [0B]*2 [+] [1B]*(limit - 1)
V is_prime = [0B]*2 [+] [1B]*(limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
Line 50: Line 50:
=={{header|360 Assembly}}==
=={{header|360 Assembly}}==
For maximum compatibility, this program uses only the basic instruction set.
For maximum compatibility, this program uses only the basic instruction set.
<syntaxhighlight lang=360_Assembly>* Sieve of Eratosthenes
<syntaxhighlight lang="360_assembly">* Sieve of Eratosthenes
ERATOST CSECT
ERATOST CSECT
USING ERATOST,R12
USING ERATOST,R12
Line 168: Line 168:
=={{header|6502 Assembly}}==
=={{header|6502 Assembly}}==
If this subroutine is called with the value of <i>n</i> in the accumulator, it will store an array of the primes less than <i>n</i> beginning at address 1000 hex and return the number of primes it has found in the accumulator.
If this subroutine is called with the value of <i>n</i> in the accumulator, it will store an array of the primes less than <i>n</i> beginning at address 1000 hex and return the number of primes it has found in the accumulator.
<syntaxhighlight lang=6502asm>ERATOS: STA $D0 ; value of n
<syntaxhighlight lang="6502asm">ERATOS: STA $D0 ; value of n
LDA #$00
LDA #$00
LDX #$00
LDX #$00
Line 215: Line 215:
Some of the macro code is derived from the examples included with EASy68K.
Some of the macro code is derived from the examples included with EASy68K.
See 68000 "100 Doors" listing for additional information.
See 68000 "100 Doors" listing for additional information.
<syntaxhighlight lang=68000devpac>*-----------------------------------------------------------
<syntaxhighlight lang="68000devpac">*-----------------------------------------------------------
* Title : BitSieve
* Title : BitSieve
* Written by : G. A. Tippery
* Written by : G. A. Tippery
Line 474: Line 474:
=={{header|8086 Assembly}}==
=={{header|8086 Assembly}}==


<syntaxhighlight lang=asm>MAXPRM: equ 5000 ; Change this value for more primes
<syntaxhighlight lang="asm">MAXPRM: equ 5000 ; Change this value for more primes
cpu 8086
cpu 8086
bits 16
bits 16
Line 548: Line 548:


=={{header|8th}}==
=={{header|8th}}==
<syntaxhighlight lang=8th>
<syntaxhighlight lang="8th">
with: n
with: n


Line 598: Line 598:
=={{header|AArch64 Assembly}}==
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang=AArch64 Assembly>
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program cribleEras64.s */
/* program cribleEras64.s */
Line 718: Line 718:


=={{header|ABAP}}==
=={{header|ABAP}}==
<syntaxhighlight lang=Lisp>
<syntaxhighlight lang="lisp">
PARAMETERS: p_limit TYPE i OBLIGATORY DEFAULT 100.
PARAMETERS: p_limit TYPE i OBLIGATORY DEFAULT 100.


Line 763: Line 763:


=={{header|ACL2}}==
=={{header|ACL2}}==
<syntaxhighlight lang=Lisp>(defun nats-to-from (n i)
<syntaxhighlight lang="lisp">(defun nats-to-from (n i)
(declare (xargs :measure (nfix (- n i))))
(declare (xargs :measure (nfix (- n i))))
(if (zp (- n i))
(if (zp (- n i))
Line 795: Line 795:


=={{header|Action!}}==
=={{header|Action!}}==
<syntaxhighlight lang=Action!>DEFINE MAX="1000"
<syntaxhighlight lang="action!">DEFINE MAX="1000"


PROC Main()
PROC Main()
Line 841: Line 841:
=={{header|ActionScript}}==
=={{header|ActionScript}}==
Works with ActionScript 3.0 (this is utilizing the actions panel, not a separated class file)
Works with ActionScript 3.0 (this is utilizing the actions panel, not a separated class file)
<syntaxhighlight lang=actionscript>function eratosthenes(limit:int):Array
<syntaxhighlight lang="actionscript">function eratosthenes(limit:int):Array
{
{
var primes:Array = new Array();
var primes:Array = new Array();
Line 873: Line 873:
=={{header|Ada}}==
=={{header|Ada}}==


<syntaxhighlight lang=Ada>with Ada.Text_IO, Ada.Command_Line;
<syntaxhighlight lang="ada">with Ada.Text_IO, Ada.Command_Line;


procedure Eratos is
procedure Eratos is
Line 906: Line 906:
=={{header|Agda}}==
=={{header|Agda}}==


<syntaxhighlight lang=agda>
<syntaxhighlight lang="agda">
-- imports
-- imports
open import Data.Nat as ℕ using (ℕ; suc; zero; _+_; _∸_)
open import Data.Nat as ℕ using (ℕ; suc; zero; _+_; _∸_)
Line 957: Line 957:
=={{header|Agena}}==
=={{header|Agena}}==
Tested with Agena 2.9.5 Win32
Tested with Agena 2.9.5 Win32
<syntaxhighlight lang=agena># Sieve of Eratosthenes
<syntaxhighlight lang="agena"># Sieve of Eratosthenes


# generate and return a sequence containing the primes up to sieveSize
# generate and return a sequence containing the primes up to sieveSize
Line 1,025: Line 1,025:


'''Works with:''' ALGOL 60 for OS/360
'''Works with:''' ALGOL 60 for OS/360
<syntaxhighlight lang=algol60>'BEGIN'
<syntaxhighlight lang="algol60">'BEGIN'
'INTEGER' 'ARRAY' CANDIDATES(/0..1000/);
'INTEGER' 'ARRAY' CANDIDATES(/0..1000/);
'INTEGER' I,J,K;
'INTEGER' I,J,K;
Line 1,067: Line 1,067:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<syntaxhighlight lang=algol68>BOOL prime = TRUE, non prime = FALSE;
<syntaxhighlight lang="algol68">BOOL prime = TRUE, non prime = FALSE;
PROC eratosthenes = (INT n)[]BOOL:
PROC eratosthenes = (INT n)[]BOOL:
(
(
Line 1,092: Line 1,092:
=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
=== Standard, non-optimised sieve ===
=== Standard, non-optimised sieve ===
<syntaxhighlight lang=algolw>begin
<syntaxhighlight lang="algolw">begin


% implements the sieve of Eratosthenes %
% implements the sieve of Eratosthenes %
Line 1,143: Line 1,143:


Alternative version that only stores odd numbers greater than 1 in the sieve.
Alternative version that only stores odd numbers greater than 1 in the sieve.
<syntaxhighlight lang=algolw>begin
<syntaxhighlight lang="algolw">begin
% implements the sieve of Eratosthenes %
% implements the sieve of Eratosthenes %
% only odd numbers appear in the sieve, which starts at 3 %
% only odd numbers appear in the sieve, which starts at 3 %
Line 1,180: Line 1,180:


=={{header|ALGOL-M}}==
=={{header|ALGOL-M}}==
<syntaxhighlight lang=algol>
<syntaxhighlight lang="algol">
BEGIN
BEGIN


Line 1,274: Line 1,274:
=== Non-Optimized Version ===
=== Non-Optimized Version ===


<syntaxhighlight lang=apl>sieve2←{
<syntaxhighlight lang="apl">sieve2←{
b←⍵⍴1
b←⍵⍴1
b[⍳2⌊⍵]←0
b[⍳2⌊⍵]←0
Line 1,289: Line 1,289:
=== Optimized Version ===
=== Optimized Version ===


<syntaxhighlight lang=apl>sieve←{
<syntaxhighlight lang="apl">sieve←{
b←⍵⍴{∧⌿↑(×/⍵)⍴¨~⍵↑¨1}2 3 5
b←⍵⍴{∧⌿↑(×/⍵)⍴¨~⍵↑¨1}2 3 5
b[⍳6⌊⍵]←(6⌊⍵)⍴0 0 1 1 0 1
b[⍳6⌊⍵]←(6⌊⍵)⍴0 0 1 1 0 1
Line 1,307: Line 1,307:
=== Examples ===
=== Examples ===


<syntaxhighlight lang=apl> primes 100
<syntaxhighlight lang="apl"> primes 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97


Line 1,326: Line 1,326:
=={{header|AppleScript}}==
=={{header|AppleScript}}==


<syntaxhighlight lang=applescript>on sieveOfEratosthenes(limit)
<syntaxhighlight lang="applescript">on sieveOfEratosthenes(limit)
script o
script o
property numberList : {missing value}
property numberList : {missing value}
Line 1,348: Line 1,348:


{{out}}
{{out}}
<syntaxhighlight lang=applescript>{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997}</syntaxhighlight>
<syntaxhighlight lang="applescript">{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997}</syntaxhighlight>


=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang=ARM Assembly>
<syntaxhighlight lang="arm assembly">


/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
Line 1,477: Line 1,477:
=={{header|Arturo}}==
=={{header|Arturo}}==


<syntaxhighlight lang=rebol>sieve: function [upto][
<syntaxhighlight lang="rebol">sieve: function [upto][
composites: array.of: inc upto false
composites: array.of: inc upto false
loop 2..to :integer sqrt upto 'x [
loop 2..to :integer sqrt upto 'x [
Line 1,501: Line 1,501:
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
{{AutoHotkey case}}Source: [http://www.autohotkey.com/forum/topic44657.html AutoHotkey forum] by Laszlo
{{AutoHotkey case}}Source: [http://www.autohotkey.com/forum/topic44657.html AutoHotkey forum] by Laszlo
<syntaxhighlight lang=autohotkey>MsgBox % "12345678901234567890`n" Sieve(20)
<syntaxhighlight lang="autohotkey">MsgBox % "12345678901234567890`n" Sieve(20)


Sieve(n) { ; Sieve of Eratosthenes => string of 0|1 chars, 1 at position k: k is prime
Sieve(n) { ; Sieve of Eratosthenes => string of 0|1 chars, 1 at position k: k is prime
Line 1,519: Line 1,519:


===Alternative Version===
===Alternative Version===
<syntaxhighlight lang=AutoHotkey>Sieve_of_Eratosthenes(n){
<syntaxhighlight lang="autohotkey">Sieve_of_Eratosthenes(n){
arr := []
arr := []
loop % n-1
loop % n-1
Line 1,534: Line 1,534:
return Arr
return Arr
}</syntaxhighlight>
}</syntaxhighlight>
Examples:<syntaxhighlight lang=AutoHotkey>n := 101
Examples:<syntaxhighlight lang="autohotkey">n := 101
Arr := Sieve_of_Eratosthenes(n)
Arr := Sieve_of_Eratosthenes(n)
loop, % n-1
loop, % n-1
Line 1,553: Line 1,553:


=={{header|AutoIt}}==
=={{header|AutoIt}}==
<syntaxhighlight lang=autoit>#include <Array.au3>
<syntaxhighlight lang="autoit">#include <Array.au3>
$M = InputBox("Integer", "Enter biggest Integer")
$M = InputBox("Integer", "Enter biggest Integer")
Global $a[$M], $r[$M], $c = 1
Global $a[$M], $r[$M], $c = 1
Line 1,592: Line 1,592:
input from commandline as well as stdin,
input from commandline as well as stdin,
and input is checked for valid numbers:
and input is checked for valid numbers:
<syntaxhighlight lang=awk>
<syntaxhighlight lang="awk">
# usage: gawk -v n=101 -f sieve.awk
# usage: gawk -v n=101 -f sieve.awk


Line 1,614: Line 1,614:


Here is an alternate version that uses an associative array to record composites with a prime dividing it. It can be considered a slow version, as it does not cross out composites until needed. This version assumes enough memory to hold all primes up to ULIMIT. It prints out noncomposites greater than 1.
Here is an alternate version that uses an associative array to record composites with a prime dividing it. It can be considered a slow version, as it does not cross out composites until needed. This version assumes enough memory to hold all primes up to ULIMIT. It prints out noncomposites greater than 1.
<syntaxhighlight lang=awk>
<syntaxhighlight lang="awk">
BEGIN { ULIMIT=100
BEGIN { ULIMIT=100


Line 1,634: Line 1,634:
{{works with|FreeBASIC}}
{{works with|FreeBASIC}}
{{works with|RapidQ}}
{{works with|RapidQ}}
<syntaxhighlight lang=freebasic>DIM n AS Integer, k AS Integer, limit AS Integer
<syntaxhighlight lang="freebasic">DIM n AS Integer, k AS Integer, limit AS Integer


INPUT "Enter number to search to: "; limit
INPUT "Enter number to search to: "; limit
Line 1,653: Line 1,653:


==={{header|Applesoft BASIC}}===
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang=basic>10 INPUT "ENTER NUMBER TO SEARCH TO: ";LIMIT
<syntaxhighlight lang="basic">10 INPUT "ENTER NUMBER TO SEARCH TO: ";LIMIT
20 DIM FLAGS(LIMIT)
20 DIM FLAGS(LIMIT)
30 FOR N = 2 TO SQR (LIMIT)
30 FOR N = 2 TO SQR (LIMIT)
Line 1,669: Line 1,669:
{{trans|Commodore BASIC}}
{{trans|Commodore BASIC}}
Auto-initialization of arrays is not reliable, so we have to do our own. Also, PRINTing with commas doesn't quite format as nicely as one might hope, so we do a little extra work to keep the columns lined up.
Auto-initialization of arrays is not reliable, so we have to do our own. Also, PRINTing with commas doesn't quite format as nicely as one might hope, so we do a little extra work to keep the columns lined up.
<syntaxhighlight lang=basic>100 REM SIEVE OF ERATOSTHENES
<syntaxhighlight lang="basic">100 REM SIEVE OF ERATOSTHENES
110 PRINT "LIMIT";:INPUT LI
110 PRINT "LIMIT";:INPUT LI
120 DIM N(LI):FOR I=0 TO LI:N(I)=1:NEXT I
120 DIM N(LI):FOR I=0 TO LI:N(I)=1:NEXT I
Line 1,703: Line 1,703:
==={{header|Commodore BASIC}}===
==={{header|Commodore BASIC}}===
Since C= BASIC initializes arrays to all zeroes automatically, we avoid needing our own initialization loop by simply letting 0 mean prime and using 1 for composite.
Since C= BASIC initializes arrays to all zeroes automatically, we avoid needing our own initialization loop by simply letting 0 mean prime and using 1 for composite.
<syntaxhighlight lang=basic>100 REM SIEVE OF ERATOSTHENES
<syntaxhighlight lang="basic">100 REM SIEVE OF ERATOSTHENES
110 INPUT "LIMIT";LI
110 INPUT "LIMIT";LI
120 DIM N(LI)
120 DIM N(LI)
Line 1,736: Line 1,736:


==={{header|IS-BASIC}}===
==={{header|IS-BASIC}}===
<syntaxhighlight lang=IS-BASIC>100 PROGRAM "Sieve.bas"
<syntaxhighlight lang="is-basic">100 PROGRAM "Sieve.bas"
110 LET LIMIT=100
110 LET LIMIT=100
120 NUMERIC T(1 TO LIMIT)
120 NUMERIC T(1 TO LIMIT)
Line 1,754: Line 1,754:


==={{header|Locomotive Basic}}===
==={{header|Locomotive Basic}}===
<syntaxhighlight lang=locobasic>10 DEFINT a-z
<syntaxhighlight lang="locobasic">10 DEFINT a-z
20 INPUT "Limit";limit
20 INPUT "Limit";limit
30 DIM f(limit)
30 DIM f(limit)
Line 1,768: Line 1,768:


==={{header|MSX Basic}}===
==={{header|MSX Basic}}===
<syntaxhighlight lang=MSX Basic>5 REM Tested with MSXPen web emulator
<syntaxhighlight lang="msx basic">5 REM Tested with MSXPen web emulator
6 REM Translated from Rosetta's ZX Spectrum implementation
6 REM Translated from Rosetta's ZX Spectrum implementation
10 INPUT "Enter number to search to: ";l
10 INPUT "Enter number to search to: ";l
Line 1,787: Line 1,787:


A note on <code>FAST</code> and <code>SLOW</code>: under normal circumstances the CPU spends about 3/4 of its time driving the display and only 1/4 doing everything else. Entering <code>FAST</code> mode blanks the screen (which we do not want to update anyway), resulting in substantially improved performance; we then return to <code>SLOW</code> mode when we have something to print out.
A note on <code>FAST</code> and <code>SLOW</code>: under normal circumstances the CPU spends about 3/4 of its time driving the display and only 1/4 doing everything else. Entering <code>FAST</code> mode blanks the screen (which we do not want to update anyway), resulting in substantially improved performance; we then return to <code>SLOW</code> mode when we have something to print out.
<syntaxhighlight lang=basic> 10 INPUT L
<syntaxhighlight lang="basic"> 10 INPUT L
20 FAST
20 FAST
30 DIM N(L)
30 DIM N(L)
Line 1,802: Line 1,802:


==={{header|ZX Spectrum Basic}}===
==={{header|ZX Spectrum Basic}}===
<syntaxhighlight lang=zxbasic>10 INPUT "Enter number to search to: ";l
<syntaxhighlight lang="zxbasic">10 INPUT "Enter number to search to: ";l
20 DIM p(l)
20 DIM p(l)
30 FOR n=2 TO SQR l
30 FOR n=2 TO SQR l
Line 1,818: Line 1,818:
{{works with|QBasic|1.1}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang=qbasic>limit = 120
<syntaxhighlight lang="qbasic">limit = 120


DIM flags(limit)
DIM flags(limit)
Line 1,842: Line 1,842:


==={{header|BASIC256}}===
==={{header|BASIC256}}===
<syntaxhighlight lang=BASIC256>arraybase 1
<syntaxhighlight lang="basic256">arraybase 1
limit = 120
limit = 120


Line 1,868: Line 1,868:
==={{header|True BASIC}}===
==={{header|True BASIC}}===
{{trans|QBasic}}
{{trans|QBasic}}
<syntaxhighlight lang=qbasic>LET limit = 120
<syntaxhighlight lang="qbasic">LET limit = 120
DIM flags(0)
DIM flags(0)
MAT redim flags(limit)
MAT redim flags(limit)
Line 1,893: Line 1,893:
{{trans|ZX Spectrum Basic}}
{{trans|ZX Spectrum Basic}}
Sets h$ to 1 for higher multiples of 2 via <code>FILL$</code>, later on sets <code>STEP</code> to 2n; replaces Floating Pt array p(z) with string variable h$(z) to sieve out all primes < z=441 (<code>l</code>=21) in under 1K, so that h$ is fillable to its maximum (32766), even on a 48K ZX Spectrum if translated back.
Sets h$ to 1 for higher multiples of 2 via <code>FILL$</code>, later on sets <code>STEP</code> to 2n; replaces Floating Pt array p(z) with string variable h$(z) to sieve out all primes < z=441 (<code>l</code>=21) in under 1K, so that h$ is fillable to its maximum (32766), even on a 48K ZX Spectrum if translated back.
<syntaxhighlight lang=qbasic>
<syntaxhighlight lang="qbasic">
10 INPUT "Enter Stopping Pt for squared factors: ";z
10 INPUT "Enter Stopping Pt for squared factors: ";z
15 LET l=SQR(z)
15 LET l=SQR(z)
Line 1,906: Line 1,906:


====2i wheel emulation of Sinclair ZX81 BASIC====
====2i wheel emulation of Sinclair ZX81 BASIC====
Backward-compatible also on Spectrums, as well as 1K ZX81s for all primes < Z=441. N.B. the <code>STEP</code> of 2 in line 40 mitigates line 50's inefficiency when going to 90. <syntaxhighlight lang=qbasic>
Backward-compatible also on Spectrums, as well as 1K ZX81s for all primes < Z=441. N.B. the <code>STEP</code> of 2 in line 40 mitigates line 50's inefficiency when going to 90. <syntaxhighlight lang="qbasic">
10 INPUT Z
10 INPUT Z
15 LET L=SQR(Z)
15 LET L=SQR(Z)
Line 1,926: Line 1,926:
====2i wheel emulation of Sinclair ZX80 BASIC====
====2i wheel emulation of Sinclair ZX80 BASIC====
. . . with 2:1 compression (of 16-bit integer variables on ZX80s) such that it obviates having to account for any multiple of 2; one has to input odd upper limits on factors to be squared, <code>L</code> (=21 at most on 1K ZX80s for all primes till 439).
. . . with 2:1 compression (of 16-bit integer variables on ZX80s) such that it obviates having to account for any multiple of 2; one has to input odd upper limits on factors to be squared, <code>L</code> (=21 at most on 1K ZX80s for all primes till 439).
Backward-compatible on ZX80s after substituting ** for ^ in line 120.<syntaxhighlight lang=qbasic>
Backward-compatible on ZX80s after substituting ** for ^ in line 120.<syntaxhighlight lang="qbasic">
10 INPUT L
10 INPUT L
15 LET Z=(L+1)*(L- 1)/2
15 LET Z=(L+1)*(L- 1)/2
Line 1,946: Line 1,946:
duplicate entries are omitted. Thus, a slightly transformed Sieve of Sundaram is what Eratosthenes' Sieve becomes upon applying all
duplicate entries are omitted. Thus, a slightly transformed Sieve of Sundaram is what Eratosthenes' Sieve becomes upon applying all
optimisations incorporated into the prior entries for QL SuperBASIC, except for any equivalent to line 50 in them.
optimisations incorporated into the prior entries for QL SuperBASIC, except for any equivalent to line 50 in them.
Backward-compatible on 1K ZX80s for all primes < 441 (O=10) after substituting ** for ^ in line 120.<syntaxhighlight lang=qbasic>
Backward-compatible on 1K ZX80s for all primes < 441 (O=10) after substituting ** for ^ in line 120.<syntaxhighlight lang="qbasic">
10 INPUT O
10 INPUT O
15 LET Z=2*O*O+O*2
15 LET Z=2*O*O+O*2
Line 1,965: Line 1,965:
====Eulerian optimisation====
====Eulerian optimisation====
While slower than the optimised Sieve of Eratosthenes before it, the Sieve of Sundaram above has a compatible compression scheme that's more convenient than the conventional one used beforehand. It is therefore applied below along with Euler's alternative optimisation in a reversed implementation that lacks backward-compatibility to ZX80 BASIC. This program is designed around features & limitations of the QL, yet can be rewritten more efficiently for 1K ZX80s, as they allow integer variables to be parameters of <code>FOR</code> statements (& as their 1K of static RAM is equivalent to L1 cache, even in <code>FAST</code> mode). That's left as an exercise for ZX80 enthusiasts, who for o%=14 should be able to generate all primes < 841, i.e. 3 orders of (base 2) magnitude above the limit for the program listed under Sinclair ZX81 BASIC. In QL SuperBASIC, o% may at most be 127--generating all primes < 65,025 (almost 2x the upper limit for indices & integer variables used to calculate them ~2x faster than for floating point as used in line 30, after which the integer code mimics an assembly algorithm for the QL's 68008.)
While slower than the optimised Sieve of Eratosthenes before it, the Sieve of Sundaram above has a compatible compression scheme that's more convenient than the conventional one used beforehand. It is therefore applied below along with Euler's alternative optimisation in a reversed implementation that lacks backward-compatibility to ZX80 BASIC. This program is designed around features & limitations of the QL, yet can be rewritten more efficiently for 1K ZX80s, as they allow integer variables to be parameters of <code>FOR</code> statements (& as their 1K of static RAM is equivalent to L1 cache, even in <code>FAST</code> mode). That's left as an exercise for ZX80 enthusiasts, who for o%=14 should be able to generate all primes < 841, i.e. 3 orders of (base 2) magnitude above the limit for the program listed under Sinclair ZX81 BASIC. In QL SuperBASIC, o% may at most be 127--generating all primes < 65,025 (almost 2x the upper limit for indices & integer variables used to calculate them ~2x faster than for floating point as used in line 30, after which the integer code mimics an assembly algorithm for the QL's 68008.)
<syntaxhighlight lang=qbasic>
<syntaxhighlight lang="qbasic">
10 INPUT "Enter highest value of diagonal index q%: ";o%
10 INPUT "Enter highest value of diagonal index q%: ";o%
15 LET z%=o%*(2+o%*2) : h$=FILL$(" ",z%+o%) : q%=1 : q=q% : m=z% DIV (2*q%+1)
15 LET z%=o%*(2+o%*2) : h$=FILL$(" ",z%+o%) : q%=1 : q=q% : m=z% DIV (2*q%+1)
Line 1,982: Line 1,982:


=={{header|Batch File}}==
=={{header|Batch File}}==
<syntaxhighlight lang=dos>:: Sieve of Eratosthenes for Rosetta Code - PG
<syntaxhighlight lang="dos">:: Sieve of Eratosthenes for Rosetta Code - PG
@echo off
@echo off
setlocal ENABLEDELAYEDEXPANSION
setlocal ENABLEDELAYEDEXPANSION
Line 2,031: Line 2,031:


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
<syntaxhighlight lang=bbcbasic> limit% = 100000
<syntaxhighlight lang="bbcbasic"> limit% = 100000
DIM sieve% limit%
DIM sieve% limit%
Line 2,048: Line 2,048:


=={{header|BCPL}}==
=={{header|BCPL}}==
<syntaxhighlight lang=bcpl>get "libhdr"
<syntaxhighlight lang="bcpl">get "libhdr"


manifest $( LIMIT = 1000 $)
manifest $( LIMIT = 1000 $)
Line 2,093: Line 2,093:
=== Odds-only bit packed array version (64 bit) ===
=== Odds-only bit packed array version (64 bit) ===
This sieve also uses an iterator structure to enumerate the primes in the sieve. It's inspired by the golang bit packed sieve that returns a closure as an iterator. However, BCPL does not support closures, so the code uses an iterator object.
This sieve also uses an iterator structure to enumerate the primes in the sieve. It's inspired by the golang bit packed sieve that returns a closure as an iterator. However, BCPL does not support closures, so the code uses an iterator object.
<syntaxhighlight lang=BCPL>
<syntaxhighlight lang="bcpl">
GET "libhdr"
GET "libhdr"


Line 2,253: Line 2,253:
A more efficient sieve (primes below one billion in under a minute) is provided as <code>PrimesTo</code> in bqn-libs [https://github.com/mlochbaum/bqn-libs/blob/master/primes.bqn primes.bqn].
A more efficient sieve (primes below one billion in under a minute) is provided as <code>PrimesTo</code> in bqn-libs [https://github.com/mlochbaum/bqn-libs/blob/master/primes.bqn primes.bqn].


<syntaxhighlight lang=bqn>Primes ← {
<syntaxhighlight lang="bqn">Primes ← {
𝕩≤2 ? ↕0 ; # No primes below 2
𝕩≤2 ? ↕0 ; # No primes below 2
p ← 𝕊⌈√n←𝕩 # Initial primes by recursion
p ← 𝕊⌈√n←𝕩 # Initial primes by recursion
Line 2,262: Line 2,262:


{{out}}
{{out}}
<syntaxhighlight lang=bqn> Primes 100
<syntaxhighlight lang="bqn"> Primes 100
⟨ 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 ⟩
⟨ 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 ⟩
≠∘Primes¨ 10⋆↕7 # Number of primes below 1e0, 1e1, ... 1e6
≠∘Primes¨ 10⋆↕7 # Number of primes below 1e0, 1e1, ... 1e6
Line 2,269: Line 2,269:
=={{header|Bracmat}}==
=={{header|Bracmat}}==
This solution does not use an array. Instead, numbers themselves are used as variables. The numbers that are not prime are set (to the silly value "nonprime"). Finally all numbers up to the limit are tested for being initialised. The uninitialised (unset) ones must be the primes.
This solution does not use an array. Instead, numbers themselves are used as variables. The numbers that are not prime are set (to the silly value "nonprime"). Finally all numbers up to the limit are tested for being initialised. The uninitialised (unset) ones must be the primes.
<syntaxhighlight lang=bracmat>( ( eratosthenes
<syntaxhighlight lang="bracmat">( ( eratosthenes
= n j i
= n j i
. !arg:?n
. !arg:?n
Line 2,295: Line 2,295:


=={{header|C}}==
=={{header|C}}==
Plain sieve, without any optimizations:<syntaxhighlight lang=c>#include <stdlib.h>
Plain sieve, without any optimizations:<syntaxhighlight lang="c">#include <stdlib.h>
#include <math.h>
#include <math.h>


Line 2,329: Line 2,329:
Then, in a loop, fill zeroes into those places where i * j is less than or equal to n (number of primes requested), which means they have multiples!
Then, in a loop, fill zeroes into those places where i * j is less than or equal to n (number of primes requested), which means they have multiples!
To understand this better, look at the output of the following example.
To understand this better, look at the output of the following example.
To print this back, we look for ones in the array and only print those spots. <syntaxhighlight lang=C>#include <stdio.h>
To print this back, we look for ones in the array and only print those spots. <syntaxhighlight lang="c">#include <stdio.h>
#include <malloc.h>
#include <malloc.h>
void sieve(int *, int);
void sieve(int *, int);
Line 2,367: Line 2,367:
}
}
printf("\n\n");
printf("\n\n");
}</syntaxhighlight>{{out}}<syntaxhighlight lang=Shell>i:2
}</syntaxhighlight>{{out}}<syntaxhighlight lang="shell">i:2
j:2
j:2
Before a[2*2]: 1
Before a[2*2]: 1
Line 2,395: Line 2,395:
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{works with|C sharp|C#|2.0+}}
{{works with|C sharp|C#|2.0+}}
<syntaxhighlight lang=csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
Line 2,445: Line 2,445:


To show that C# code can be written in somewhat functional paradigms, the following in an implementation of the Richard Bird sieve from the Epilogue of [Melissa E. O'Neill's definitive article](http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf) in Haskell:
To show that C# code can be written in somewhat functional paradigms, the following in an implementation of the Richard Bird sieve from the Epilogue of [Melissa E. O'Neill's definitive article](http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf) in Haskell:
<syntaxhighlight lang=csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
Line 2,492: Line 2,492:


The above code can easily be converted to "'''odds-only'''" and a infinite tree-like folding scheme with the following minor changes:
The above code can easily be converted to "'''odds-only'''" and a infinite tree-like folding scheme with the following minor changes:
<syntaxhighlight lang=csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
Line 2,549: Line 2,549:


First, an implementation of a Min Heap Priority Queue is provided as extracted from the entry at [http://rosettacode.org/wiki/Priority_queue#C.23 RosettaCode], with only the necessary methods duplicated here:
First, an implementation of a Min Heap Priority Queue is provided as extracted from the entry at [http://rosettacode.org/wiki/Priority_queue#C.23 RosettaCode], with only the necessary methods duplicated here:
<syntaxhighlight lang=csharp>namespace PriorityQ {
<syntaxhighlight lang="csharp">namespace PriorityQ {
using KeyT = System.UInt32;
using KeyT = System.UInt32;
using System;
using System;
Line 2,613: Line 2,613:
The following code implements an improved version of the '''odds-only''' O'Neil algorithm, which provides the improvements of only adding base prime composite number streams to the queue when the sieved number reaches the square of the base prime (saving a huge amount of memory and considerable execution time, including not needing as wide a range of a type for the internal prime numbers) as well as minimizing stream processing using fusion:
The following code implements an improved version of the '''odds-only''' O'Neil algorithm, which provides the improvements of only adding base prime composite number streams to the queue when the sieved number reaches the square of the base prime (saving a huge amount of memory and considerable execution time, including not needing as wide a range of a type for the internal prime numbers) as well as minimizing stream processing using fusion:


<syntaxhighlight lang=csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
Line 2,657: Line 2,657:
The above code adds quite a bit of overhead in having to provide a version of a Priority Queue for little advantage over a Dictionary (hash table based) version as per the code below:
The above code adds quite a bit of overhead in having to provide a version of a Priority Queue for little advantage over a Dictionary (hash table based) version as per the code below:


<syntaxhighlight lang=csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
Line 2,697: Line 2,697:


All of the above unbounded versions are really just an intellectual exercise as with very little extra lines of code above the fastest Dictionary based version, one can have an bit-packed page-segmented array based version as follows:
All of the above unbounded versions are really just an intellectual exercise as with very little extra lines of code above the fastest Dictionary based version, one can have an bit-packed page-segmented array based version as follows:
<syntaxhighlight lang=csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
Line 2,764: Line 2,764:


All of the above unbounded code can be tested by the following "main" method (replace the name "PrimesXXX" with the name of the class to be tested):
All of the above unbounded code can be tested by the following "main" method (replace the name "PrimesXXX" with the name of the class to be tested):
<syntaxhighlight lang=csharp> static void Main(string[] args) {
<syntaxhighlight lang="csharp"> static void Main(string[] args) {
Console.WriteLine(PrimesXXX().ElementAt(1000000 - 1)); // zero based indexing...
Console.WriteLine(PrimesXXX().ElementAt(1000000 - 1)); // zero based indexing...
}</syntaxhighlight>
}</syntaxhighlight>
Line 2,777: Line 2,777:
This implementation follows the standard library pattern of [http://en.cppreference.com/w/cpp/algorithm/iota std::iota]. The start and end iterators are provided for the container. The destination container is used for marking primes and then filled with the primes which are less than the container size. This method requires no memory allocation inside the function.
This implementation follows the standard library pattern of [http://en.cppreference.com/w/cpp/algorithm/iota std::iota]. The start and end iterators are provided for the container. The destination container is used for marking primes and then filled with the primes which are less than the container size. This method requires no memory allocation inside the function.


<syntaxhighlight lang=cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <algorithm>
#include <algorithm>
#include <vector>
#include <vector>
Line 2,828: Line 2,828:
=== Boost ===
=== Boost ===


<syntaxhighlight lang=cpp>// yield all prime numbers less than limit.
<syntaxhighlight lang="cpp">// yield all prime numbers less than limit.
template<class UnaryFunction>
template<class UnaryFunction>
void primesupto(int limit, UnaryFunction yield)
void primesupto(int limit, UnaryFunction yield)
Line 2,851: Line 2,851:
Full program:
Full program:


{{works with|Boost}}<syntaxhighlight lang=cpp>/**
{{works with|Boost}}<syntaxhighlight lang="cpp">/**
$ g++ -I/path/to/boost sieve.cpp -o sieve && sieve 10000000
$ g++ -I/path/to/boost sieve.cpp -o sieve && sieve 10000000
*/
*/
Line 2,896: Line 2,896:
{{incorrect|Chapel|Doesn't compile since at least Chapel version 1.20 to 1.24.1.}}
{{incorrect|Chapel|Doesn't compile since at least Chapel version 1.20 to 1.24.1.}}
This solution uses nested iterators to create new wheels at run time:
This solution uses nested iterators to create new wheels at run time:
<syntaxhighlight lang=chapel>// yield prime and remove all multiples of it from children sieves
<syntaxhighlight lang="chapel">// yield prime and remove all multiples of it from children sieves
iter sieve(prime):int {
iter sieve(prime):int {


Line 2,919: Line 2,919:
}
}
}</syntaxhighlight>The topmost sieve needs to be started with 2 (the smallest prime):
}</syntaxhighlight>The topmost sieve needs to be started with 2 (the smallest prime):
<syntaxhighlight lang=chapel>config const N = 30;
<syntaxhighlight lang="chapel">config const N = 30;
for p in sieve(2) {
for p in sieve(2) {
if p > N {
if p > N {
Line 2,934: Line 2,934:
compile with the `--fast` option
compile with the `--fast` option


<syntaxhighlight lang=chapel>use Time;
<syntaxhighlight lang="chapel">use Time;
use BitOps;
use BitOps;


Line 2,985: Line 2,985:
===Alternate Odds-Only Bit-Packed Implementation===
===Alternate Odds-Only Bit-Packed Implementation===


<syntaxhighlight lang=chapel>use Time;
<syntaxhighlight lang="chapel">use Time;
use BitOps;
use BitOps;


Line 3,045: Line 3,045:
{{works with|Chapel|1.25.1}}
{{works with|Chapel|1.25.1}}


<syntaxhighlight lang=chapel>use Time;
<syntaxhighlight lang="chapel">use Time;


config const limit = 100000000;
config const limit = 100000000;
Line 3,112: Line 3,112:
Compile with the `--fast` compiler command line option
Compile with the `--fast` compiler command line option


<syntaxhighlight lang=chapel>use Time;
<syntaxhighlight lang="chapel">use Time;
config const limit = 100000000;
config const limit = 100000000;
Line 3,221: Line 3,221:
Compile with the `--fast` compiler command line option
Compile with the `--fast` compiler command line option


<syntaxhighlight lang=chapel>use Time;
<syntaxhighlight lang="chapel">use Time;


use Map;
use Map;
Line 3,286: Line 3,286:
{{trans|Nim}} [https://rosettacode.org/wiki/Sieve_of_Eratosthenes#Nim_Unbounded_Versions code link]
{{trans|Nim}} [https://rosettacode.org/wiki/Sieve_of_Eratosthenes#Nim_Unbounded_Versions code link]
{{works with|Chapel|1.22|- compile with the --fast compiler command line flag for full optimization}}
{{works with|Chapel|1.22|- compile with the --fast compiler command line flag for full optimization}}
<syntaxhighlight lang=chapel>use Time;
<syntaxhighlight lang="chapel">use Time;


type Prime = uint(32);
type Prime = uint(32);
Line 3,414: Line 3,414:
To take advantage of the features that make Chapel shine, we need to use it to do some parallel computations, and to efficiently do that for the Sieve of Eratosthenes, we need to divide the work into page segments where we can assign each largish segment to a separate thread/task; this also improves the speed due to better cache associativity with most memory accesses to values that are already in the cache(s). Once we have divided the work, Chapel offers lots of means to implement the parallelism but to be a true Sieve of Eratosthenes, we need to have the ability to output the results in order; with many of the convenience mechanisms not doing that, the best/simplest option is likely task parallelism with the output results assigned to an rotary indexed array containing the `sync` results. It turns out that, although the Chapel compiler can sometimes optimize the code so the overhead of creating tasks is not onerous, for this case where the actual tasks are somewhat complex, the compiler can't recognize that an automatically generated thread pool(s) are required so we need to generate the thread pool(s) manually. The code that implements the multi-threading of page segments using thread pools is as follows:
To take advantage of the features that make Chapel shine, we need to use it to do some parallel computations, and to efficiently do that for the Sieve of Eratosthenes, we need to divide the work into page segments where we can assign each largish segment to a separate thread/task; this also improves the speed due to better cache associativity with most memory accesses to values that are already in the cache(s). Once we have divided the work, Chapel offers lots of means to implement the parallelism but to be a true Sieve of Eratosthenes, we need to have the ability to output the results in order; with many of the convenience mechanisms not doing that, the best/simplest option is likely task parallelism with the output results assigned to an rotary indexed array containing the `sync` results. It turns out that, although the Chapel compiler can sometimes optimize the code so the overhead of creating tasks is not onerous, for this case where the actual tasks are somewhat complex, the compiler can't recognize that an automatically generated thread pool(s) are required so we need to generate the thread pool(s) manually. The code that implements the multi-threading of page segments using thread pools is as follows:
{{works with|Chapel|1.24.1|- compile with the --fast compiler command line flag for full optimization}}
{{works with|Chapel|1.24.1|- compile with the --fast compiler command line flag for full optimization}}
<syntaxhighlight lang=chapel>use Time; use BitOps; use CPtr;
<syntaxhighlight lang="chapel">use Time; use BitOps; use CPtr;


type Prime = uint(64);
type Prime = uint(64);
Line 3,772: Line 3,772:


=={{header|Clojure}}==
=={{header|Clojure}}==
<syntaxhighlight lang=clojure>(defn primes< [n]
<syntaxhighlight lang="clojure">(defn primes< [n]
(remove (set (mapcat #(range (* % %) n %)
(remove (set (mapcat #(range (* % %) n %)
(range 2 (Math/sqrt n))))
(range 2 (Math/sqrt n))))
Line 3,781: Line 3,781:
It may be written using the ''into #{}'' function to run slightly faster due to the ''set'' function being concerned with only distinct elements whereas the ''into #{}'' only does the conjunction, and even at that doesn't do that much as it does the conjunction to an empty sequence, the code as follows:
It may be written using the ''into #{}'' function to run slightly faster due to the ''set'' function being concerned with only distinct elements whereas the ''into #{}'' only does the conjunction, and even at that doesn't do that much as it does the conjunction to an empty sequence, the code as follows:


<syntaxhighlight lang=clojure>(defn primes< [n]
<syntaxhighlight lang="clojure">(defn primes< [n]
(remove (into #{}
(remove (into #{}
(mapcat #(range (* % %) n %)
(mapcat #(range (* % %) n %)
Line 3,790: Line 3,790:


The following code also uses the ''into #{}'' transducer but has been slightly wheel-factorized to sieve odds-only:
The following code also uses the ''into #{}'' transducer but has been slightly wheel-factorized to sieve odds-only:
<syntaxhighlight lang=clojure>(defn primes< [n]
<syntaxhighlight lang="clojure">(defn primes< [n]
(if (< n 2) ()
(if (< n 2) ()
(cons 2 (remove (into #{}
(cons 2 (remove (into #{}
Line 3,800: Line 3,800:


The following code calculates primes up to and including ''n'' using a mutable boolean array but otherwise entirely functional code; it is tens (to a hundred) times faster than the purely functional codes due to the use of mutability in the boolean array:
The following code calculates primes up to and including ''n'' using a mutable boolean array but otherwise entirely functional code; it is tens (to a hundred) times faster than the purely functional codes due to the use of mutability in the boolean array:
<syntaxhighlight lang=clojure>(defn primes-to
<syntaxhighlight lang="clojure">(defn primes-to
"Computes lazy sequence of prime numbers up to a given number using sieve of Eratosthenes"
"Computes lazy sequence of prime numbers up to a given number using sieve of Eratosthenes"
[n]
[n]
Line 3,816: Line 3,816:
'''Alternative implementation using Clojure's side-effect oriented list comprehension.'''
'''Alternative implementation using Clojure's side-effect oriented list comprehension.'''


<syntaxhighlight lang=clojure> (defn primes-to
<syntaxhighlight lang="clojure"> (defn primes-to
"Returns a lazy sequence of prime numbers less than lim"
"Returns a lazy sequence of prime numbers less than lim"
[lim]
[lim]
Line 3,829: Line 3,829:


'''Alternative implementation using Clojure's side-effect oriented list comprehension. Odds only.'''
'''Alternative implementation using Clojure's side-effect oriented list comprehension. Odds only.'''
<syntaxhighlight lang=clojure>(defn primes-to
<syntaxhighlight lang="clojure">(defn primes-to
"Returns a lazy sequence of prime numbers less than lim"
"Returns a lazy sequence of prime numbers less than lim"
[lim]
[lim]
Line 3,848: Line 3,848:
'''Alternative very slow entirely functional implementation using lazy sequences'''
'''Alternative very slow entirely functional implementation using lazy sequences'''


<syntaxhighlight lang=clojure>(defn primes-to
<syntaxhighlight lang="clojure">(defn primes-to
"Computes lazy sequence of prime numbers up to a given number using sieve of Eratosthenes"
"Computes lazy sequence of prime numbers up to a given number using sieve of Eratosthenes"
[n]
[n]
Line 3,863: Line 3,863:


Here is an immutable boolean vector based non-lazy sequence version other than for the lazy sequence operations to output the result:
Here is an immutable boolean vector based non-lazy sequence version other than for the lazy sequence operations to output the result:
<syntaxhighlight lang=clojure>(defn primes-to
<syntaxhighlight lang="clojure">(defn primes-to
"Computes lazy sequence of prime numbers up to a given number using sieve of Eratosthenes"
"Computes lazy sequence of prime numbers up to a given number using sieve of Eratosthenes"
[max-prime]
[max-prime]
Line 3,884: Line 3,884:


The following code implements an odds-only sieve using a mutable bit packed long array, only using a lazy sequence for the output of the resulting primes:
The following code implements an odds-only sieve using a mutable bit packed long array, only using a lazy sequence for the output of the resulting primes:
<syntaxhighlight lang=clojure>(set! *unchecked-math* true)
<syntaxhighlight lang="clojure">(set! *unchecked-math* true)


(defn primes-to
(defn primes-to
Line 3,917: Line 3,917:


The following code overcomes many of those limitations by using an internal (OPSeq) "deftype" which implements the ISeq interface as well as the Counted interface to provide immediate count returns (based on a pre-computed total), as well as the IReduce interface which can greatly speed come computations based on the primes sequence (eased greatly using facilities provided by Clojure 1.7.0 and up):
The following code overcomes many of those limitations by using an internal (OPSeq) "deftype" which implements the ISeq interface as well as the Counted interface to provide immediate count returns (based on a pre-computed total), as well as the IReduce interface which can greatly speed come computations based on the primes sequence (eased greatly using facilities provided by Clojure 1.7.0 and up):
<syntaxhighlight lang=clojure>(defn primes-tox
<syntaxhighlight lang="clojure">(defn primes-tox
"Computes lazy sequence of prime numbers up to a given number using sieve of Eratosthenes"
"Computes lazy sequence of prime numbers up to a given number using sieve of Eratosthenes"
[n]
[n]
Line 4,007: Line 4,007:


'''A Clojure version of Richard Bird's Sieve using Lazy Sequences (sieves odds only)'''
'''A Clojure version of Richard Bird's Sieve using Lazy Sequences (sieves odds only)'''
<syntaxhighlight lang=clojure>(defn primes-Bird
<syntaxhighlight lang="clojure">(defn primes-Bird
"Computes the unbounded sequence of primes using a Sieve of Eratosthenes algorithm by Richard Bird."
"Computes the unbounded sequence of primes using a Sieve of Eratosthenes algorithm by Richard Bird."
[]
[]
Line 4,035: Line 4,035:


The following code speeds up the above code by merging the linear sequence of sequences as above by pairs into a right-leaning tree structure:
The following code speeds up the above code by merging the linear sequence of sequences as above by pairs into a right-leaning tree structure:
<syntaxhighlight lang=clojure>(defn primes-treeFolding
<syntaxhighlight lang="clojure">(defn primes-treeFolding
"Computes the unbounded sequence of primes using a Sieve of Eratosthenes algorithm modified from Bird."
"Computes the unbounded sequence of primes using a Sieve of Eratosthenes algorithm modified from Bird."
[]
[]
Line 4,066: Line 4,066:


The following code uses a custom "deftype" non-memoizing Co Inductive Stream/Sequence (CIS) implementing the ISeq interface to make the sequence operations more efficient and is about four times faster than the above code:
The following code uses a custom "deftype" non-memoizing Co Inductive Stream/Sequence (CIS) implementing the ISeq interface to make the sequence operations more efficient and is about four times faster than the above code:
<syntaxhighlight lang=clojure>(deftype CIS [v cont]
<syntaxhighlight lang="clojure">(deftype CIS [v cont]
clojure.lang.ISeq
clojure.lang.ISeq
(first [_] v)
(first [_] v)
Line 4,124: Line 4,124:
The following code is a version of the O'Neill Haskell code but does not use wheel factorization other than for sieving odds only (although it could be easily added) and uses a Hash Map (constant amortized access time) rather than a Priority Queue (log n access time for combined remove-and-insert-anew operations, which are the majority used for this algorithm) with a lazy sequence for output of the resulting primes; the code has the added feature that it uses a secondary base primes sequence generator and only adds prime culling sequences to the composites map when they are necessary, thus saving time and limiting storage to only that required for the map entries for primes up to the square root of the currently sieved number:
The following code is a version of the O'Neill Haskell code but does not use wheel factorization other than for sieving odds only (although it could be easily added) and uses a Hash Map (constant amortized access time) rather than a Priority Queue (log n access time for combined remove-and-insert-anew operations, which are the majority used for this algorithm) with a lazy sequence for output of the resulting primes; the code has the added feature that it uses a secondary base primes sequence generator and only adds prime culling sequences to the composites map when they are necessary, thus saving time and limiting storage to only that required for the map entries for primes up to the square root of the currently sieved number:


<syntaxhighlight lang=clojure>(defn primes-hashmap
<syntaxhighlight lang="clojure">(defn primes-hashmap
"Infinite sequence of primes using an incremental Sieve or Eratosthenes with a Hashmap"
"Infinite sequence of primes using an incremental Sieve or Eratosthenes with a Hashmap"
[]
[]
Line 4,150: Line 4,150:
In order to implement the O'Neill Priority Queue incremental Sieve of Eratosthenes algorithm, one requires an efficient implementation of a Priority Queue, which is not part of standard Clojure. For this purpose, the most suitable Priority Queue is a binary tree heap based MinHeap algorithm. The following code implements a purely functional (using entirely immutable state) MinHeap Priority Queue providing the required functions of (emtpy-pq) initialization, (getMin-pq pq) to examinte the minimum key/value pair in the queue, (insert-pq pq k v) to add entries to the queue, and (replaceMinAs-pq pq k v) to replaace the minimum entry with a key/value pair as given (it is more efficient that if functions were provided to delete and then re-insert entries in the queue; there is therefore no "delete" or other queue functions supplied as the algorithm does not requrie them:
In order to implement the O'Neill Priority Queue incremental Sieve of Eratosthenes algorithm, one requires an efficient implementation of a Priority Queue, which is not part of standard Clojure. For this purpose, the most suitable Priority Queue is a binary tree heap based MinHeap algorithm. The following code implements a purely functional (using entirely immutable state) MinHeap Priority Queue providing the required functions of (emtpy-pq) initialization, (getMin-pq pq) to examinte the minimum key/value pair in the queue, (insert-pq pq k v) to add entries to the queue, and (replaceMinAs-pq pq k v) to replaace the minimum entry with a key/value pair as given (it is more efficient that if functions were provided to delete and then re-insert entries in the queue; there is therefore no "delete" or other queue functions supplied as the algorithm does not requrie them:


<syntaxhighlight lang=clojure>(deftype PQEntry [k, v]
<syntaxhighlight lang="clojure">(deftype PQEntry [k, v]
Object
Object
(toString [_] (str "<" k "," v ">")))
(toString [_] (str "<" k "," v ">")))
Line 4,201: Line 4,201:
The actual incremental sieve using the Priority Queue is as follows, which code uses the same optimizations of postponing the addition of prime composite streams to the queue until the square root of the currently sieved number is reached and using a secondary base primes stream to generate the primes composite stream markers in the queue as was used for the Hash Map version:
The actual incremental sieve using the Priority Queue is as follows, which code uses the same optimizations of postponing the addition of prime composite streams to the queue until the square root of the currently sieved number is reached and using a secondary base primes stream to generate the primes composite stream markers in the queue as was used for the Hash Map version:


<syntaxhighlight lang=clojure>(defn primes-pq
<syntaxhighlight lang="clojure">(defn primes-pq
"Infinite sequence of primes using an incremental Sieve or Eratosthenes with a Priority Queue"
"Infinite sequence of primes using an incremental Sieve or Eratosthenes with a Priority Queue"
[]
[]
Line 4,235: Line 4,235:
To show that Clojure does not need to be particularly slow, the following version runs about twice as fast as the non-segmented unbounded array based version above (extremely fast compared to the non-array based versions) and only a little slower than other equivalent versions running on virtual machines: C# or F# on DotNet or Java and Scala on the JVM:
To show that Clojure does not need to be particularly slow, the following version runs about twice as fast as the non-segmented unbounded array based version above (extremely fast compared to the non-array based versions) and only a little slower than other equivalent versions running on virtual machines: C# or F# on DotNet or Java and Scala on the JVM:


<syntaxhighlight lang=clojure>(set! *unchecked-math* true)
<syntaxhighlight lang="clojure">(set! *unchecked-math* true)


(def PGSZ (bit-shift-left 1 14)) ;; size of CPU cache
(def PGSZ (bit-shift-left 1 14)) ;; size of CPU cache
Line 4,395: Line 4,395:


=={{header|CLU}}==
=={{header|CLU}}==
<syntaxhighlight lang=clu>% Sieve of Eratosthenes
<syntaxhighlight lang="clu">% Sieve of Eratosthenes
eratosthenes = proc (n: int) returns (array[bool])
eratosthenes = proc (n: int) returns (array[bool])
prime: array[bool] := array[bool]$fill(1, n, true)
prime: array[bool] := array[bool]$fill(1, n, true)
Line 4,447: Line 4,447:


=={{header|CMake}}==
=={{header|CMake}}==
<syntaxhighlight lang=cmake>function(eratosthenes var limit)
<syntaxhighlight lang="cmake">function(eratosthenes var limit)
# Check for integer overflow. With CMake using 32-bit signed integer,
# Check for integer overflow. With CMake using 32-bit signed integer,
# this check fails when limit > 46340.
# this check fails when limit > 46340.
Line 4,487: Line 4,487:


=={{header|COBOL}}==
=={{header|COBOL}}==
<syntaxhighlight lang=cobol>*> Please ignore the asterisks in the first column of the next comments,
<syntaxhighlight lang="cobol">*> Please ignore the asterisks in the first column of the next comments,
*> which are kludges to get syntax highlighting to work.
*> which are kludges to get syntax highlighting to work.
IDENTIFICATION DIVISION.
IDENTIFICATION DIVISION.
Line 4,545: Line 4,545:
=={{header|Comal}}==
=={{header|Comal}}==
{{trans|BASIC}}
{{trans|BASIC}}
<syntaxhighlight lang=comal>// Sieve of Eratosthenes
<syntaxhighlight lang="comal">// Sieve of Eratosthenes
input "Limit? ": limit
input "Limit? ": limit
dim sieve(1:limit)
dim sieve(1:limit)
Line 4,580: Line 4,580:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<syntaxhighlight lang=lisp>(defun sieve-of-eratosthenes (maximum)
<syntaxhighlight lang="lisp">(defun sieve-of-eratosthenes (maximum)
(loop
(loop
with sieve = (make-array (1+ maximum)
with sieve = (make-array (1+ maximum)
Line 4,594: Line 4,594:
Working with odds only (above twice speedup), and marking composites only for primes up to the square root of the maximum:
Working with odds only (above twice speedup), and marking composites only for primes up to the square root of the maximum:


<syntaxhighlight lang=lisp>(defun sieve-odds (maximum)
<syntaxhighlight lang="lisp">(defun sieve-odds (maximum)
"Prime numbers sieve for odd numbers.
"Prime numbers sieve for odd numbers.
Returns a list with all the primes that are less than or equal to maximum."
Returns a list with all the primes that are less than or equal to maximum."
Line 4,614: Line 4,614:


=={{header|Cowgol}}==
=={{header|Cowgol}}==
<syntaxhighlight lang=cowgol>include "cowgol.coh";
<syntaxhighlight lang="cowgol">include "cowgol.coh";


# To change the maximum prime, change the size of this array
# To change the maximum prime, change the size of this array
Line 4,665: Line 4,665:
This implementation uses a `BitArray` so it is automatically bit-packed to use just one bit per number representation:
This implementation uses a `BitArray` so it is automatically bit-packed to use just one bit per number representation:


<syntaxhighlight lang=ruby># compile with `--release --no-debug` for speed...
<syntaxhighlight lang="ruby"># compile with `--release --no-debug` for speed...


require "bit_array"
require "bit_array"
Line 4,727: Line 4,727:
the non-odds-only version as per the above should never be used because in not using odds-only, it uses twice the memory and over two and a half times the CPU operations as the following odds-only code, which is very little more complex:
the non-odds-only version as per the above should never be used because in not using odds-only, it uses twice the memory and over two and a half times the CPU operations as the following odds-only code, which is very little more complex:


<syntaxhighlight lang=ruby># compile with `--release --no-debug` for speed...
<syntaxhighlight lang="ruby"># compile with `--release --no-debug` for speed...


require "bit_array"
require "bit_array"
Line 4,792: Line 4,792:
For sieving of ranges larger than a few million efficiently, a page-segmented sieve should always be used to preserve CPU cache associativity by making the page size to be about that of the CPU L1 data cache. The following code implements a page-segmented version that is an extensible sieve (no upper limit needs be specified) using a secondary memoized feed of base prime value arrays which use a smaller page-segment size for efficiency. When the count of the number of primes is desired, the sieve is polymorphic in output and counts the unmarked composite bits by using fast `popcount` instructions taken 64-bits at a time. The code is as follows:
For sieving of ranges larger than a few million efficiently, a page-segmented sieve should always be used to preserve CPU cache associativity by making the page size to be about that of the CPU L1 data cache. The following code implements a page-segmented version that is an extensible sieve (no upper limit needs be specified) using a secondary memoized feed of base prime value arrays which use a smaller page-segment size for efficiency. When the count of the number of primes is desired, the sieve is polymorphic in output and counts the unmarked composite bits by using fast `popcount` instructions taken 64-bits at a time. The code is as follows:


<syntaxhighlight lang=ruby># compile with `--release --no-debug` for speed...
<syntaxhighlight lang="ruby"># compile with `--release --no-debug` for speed...


alias Prime = UInt64
alias Prime = UInt64
Line 5,007: Line 5,007:
=={{header|D}}==
=={{header|D}}==
===Simpler Version===
===Simpler Version===
Prints all numbers less than the limit.<syntaxhighlight lang=d>import std.stdio, std.algorithm, std.range, std.functional;
Prints all numbers less than the limit.<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.functional;


uint[] sieve(in uint limit) nothrow @safe {
uint[] sieve(in uint limit) nothrow @safe {
Line 5,031: Line 5,031:
===Faster Version===
===Faster Version===
This version uses an array of bits (instead of booleans, that are represented with one byte), and skips even numbers. The output is the same.
This version uses an array of bits (instead of booleans, that are represented with one byte), and skips even numbers. The output is the same.
<syntaxhighlight lang=d>import std.stdio, std.math, std.array;
<syntaxhighlight lang="d">import std.stdio, std.math, std.array;


size_t[] sieve(in size_t m) pure nothrow @safe {
size_t[] sieve(in size_t m) pure nothrow @safe {
Line 5,073: Line 5,073:
===Extensible Version===
===Extensible Version===
(This version is used in the task [[Extensible prime generator#D|Extensible prime generator]].)
(This version is used in the task [[Extensible prime generator#D|Extensible prime generator]].)
<syntaxhighlight lang=d>/// Extensible Sieve of Eratosthenes.
<syntaxhighlight lang="d">/// Extensible Sieve of Eratosthenes.
struct Prime {
struct Prime {
uint[] a = [2];
uint[] a = [2];
Line 5,111: Line 5,111:


=={{header|Dart}}==
=={{header|Dart}}==
<syntaxhighlight lang=dart>// helper function to pretty print an Iterable
<syntaxhighlight lang="dart">// helper function to pretty print an Iterable
String iterableToString(Iterable seq) {
String iterableToString(Iterable seq) {
String str = "[";
String str = "[";
Line 5,151: Line 5,151:


===faster bit-packed array odds-only solution===
===faster bit-packed array odds-only solution===
<syntaxhighlight lang=dart>import 'dart:typed_data';
<syntaxhighlight lang="dart">import 'dart:typed_data';
import 'dart:math';
import 'dart:math';


Line 5,198: Line 5,198:
The following code will have about O(n log (log n)) performance due to a hash table having O(1) average performance and is only somewhat slow due to the constant overhead of processing hashes:
The following code will have about O(n log (log n)) performance due to a hash table having O(1) average performance and is only somewhat slow due to the constant overhead of processing hashes:


<syntaxhighlight lang=dart>Iterable<int> primesMap() {
<syntaxhighlight lang="dart">Iterable<int> primesMap() {
Iterable<int> oddprms() sync* {
Iterable<int> oddprms() sync* {
yield(3); yield(5); // need at least 2 for initialization
yield(3); yield(5); // need at least 2 for initialization
Line 5,256: Line 5,256:


{{trans|Kotlin}}
{{trans|Kotlin}}
<syntaxhighlight lang=dart>import 'dart:typed_data';
<syntaxhighlight lang="dart">import 'dart:typed_data';
import 'dart:math';
import 'dart:math';
import 'dart:collection';
import 'dart:collection';
Line 5,478: Line 5,478:


=={{header|Delphi}}==
=={{header|Delphi}}==
<syntaxhighlight lang=delphi>program erathostenes;
<syntaxhighlight lang="delphi">program erathostenes;


{$APPTYPE CONSOLE}
{$APPTYPE CONSOLE}
Line 5,567: Line 5,567:


=={{header|Draco}}==
=={{header|Draco}}==
<syntaxhighlight lang=draco>/* Sieve of Eratosthenes - fill a given boolean array */
<syntaxhighlight lang="draco">/* Sieve of Eratosthenes - fill a given boolean array */
proc nonrec sieve([*] bool prime) void:
proc nonrec sieve([*] bool prime) void:
word p, c, max;
word p, c, max;
Line 5,621: Line 5,621:
=={{header|DWScript}}==
=={{header|DWScript}}==


<syntaxhighlight lang=delphi>function Primes(limit : Integer) : array of Integer;
<syntaxhighlight lang="delphi">function Primes(limit : Integer) : array of Integer;
var
var
n, k : Integer;
n, k : Integer;
Line 5,645: Line 5,645:
=={{header|Dylan}}==
=={{header|Dylan}}==
With outer to sqrt and inner to p^2 optimizations:
With outer to sqrt and inner to p^2 optimizations:
<syntaxhighlight lang=dylan>define method primes(n)
<syntaxhighlight lang="dylan">define method primes(n)
let limit = floor(n ^ 0.5) + 1;
let limit = floor(n ^ 0.5) + 1;
let sieve = make(limited(<simple-vector>, of: <boolean>), size: n + 1, fill: #t);
let sieve = make(limited(<simple-vector>, of: <boolean>), size: n + 1, fill: #t);
Line 5,706: Line 5,706:


=={{header|EasyLang}}==
=={{header|EasyLang}}==
<lang>len prims[] 100
<syntaxhighlight lang="text">len prims[] 100
max = sqrt len prims[]
max = sqrt len prims[]
tst = 2
tst = 2
Line 5,730: Line 5,730:
{{incorrect|eC|It uses rem testing and so is a trial division algorithm, not a sieve of Eratosthenes.}}
{{incorrect|eC|It uses rem testing and so is a trial division algorithm, not a sieve of Eratosthenes.}}
Note: this is not a Sieve of Eratosthenes; it is just trial division.
Note: this is not a Sieve of Eratosthenes; it is just trial division.
<syntaxhighlight lang=cpp>
<syntaxhighlight lang="cpp">
public class FindPrime
public class FindPrime
{
{
Line 5,778: Line 5,778:
=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
===Sieve===
===Sieve===
<syntaxhighlight lang=lisp>(require 'types) ;; bit-vector
<syntaxhighlight lang="lisp">(require 'types) ;; bit-vector


;; converts sieve->list for integers in [nmin .. nmax[
;; converts sieve->list for integers in [nmin .. nmax[
Line 5,811: Line 5,811:
===Segmented sieve===
===Segmented sieve===
Allow to extend the basis sieve (n) up to n^2. Memory requirement is O(√n)
Allow to extend the basis sieve (n) up to n^2. Memory requirement is O(√n)
<syntaxhighlight lang=scheme>;; ref : http://research.cs.wisc.edu/techreports/1990/TR909.pdf
<syntaxhighlight lang="scheme">;; ref : http://research.cs.wisc.edu/techreports/1990/TR909.pdf
;; delta multiple of sqrt(n)
;; delta multiple of sqrt(n)
;; segment is [left .. left+delta-1]
;; segment is [left .. left+delta-1]
Line 5,848: Line 5,848:
===Wheel===
===Wheel===
A 2x3 wheel gives a 50% performance gain.
A 2x3 wheel gives a 50% performance gain.
<syntaxhighlight lang=scheme>;; 2x3 wheel
<syntaxhighlight lang="scheme">;; 2x3 wheel
(define (weratosthenes n)
(define (weratosthenes n)
(define primes (make-bit-vector n )) ;; everybody to #f (false)
(define primes (make-bit-vector n )) ;; everybody to #f (false)
Line 5,872: Line 5,872:


On the EdsacPC simulator (see link above) the printout starts off very slowly, and gradually gets faster.
On the EdsacPC simulator (see link above) the printout starts off very slowly, and gradually gets faster.
<syntaxhighlight lang=edsac>
<syntaxhighlight lang="edsac">
[Sieve of Eratosthenes]
[Sieve of Eratosthenes]
[EDSAC program. Initial Orders 2]
[EDSAC program. Initial Orders 2]
Line 6,117: Line 6,117:
=={{header|Eiffel}}==
=={{header|Eiffel}}==
{{works with|EiffelStudio|6.6 beta (with provisional loop syntax)}}
{{works with|EiffelStudio|6.6 beta (with provisional loop syntax)}}
<syntaxhighlight lang=eiffel>class
<syntaxhighlight lang="eiffel">class
APPLICATION
APPLICATION
Line 6,159: Line 6,159:


=={{header|Elixir}}==
=={{header|Elixir}}==
<syntaxhighlight lang=elixir>defmodule Prime do
<syntaxhighlight lang="elixir">defmodule Prime do
def eratosthenes(limit \\ 1000) do
def eratosthenes(limit \\ 1000) do
sieve = [false, false | Enum.to_list(2..limit)] |> List.to_tuple
sieve = [false, false | Enum.to_list(2..limit)] |> List.to_tuple
Line 6,201: Line 6,201:
Shorter version (but slow):
Shorter version (but slow):


<syntaxhighlight lang=elixir>
<syntaxhighlight lang="elixir">
defmodule Sieve do
defmodule Sieve do
def primes_to(limit), do: sieve(Enum.to_list(2..limit))
def primes_to(limit), do: sieve(Enum.to_list(2..limit))
Line 6,213: Line 6,213:


The above code has a very limited useful range due to being very slow: for example, to sieve to a million, even changing the algorithm to odds-only, requires over 800 thousand "copy-on-update" operations of the entire saved immutable tuple ("array") of 500 thousand bytes in size, making it very much a "toy" application. The following code overcomes that problem by using a (immutable/hashed) Map to store the record of the current state of the composite number chains resulting from each of the secondary streams of base primes, which are only 167 in number up to this range; it is a functional "incremental" Sieve of Eratosthenes implementation:
The above code has a very limited useful range due to being very slow: for example, to sieve to a million, even changing the algorithm to odds-only, requires over 800 thousand "copy-on-update" operations of the entire saved immutable tuple ("array") of 500 thousand bytes in size, making it very much a "toy" application. The following code overcomes that problem by using a (immutable/hashed) Map to store the record of the current state of the composite number chains resulting from each of the secondary streams of base primes, which are only 167 in number up to this range; it is a functional "incremental" Sieve of Eratosthenes implementation:
<syntaxhighlight lang=elixir>defmodule PrimesSoEMap do
<syntaxhighlight lang="elixir">defmodule PrimesSoEMap do
@typep stt :: {integer, integer, integer, Enumerable.integer, %{integer => integer}}
@typep stt :: {integer, integer, integer, Enumerable.integer, %{integer => integer}}


Line 6,279: Line 6,279:


In order to save the computation time of computing the hashes, the following version uses a deferred execution Co-Inductive Stream type (constructed using Tuple's) in an infinite tree folding structure (by the `pairs` function):
In order to save the computation time of computing the hashes, the following version uses a deferred execution Co-Inductive Stream type (constructed using Tuple's) in an infinite tree folding structure (by the `pairs` function):
<syntaxhighlight lang=elixir>defmodule PrimesSoETreeFolding do
<syntaxhighlight lang="elixir">defmodule PrimesSoETreeFolding do
@typep cis :: {integer, (() -> cis)}
@typep cis :: {integer, (() -> cis)}
@typep ciss :: {cis, (() -> ciss)}
@typep ciss :: {cis, (() -> ciss)}
Line 6,367: Line 6,367:
The Elm language doesn't handle efficiently doing the Sieve of Eratosthenes (SoE) algorithm efficiently because it doesn't have directly accessible linear arrays and also does Copy On Write (COW) for every write to every location as well as a logarithmic process of updating as a "tree" to minimize the COW operations. Thus, there is better performance implementing the Richard Bird Tree Folding functional algorithm, as follows:
The Elm language doesn't handle efficiently doing the Sieve of Eratosthenes (SoE) algorithm efficiently because it doesn't have directly accessible linear arrays and also does Copy On Write (COW) for every write to every location as well as a logarithmic process of updating as a "tree" to minimize the COW operations. Thus, there is better performance implementing the Richard Bird Tree Folding functional algorithm, as follows:
{{trans|Haskell}}
{{trans|Haskell}}
<syntaxhighlight lang=elm>module Main exposing (main)
<syntaxhighlight lang="elm">module Main exposing (main)


import Browser exposing (element)
import Browser exposing (element)
Line 6,465: Line 6,465:
Using a Binary Minimum Heap Priority Queue is a constant factor faster than the above code as the data structure is balanced rather than "heavy to the right" in the following code, which implements enough of the Priority Queue for the purpose:
Using a Binary Minimum Heap Priority Queue is a constant factor faster than the above code as the data structure is balanced rather than "heavy to the right" in the following code, which implements enough of the Priority Queue for the purpose:


<syntaxhighlight lang=elm>module Main exposing ( main )
<syntaxhighlight lang="elm">module Main exposing ( main )


import Task exposing ( Task, succeed, perform, andThen )
import Task exposing ( Task, succeed, perform, andThen )
Line 6,596: Line 6,596:
=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==
{{libheader|cl-lib}}
{{libheader|cl-lib}}
<syntaxhighlight lang=lisp>(defun sieve-set (limit)
<syntaxhighlight lang="lisp">(defun sieve-set (limit)
(let ((xs (make-vector (1+ limit) 0)))
(let ((xs (make-vector (1+ limit) 0)))
(cl-loop for i from 2 to limit
(cl-loop for i from 2 to limit
Line 6,606: Line 6,606:
Straightforward implementation of [http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Implementation sieve of Eratosthenes], 2 times faster:
Straightforward implementation of [http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Implementation sieve of Eratosthenes], 2 times faster:


<syntaxhighlight lang=lisp>(defun sieve (limit)
<syntaxhighlight lang="lisp">(defun sieve (limit)
(let ((xs (vconcat [0 0] (number-sequence 2 limit))))
(let ((xs (vconcat [0 0] (number-sequence 2 limit))))
(cl-loop for i from 2 to (sqrt limit)
(cl-loop for i from 2 to (sqrt limit)
Line 6,616: Line 6,616:
=={{header|Erlang}}==
=={{header|Erlang}}==
===Erlang using Dicts===
===Erlang using Dicts===
<syntaxhighlight lang=Erlang>
<syntaxhighlight lang="erlang">
-module( sieve_of_eratosthenes ).
-module( sieve_of_eratosthenes ).


Line 6,648: Line 6,648:
Has the virtue of working for any -> N :)
Has the virtue of working for any -> N :)


<syntaxhighlight lang=Erlang>
<syntaxhighlight lang="erlang">
-module( sieve ).
-module( sieve ).
-export( [main/1,primes/2] ).
-export( [main/1,primes/2] ).
Line 6,696: Line 6,696:
Since I had written a really odd and slow one, I thought I'd best do a better performer. Inspired by an example from https://github.com/jupp0r
Since I had written a really odd and slow one, I thought I'd best do a better performer. Inspired by an example from https://github.com/jupp0r


<syntaxhighlight lang=Erlang>
<syntaxhighlight lang="erlang">


-module(ossieve).
-module(ossieve).
Line 6,737: Line 6,737:
A pure list comprehension approach.
A pure list comprehension approach.


<syntaxhighlight lang=Erlang>
<syntaxhighlight lang="erlang">
-module(sieveof).
-module(sieveof).
-export([main/1,primes/1, primes/2]).
-export([main/1,primes/1, primes/2]).
Line 6,770: Line 6,770:
===Erlang ets + cpu distributed implementation ===
===Erlang ets + cpu distributed implementation ===
much faster previous erlang examples
much faster previous erlang examples
<syntaxhighlight lang=Erlang>
<syntaxhighlight lang="erlang">
#!/usr/bin/env escript
#!/usr/bin/env escript
%% -*- erlang -*-
%% -*- erlang -*-
Line 6,846: Line 6,846:


=={{header|ERRE}}==
=={{header|ERRE}}==
<syntaxhighlight lang=ERRE>
<syntaxhighlight lang="erre">
PROGRAM SIEVE_ORG
PROGRAM SIEVE_ORG
! --------------------------------------------------
! --------------------------------------------------
Line 6,890: Line 6,890:


=={{header|Euphoria}}==
=={{header|Euphoria}}==
<syntaxhighlight lang=euphoria>constant limit = 1000
<syntaxhighlight lang="euphoria">constant limit = 1000
sequence flags,primes
sequence flags,primes
flags = repeat(1, limit)
flags = repeat(1, limit)
Line 6,923: Line 6,923:
=={{header|F Sharp}}==
=={{header|F Sharp}}==
===Short with mutable state===
===Short with mutable state===
<syntaxhighlight lang=fsharp>
<syntaxhighlight lang="fsharp">
let primes max =
let primes max =
let mutable xs = [|2..max|]
let mutable xs = [|2..max|]
Line 6,933: Line 6,933:
===Short Sweet Functional and Idiotmatic===
===Short Sweet Functional and Idiotmatic===
Well lists may not be lazy, but if you call it a sequence then it's a lazy list!
Well lists may not be lazy, but if you call it a sequence then it's a lazy list!
<syntaxhighlight lang=fsharp>
<syntaxhighlight lang="fsharp">
(*
(*
An interesting implementation of The Sieve of Eratosthenes.
An interesting implementation of The Sieve of Eratosthenes.
Line 6,972: Line 6,972:


This is the idea behind Richard Bird's unbounded code presented in the Epilogue of [http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf M. O'Neill's article] in Haskell. It is about twice as much code as the Haskell code because F# does not have a built-in lazy list so that the effect must be constructed using a Co-Inductive Stream (CIS) type since no memoization is required, along with the use of recursive functions in combination with sequences. The type inference needs some help with the new CIS type (including selecting the generic type for speed). Note the use of recursive functions to implement multiple non-sharing delayed generating base primes streams, which along with these being non-memoizing means that the entire primes stream is not held in memory as for the original Bird code:
This is the idea behind Richard Bird's unbounded code presented in the Epilogue of [http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf M. O'Neill's article] in Haskell. It is about twice as much code as the Haskell code because F# does not have a built-in lazy list so that the effect must be constructed using a Co-Inductive Stream (CIS) type since no memoization is required, along with the use of recursive functions in combination with sequences. The type inference needs some help with the new CIS type (including selecting the generic type for speed). Note the use of recursive functions to implement multiple non-sharing delayed generating base primes streams, which along with these being non-memoizing means that the entire primes stream is not held in memory as for the original Bird code:
<syntaxhighlight lang=fsharp>type 'a CIS = CIS of 'a * (unit -> 'a CIS) //'Co Inductive Stream for laziness
<syntaxhighlight lang="fsharp">type 'a CIS = CIS of 'a * (unit -> 'a CIS) //'Co Inductive Stream for laziness


let primesBird() =
let primesBird() =
Line 6,990: Line 6,990:


The above code sieves all numbers of two and up including all even numbers as per the page specification; the following code makes the very minor changes for an odds-only sieve, with a speedup of over a factor of two:
The above code sieves all numbers of two and up including all even numbers as per the page specification; the following code makes the very minor changes for an odds-only sieve, with a speedup of over a factor of two:
<syntaxhighlight lang=fsharp>type 'a CIS = CIS of 'a * (unit -> 'a CIS) //'Co Inductive Stream for laziness
<syntaxhighlight lang="fsharp">type 'a CIS = CIS of 'a * (unit -> 'a CIS) //'Co Inductive Stream for laziness


let primesBirdOdds() =
let primesBirdOdds() =
Line 7,011: Line 7,011:


The above code is still somewhat inefficient as it operates on a linear right extending structure that deepens linearly with increasing base primes (those up to the square root of the currently sieved number); the following code changes the structure into an infinite binary tree-like folding by combining each pair of prime composite streams before further processing as usual - this decreases the processing by approximately a factor of log n:
The above code is still somewhat inefficient as it operates on a linear right extending structure that deepens linearly with increasing base primes (those up to the square root of the currently sieved number); the following code changes the structure into an infinite binary tree-like folding by combining each pair of prime composite streams before further processing as usual - this decreases the processing by approximately a factor of log n:
<syntaxhighlight lang=fsharp>type 'a CIS = CIS of 'a * (unit -> 'a CIS) //'Co Inductive Stream for laziness
<syntaxhighlight lang="fsharp">type 'a CIS = CIS of 'a * (unit -> 'a CIS) //'Co Inductive Stream for laziness


let primesTreeFold() =
let primesTreeFold() =
Line 7,036: Line 7,036:


In order to investigate Priority Queue Sieves as espoused by O'Neill in the referenced article, one must find an equivalent implementation of a Min Heap Priority Queue as used by her. There is such an purely functional implementation [http://rosettacode.org/wiki/Priority_queue#Functional in RosettaCode translated from the Haskell code she used], from which the essential parts are duplicated here (Note that the key value is given an integer type in order to avoid the inefficiency of F# in generic comparison):
In order to investigate Priority Queue Sieves as espoused by O'Neill in the referenced article, one must find an equivalent implementation of a Min Heap Priority Queue as used by her. There is such an purely functional implementation [http://rosettacode.org/wiki/Priority_queue#Functional in RosettaCode translated from the Haskell code she used], from which the essential parts are duplicated here (Note that the key value is given an integer type in order to avoid the inefficiency of F# in generic comparison):
<syntaxhighlight lang=fsharp>[<RequireQualifiedAccess>]
<syntaxhighlight lang="fsharp">[<RequireQualifiedAccess>]
module MinHeap =
module MinHeap =


Line 7,076: Line 7,076:


Except as noted for any individual code, all of the following codes need the following prefix code in order to implement the non-memoizing Co-Inductive Streams (CIS's) and to set the type of particular constants used in the codes to the same time as the "Prime" type:
Except as noted for any individual code, all of the following codes need the following prefix code in order to implement the non-memoizing Co-Inductive Streams (CIS's) and to set the type of particular constants used in the codes to the same time as the "Prime" type:
<syntaxhighlight lang=fsharp>type CIS<'T> = struct val v: 'T val cont: unit -> CIS<'T> new(v,cont) = {v=v;cont=cont} end
<syntaxhighlight lang="fsharp">type CIS<'T> = struct val v: 'T val cont: unit -> CIS<'T> new(v,cont) = {v=v;cont=cont} end
type Prime = uint32
type Prime = uint32
let frstprm = 2u
let frstprm = 2u
Line 7,084: Line 7,084:


The F# equivalent to O'Neill's "odds-only" code is then implemented as follows, which needs the included changed prefix in order to change the primes type to a larger one to prevent overflow (as well the key type for the MinHeap needs to be changed from uint32 to uint64); it is functionally the same as the O'Neill code other than for minor changes to suit the use of CIS streams and the option output of the "peekMin" function:
The F# equivalent to O'Neill's "odds-only" code is then implemented as follows, which needs the included changed prefix in order to change the primes type to a larger one to prevent overflow (as well the key type for the MinHeap needs to be changed from uint32 to uint64); it is functionally the same as the O'Neill code other than for minor changes to suit the use of CIS streams and the option output of the "peekMin" function:
<syntaxhighlight lang=fsharp>type CIS<'T> = struct val v: 'T val cont: unit -> CIS<'T> new(v,cont) = {v=v;cont=cont} end
<syntaxhighlight lang="fsharp">type CIS<'T> = struct val v: 'T val cont: unit -> CIS<'T> new(v,cont) = {v=v;cont=cont} end
type Prime = uint64
type Prime = uint64
let frstprm = 2UL
let frstprm = 2UL
Line 7,117: Line 7,117:


However, that algorithm suffers in speed and memory use due to over-eager adding of prime composite streams to the queue such that the queue used is much larger than it needs to be and a much larger range of primes number must be used in order to avoid numeric overflow on the square of the prime added to the queue. The following code corrects that by using a secondary (actually a multiple of) base primes streams which are constrained to be based on a prime that is no larger than the square root of the currently sieved number - this permits the use of much smaller Prime types as per the default prefix:
However, that algorithm suffers in speed and memory use due to over-eager adding of prime composite streams to the queue such that the queue used is much larger than it needs to be and a much larger range of primes number must be used in order to avoid numeric overflow on the square of the prime added to the queue. The following code corrects that by using a secondary (actually a multiple of) base primes streams which are constrained to be based on a prime that is no larger than the square root of the currently sieved number - this permits the use of much smaller Prime types as per the default prefix:
<syntaxhighlight lang=fsharp>let primesPQx() =
<syntaxhighlight lang="fsharp">let primesPQx() =
let rec nxtprm n pq q (bps: CIS<Prime>) =
let rec nxtprm n pq q (bps: CIS<Prime>) =
if n >= q then let bp = bps.v in let adv = bp + bp
if n >= q then let bp = bps.v in let adv = bp + bp
Line 7,154: Line 7,154:
The following code is written in functional style other than it uses a mutable bit array to sieve the composites:
The following code is written in functional style other than it uses a mutable bit array to sieve the composites:


<syntaxhighlight lang=fsharp>let primes limit =
<syntaxhighlight lang="fsharp">let primes limit =
let buf = System.Collections.BitArray(int limit + 1, true)
let buf = System.Collections.BitArray(int limit + 1, true)
let cull p = { p * p .. p .. limit } |> Seq.iter (fun c -> buf.[int c] <- false)
let cull p = { p * p .. p .. limit } |> Seq.iter (fun c -> buf.[int c] <- false)
Line 7,168: Line 7,168:
Substituting the following minor changes to the code for the "primes" function will only deal with the odd prime candidates for a speed up of over a factor of two as well as a reduction of the buffer size by a factor of two:
Substituting the following minor changes to the code for the "primes" function will only deal with the odd prime candidates for a speed up of over a factor of two as well as a reduction of the buffer size by a factor of two:


<syntaxhighlight lang=fsharp>let primes limit =
<syntaxhighlight lang="fsharp">let primes limit =
let lmtb,lmtbsqrt = (limit - 3u) / 2u, (uint32 (sqrt (double limit)) - 3u) / 2u
let lmtb,lmtbsqrt = (limit - 3u) / 2u, (uint32 (sqrt (double limit)) - 3u) / 2u
let buf = System.Collections.BitArray(int lmtb + 1, true)
let buf = System.Collections.BitArray(int lmtb + 1, true)
Line 7,180: Line 7,180:
The following code uses other functional forms for the inner culling loops of the "primes function" to reduce the use of inefficient sequences so as to reduce the execution time by another factor of almost three:
The following code uses other functional forms for the inner culling loops of the "primes function" to reduce the use of inefficient sequences so as to reduce the execution time by another factor of almost three:


<syntaxhighlight lang=fsharp>let primes limit =
<syntaxhighlight lang="fsharp">let primes limit =
let lmtb,lmtbsqrt = (limit - 3u) / 2u, (uint32 (sqrt (double limit)) - 3u) / 2u
let lmtb,lmtbsqrt = (limit - 3u) / 2u, (uint32 (sqrt (double limit)) - 3u) / 2u
let buf = System.Collections.BitArray(int lmtb + 1, true)
let buf = System.Collections.BitArray(int lmtb + 1, true)
Line 7,191: Line 7,191:
Now much of the remaining execution time is just the time to enumerate the primes as can be seen by turning "primes" into a primes counting function by substituting the following for the last line in the above code doing the enumeration; this makes the code run about a further five times faster:
Now much of the remaining execution time is just the time to enumerate the primes as can be seen by turning "primes" into a primes counting function by substituting the following for the last line in the above code doing the enumeration; this makes the code run about a further five times faster:


<syntaxhighlight lang=fsharp> let rec count i acc =
<syntaxhighlight lang="fsharp"> let rec count i acc =
if i > int lmtb then acc else if buf.[i] then count (i + 1) (acc + 1) else count (i + 1) acc
if i > int lmtb then acc else if buf.[i] then count (i + 1) (acc + 1) else count (i + 1) acc
count 0 1</syntaxhighlight>
count 0 1</syntaxhighlight>
Line 7,197: Line 7,197:
Since the final enumeration of primes is the main remaining bottleneck, it is worth using a "roll-your-own" enumeration implemented as an object expression so as to save many inefficiencies in the use of the built-in seq computational expression by substituting the following code for the last line of the previous codes, which will decrease the execution time by a factor of over three (instead of almost five for the counting-only version, making it almost as fast):
Since the final enumeration of primes is the main remaining bottleneck, it is worth using a "roll-your-own" enumeration implemented as an object expression so as to save many inefficiencies in the use of the built-in seq computational expression by substituting the following code for the last line of the previous codes, which will decrease the execution time by a factor of over three (instead of almost five for the counting-only version, making it almost as fast):


<syntaxhighlight lang=fsharp> let nmrtr() =
<syntaxhighlight lang="fsharp"> let nmrtr() =
let i = ref -2
let i = ref -2
let rec nxti() = i:=!i + 1;if !i <= int lmtb && not buf.[!i] then nxti() else !i <= int lmtb
let rec nxti() = i:=!i + 1;if !i <= int lmtb && not buf.[!i] then nxti() else !i <= int lmtb
Line 7,224: Line 7,224:


The following code uses the DotNet Dictionary class instead of the above functional Priority Queue to implement the sieve; as average (amortized) hash table access is O(1) rather than O(log n) as for the priority queue, this implementation is slightly faster than the priority queue version for the first million primes and will always be faster for any range above some low range value:
The following code uses the DotNet Dictionary class instead of the above functional Priority Queue to implement the sieve; as average (amortized) hash table access is O(1) rather than O(log n) as for the priority queue, this implementation is slightly faster than the priority queue version for the first million primes and will always be faster for any range above some low range value:
<syntaxhighlight lang=fsharp>type Prime = uint32
<syntaxhighlight lang="fsharp">type Prime = uint32
let frstprm = 2u
let frstprm = 2u
let frstoddprm = 3u
let frstoddprm = 3u
Line 7,260: Line 7,260:


All of the above unbounded implementations including the above Dictionary based version are quite slow due to their large constant factor computational overheads, making them more of an intellectual exercise than something practical, especially when larger sieving ranges are required. The following code implements an unbounded page segmented version of the sieve in not that many more lines of code, yet runs about 25 times faster than the Dictionary version for larger ranges of sieving such as to one billion; it uses functional forms without mutability other than for the contents of the arrays and the `primes` enumeration generator function that must use mutability for speed:
All of the above unbounded implementations including the above Dictionary based version are quite slow due to their large constant factor computational overheads, making them more of an intellectual exercise than something practical, especially when larger sieving ranges are required. The following code implements an unbounded page segmented version of the sieve in not that many more lines of code, yet runs about 25 times faster than the Dictionary version for larger ranges of sieving such as to one billion; it uses functional forms without mutability other than for the contents of the arrays and the `primes` enumeration generator function that must use mutability for speed:
<syntaxhighlight lang=fsharp>type Prime = float // use uint64/int64 for regular 64-bit F#
<syntaxhighlight lang="fsharp">type Prime = float // use uint64/int64 for regular 64-bit F#
type private PrimeNdx = float // they are slow in JavaScript polyfills
type private PrimeNdx = float // they are slow in JavaScript polyfills


Line 7,415: Line 7,415:


Factor is pleasantly multiparadigm. Usually, it's natural to write more functional or declarative code in Factor, but this is an instance where it is more natural to write imperative code. Lexical variables are useful here for expressing the necessary mutations in a clean way.
Factor is pleasantly multiparadigm. Usually, it's natural to write more functional or declarative code in Factor, but this is an instance where it is more natural to write imperative code. Lexical variables are useful here for expressing the necessary mutations in a clean way.
<syntaxhighlight lang=factor>USING: bit-arrays io kernel locals math math.functions
<syntaxhighlight lang="factor">USING: bit-arrays io kernel locals math math.functions
math.ranges prettyprint sequences ;
math.ranges prettyprint sequences ;
IN: rosetta-code.sieve-of-erato
IN: rosetta-code.sieve-of-erato
Line 7,449: Line 7,449:


=={{header|FOCAL}}==
=={{header|FOCAL}}==
<syntaxhighlight lang=FOCAL>1.1 T "PLEASE ENTER LIMIT"
<syntaxhighlight lang="focal">1.1 T "PLEASE ENTER LIMIT"
1.2 A N
1.2 A N
1.3 I (2047-N)5.1
1.3 I (2047-N)5.1
Line 7,496: Line 7,496:
The above code is not really very good Forth style as the main initialization, sieving, and output, are all in one `sieve` routine which makes it difficult to understand and refactor; Forth code is normally written in a series of very small routines which makes it easier to understand what is happening on the data stack, since Forth does not have named local re-entrant variable names as most other languages do for local variables (which other languages also normally store local variables on the stack). Also, it uses the `HERE` pointer to user space which points to the next available memory after all compilation is done as a unsized buffer pointer, but as it does not reserve that space for the sieving buffer, it can be changed by other concatenated routines in unexpected ways; better is to allocate the sieving buffer as required from the available space at the time the routines are run and pass that address between concatenated functions until a finalization function frees the memory and clears the stack; this is equivalent to allocating from the "heap" in other languages. The below code demonstrates these ideas:
The above code is not really very good Forth style as the main initialization, sieving, and output, are all in one `sieve` routine which makes it difficult to understand and refactor; Forth code is normally written in a series of very small routines which makes it easier to understand what is happening on the data stack, since Forth does not have named local re-entrant variable names as most other languages do for local variables (which other languages also normally store local variables on the stack). Also, it uses the `HERE` pointer to user space which points to the next available memory after all compilation is done as a unsized buffer pointer, but as it does not reserve that space for the sieving buffer, it can be changed by other concatenated routines in unexpected ways; better is to allocate the sieving buffer as required from the available space at the time the routines are run and pass that address between concatenated functions until a finalization function frees the memory and clears the stack; this is equivalent to allocating from the "heap" in other languages. The below code demonstrates these ideas:


<syntaxhighlight lang=forth>: prime? ( addr -- ? ) C@ 0= ; \ test composites array for prime
<syntaxhighlight lang="forth">: prime? ( addr -- ? ) C@ 0= ; \ test composites array for prime


\ given square index and prime index, u0, sieve the multiples of said prime...
\ given square index and prime index, u0, sieve the multiples of said prime...
Line 7,554: Line 7,554:
Although the above version resolves many problems of the first version, it is wasteful of memory as each composite number in the sieve buffer is a byte of eight bits representing a boolean value. The memory required can be reduced eight-fold by bit packing the sieve buffer; this will take more "bit-twiddling" to read and write the bits, but reducing the memory used will give better cache assiciativity to larger ranges such that there will be a net gain in performance. This will make the code more complex and the stack manipulations will be harder to write, debug, and maintain, so ANS Forth 1994 provides a local variable naming facility to make this much easier. The following code implements bit-packing of the sieve buffer using local named variables when required:
Although the above version resolves many problems of the first version, it is wasteful of memory as each composite number in the sieve buffer is a byte of eight bits representing a boolean value. The memory required can be reduced eight-fold by bit packing the sieve buffer; this will take more "bit-twiddling" to read and write the bits, but reducing the memory used will give better cache assiciativity to larger ranges such that there will be a net gain in performance. This will make the code more complex and the stack manipulations will be harder to write, debug, and maintain, so ANS Forth 1994 provides a local variable naming facility to make this much easier. The following code implements bit-packing of the sieve buffer using local named variables when required:


<lang>\ produces number of one bits in given word...
<syntaxhighlight lang="text">\ produces number of one bits in given word...
: numbts ( u -- u ) \ pop count number of bits...
: numbts ( u -- u ) \ pop count number of bits...
0 SWAP BEGIN DUP 0<> WHILE SWAP 1+ SWAP DUP 1- AND REPEAT DROP ;
0 SWAP BEGIN DUP 0<> WHILE SWAP 1+ SWAP DUP 1- AND REPEAT DROP ;
Line 7,647: Line 7,647:
While the above version does greatly reduce the amount of memory used for a given sieving range and thereby also somewhat reduces execution time; any sieve intended for sieving to limits of a hundred million or more should use a page-segmented implementation; page-segmentation means that only storage for a representation of the base primes up to the square root of the limit plus a sieve buffer that should also be at least proportional to the same square root is required; this will again make the execution faster as ranges go up due to better cache associativity with most memory accesses being within the CPU cache sizes. The following Forth code implements a basic version that does this:
While the above version does greatly reduce the amount of memory used for a given sieving range and thereby also somewhat reduces execution time; any sieve intended for sieving to limits of a hundred million or more should use a page-segmented implementation; page-segmentation means that only storage for a representation of the base primes up to the square root of the limit plus a sieve buffer that should also be at least proportional to the same square root is required; this will again make the execution faster as ranges go up due to better cache associativity with most memory accesses being within the CPU cache sizes. The following Forth code implements a basic version that does this:


<syntaxhighlight lang=forth>\ CPU L1 and L2 cache sizes in bits; power of 2...
<syntaxhighlight lang="forth">\ CPU L1 and L2 cache sizes in bits; power of 2...
1 17 LSHIFT CONSTANT L1CacheBits
1 17 LSHIFT CONSTANT L1CacheBits
L1CacheBits 8 * CONSTANT L2CacheBits
L1CacheBits 8 * CONSTANT L2CacheBits
Line 7,781: Line 7,781:
=={{header|Fortran}}==
=={{header|Fortran}}==
{{works with|Fortran|77}}
{{works with|Fortran|77}}
<syntaxhighlight lang=fortran>
<syntaxhighlight lang="fortran">
PROGRAM MAIN
PROGRAM MAIN
INTEGER LI
INTEGER LI
Line 7,821: Line 7,821:


{{works with|Fortran|90 and later}}
{{works with|Fortran|90 and later}}
<syntaxhighlight lang=fortran>program sieve
<syntaxhighlight lang="fortran">program sieve


implicit none
implicit none
Line 7,840: Line 7,840:
end program sieve</syntaxhighlight>
end program sieve</syntaxhighlight>
Output:
Output:
<lang>2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97</syntaxhighlight>
<syntaxhighlight lang="text">2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97</syntaxhighlight>


Because it uses four byte logical's (default size) as elements of the sieve buffer, the above code uses 400 bytes of memory for this trivial task of sieving to 100; it also has 49 + 31 + 16 + 8 = 104 (for the culling by the primes of two, three, five, and seven) culling operations.
Because it uses four byte logical's (default size) as elements of the sieve buffer, the above code uses 400 bytes of memory for this trivial task of sieving to 100; it also has 49 + 31 + 16 + 8 = 104 (for the culling by the primes of two, three, five, and seven) culling operations.
Line 7,846: Line 7,846:
'''Optimised using a pre-computed wheel based on 2:'''
'''Optimised using a pre-computed wheel based on 2:'''


<syntaxhighlight lang=fortran>program sieve_wheel_2
<syntaxhighlight lang="fortran">program sieve_wheel_2


implicit none
implicit none
Line 7,866: Line 7,866:
end program sieve_wheel_2</syntaxhighlight>
end program sieve_wheel_2</syntaxhighlight>
Output:
Output:
<lang>2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97</syntaxhighlight>
<syntaxhighlight lang="text">2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97</syntaxhighlight>


This so-called "optimized" version still uses 400 bytes of memory but slightly reduces to 74 operations from 104 operations including the initialization of marking all of the even representations as composite due to skipping the re-culling of the even representation, so isn't really much of an optimization at all!
This so-called "optimized" version still uses 400 bytes of memory but slightly reduces to 74 operations from 104 operations including the initialization of marking all of the even representations as composite due to skipping the re-culling of the even representation, so isn't really much of an optimization at all!
Line 7,874: Line 7,874:
The above implementations, especially the second odds-only code, are some of the most inefficient versions of the Sieve of Eratosthenes in any language here as to time and space efficiency, only worse by some naive JavaScript implementations that use eight-byte Number's as logical values; the second claims to be wheel factorized but still uses all the same memory as the first and still culls by the even numbers in the initialization of the sieve buffer. As well, using four bytes (default logical size) to store a boolean value is terribly wasteful if these implementations were to be extended to non-toy ranges. The following code implements proper wheel factorization by two, reducing the space used by a factor of about eight to 49 bytes by using `byte` as the sieve buffer array elements and not requiring the evens initialization, thus reducing the number of operations to 16 + 8 + 4 = 28 (for the culling primes of three, five, and seven) culling operations:
The above implementations, especially the second odds-only code, are some of the most inefficient versions of the Sieve of Eratosthenes in any language here as to time and space efficiency, only worse by some naive JavaScript implementations that use eight-byte Number's as logical values; the second claims to be wheel factorized but still uses all the same memory as the first and still culls by the even numbers in the initialization of the sieve buffer. As well, using four bytes (default logical size) to store a boolean value is terribly wasteful if these implementations were to be extended to non-toy ranges. The following code implements proper wheel factorization by two, reducing the space used by a factor of about eight to 49 bytes by using `byte` as the sieve buffer array elements and not requiring the evens initialization, thus reducing the number of operations to 16 + 8 + 4 = 28 (for the culling primes of three, five, and seven) culling operations:


<syntaxhighlight lang=fortran>program sieve_wheel_2
<syntaxhighlight lang="fortran">program sieve_wheel_2
implicit none
implicit none
Line 7,900: Line 7,900:
The above implementation is still space inefficient in effectively only using one bit out of eight; the following version implements bit packing to reduce memory use by a factor of eight by using bits to represent composite numbers rather than bytes:
The above implementation is still space inefficient in effectively only using one bit out of eight; the following version implements bit packing to reduce memory use by a factor of eight by using bits to represent composite numbers rather than bytes:


<syntaxhighlight lang=fortran>program sieve_wheel_2
<syntaxhighlight lang="fortran">program sieve_wheel_2
implicit none
implicit none
Line 7,939: Line 7,939:
As well as adding page-segmentation, the following code adds multi-processing which is onc of the capabilities for which modern Fortran is known:
As well as adding page-segmentation, the following code adds multi-processing which is onc of the capabilities for which modern Fortran is known:


<syntaxhighlight lang=fortran>subroutine cullSieveBuffer(lwi, size, bpa, sba)
<syntaxhighlight lang="fortran">subroutine cullSieveBuffer(lwi, size, bpa, sba)


implicit none
implicit none
Line 8,097: Line 8,097:
===Basic version===
===Basic version===
function Sieve returns a list of primes less than or equal to the given aLimit
function Sieve returns a list of primes less than or equal to the given aLimit
<syntaxhighlight lang=pascal>
<syntaxhighlight lang="pascal">
program prime_sieve;
program prime_sieve;
{$mode objfpc}{$coperators on}
{$mode objfpc}{$coperators on}
Line 8,174: Line 8,174:
===Alternative segmented(odds only) version===
===Alternative segmented(odds only) version===
function OddSegmentSieve returns a list of primes less than or equal to the given aLimit
function OddSegmentSieve returns a list of primes less than or equal to the given aLimit
<syntaxhighlight lang=pascal>
<syntaxhighlight lang="pascal">
program prime_sieve;
program prime_sieve;
{$mode objfpc}{$coperators on}
{$mode objfpc}{$coperators on}
Line 8,307: Line 8,307:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<syntaxhighlight lang=freebasic>' FB 1.05.0
<syntaxhighlight lang="freebasic">' FB 1.05.0


Sub sieve(n As Integer)
Sub sieve(n As Integer)
Line 8,363: Line 8,363:


=={{header|Frink}}==
=={{header|Frink}}==
<syntaxhighlight lang=frink>
<syntaxhighlight lang="frink">
n = eval[input["Enter highest number: "]]
n = eval[input["Enter highest number: "]]
results = array[sieve[n]]
results = array[sieve[n]]
Line 8,387: Line 8,387:
''Note: With benchmark function''
''Note: With benchmark function''


<syntaxhighlight lang=Furor>
<syntaxhighlight lang="furor">
tick sto startingtick
tick sto startingtick
#g 100000 sto MAX
#g 100000 sto MAX
Line 8,410: Line 8,410:
=={{header|FutureBasic}}==
=={{header|FutureBasic}}==
===Basic sieve of array of booleans===
===Basic sieve of array of booleans===
<syntaxhighlight lang=futurebasic>window 1, @"Sieve of Eratosthenes", (0,0,720,300)
<syntaxhighlight lang="futurebasic">window 1, @"Sieve of Eratosthenes", (0,0,720,300)


begin globals
begin globals
Line 8,445: Line 8,445:


=={{header|GAP}}==
=={{header|GAP}}==
<syntaxhighlight lang=gap>Eratosthenes := function(n)
<syntaxhighlight lang="gap">Eratosthenes := function(n)
local a, i, j;
local a, i, j;
a := ListWithIdenticalEntries(n, true);
a := ListWithIdenticalEntries(n, true);
Line 8,472: Line 8,472:


=={{header|GLBasic}}==
=={{header|GLBasic}}==
<syntaxhighlight lang=GLBasic>// Sieve of Eratosthenes (find primes)
<syntaxhighlight lang="glbasic">// Sieve of Eratosthenes (find primes)
// GLBasic implementation
// GLBasic implementation


Line 8,500: Line 8,500:
=={{header|Go}}==
=={{header|Go}}==
===Basic sieve of array of booleans===
===Basic sieve of array of booleans===
<syntaxhighlight lang=go>package main
<syntaxhighlight lang="go">package main
import "fmt"
import "fmt"


Line 8,560: Line 8,560:
The above version's output is rather specialized; the following version uses a closure function to enumerate over the culled composite number array, which is bit packed. By using this scheme for output, no extra memory is required above that required for the culling array:
The above version's output is rather specialized; the following version uses a closure function to enumerate over the culled composite number array, which is bit packed. By using this scheme for output, no extra memory is required above that required for the culling array:


<syntaxhighlight lang=go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 8,614: Line 8,614:
===Sieve Tree===
===Sieve Tree===
A fairly odd sieve tree method:
A fairly odd sieve tree method:
<syntaxhighlight lang=go>package main
<syntaxhighlight lang="go">package main
import "fmt"
import "fmt"


Line 8,677: Line 8,677:
===Concurrent Daisy-chain sieve===
===Concurrent Daisy-chain sieve===
A concurrent prime sieve adopted from the example in the "Go Playground" window at http://golang.org/
A concurrent prime sieve adopted from the example in the "Go Playground" window at http://golang.org/
<syntaxhighlight lang=go>package main
<syntaxhighlight lang="go">package main
import "fmt"
import "fmt"
Line 8,738: Line 8,738:
===Postponed Concurrent Daisy-chain sieve===
===Postponed Concurrent Daisy-chain sieve===
Here we postpone the ''creation'' of filters until the prime's square is seen in the input, to radically reduce the amount of filter channels in the sieve chain.
Here we postpone the ''creation'' of filters until the prime's square is seen in the input, to radically reduce the amount of filter channels in the sieve chain.
<syntaxhighlight lang=go>package main
<syntaxhighlight lang="go">package main
import "fmt"
import "fmt"
Line 8,815: Line 8,815:
===Incremental Odds-only Sieve===
===Incremental Odds-only Sieve===
Uses Go's built-in hash tables to store odd composites, and defers adding new known composites until the square is seen.
Uses Go's built-in hash tables to store odd composites, and defers adding new known composites until the square is seen.
<syntaxhighlight lang=go>
<syntaxhighlight lang="go">
package main
package main


Line 8,882: Line 8,882:
=={{header|Groovy}}==
=={{header|Groovy}}==
This solution uses a BitSet for compactness and speed, but in [[Groovy]], BitSet has full List semantics. It also uses both the "square root of the boundary" shortcut and the "square of the prime" shortcut.
This solution uses a BitSet for compactness and speed, but in [[Groovy]], BitSet has full List semantics. It also uses both the "square root of the boundary" shortcut and the "square of the prime" shortcut.
<syntaxhighlight lang=groovy>def sievePrimes = { bound ->
<syntaxhighlight lang="groovy">def sievePrimes = { bound ->
def isPrime = new BitSet(bound)
def isPrime = new BitSet(bound)
isPrime[0..1] = false
isPrime[0..1] = false
Line 8,895: Line 8,895:


Test:
Test:
<syntaxhighlight lang=groovy>println sievePrimes(100)</syntaxhighlight>
<syntaxhighlight lang="groovy">println sievePrimes(100)</syntaxhighlight>


Output:
Output:
Line 8,902: Line 8,902:
=={{header|GW-BASIC}}==
=={{header|GW-BASIC}}==


<syntaxhighlight lang=qbasic>10 INPUT "ENTER NUMBER TO SEARCH TO: ";LIMIT
<syntaxhighlight lang="qbasic">10 INPUT "ENTER NUMBER TO SEARCH TO: ";LIMIT
20 DIM FLAGS(LIMIT)
20 DIM FLAGS(LIMIT)
30 FOR N = 2 TO SQR (LIMIT)
30 FOR N = 2 TO SQR (LIMIT)
Line 8,920: Line 8,920:
Mutable array of unboxed <code>Bool</code>s indexed by <code>Int</code>s:
Mutable array of unboxed <code>Bool</code>s indexed by <code>Int</code>s:


<syntaxhighlight lang=haskell>{-# LANGUAGE FlexibleContexts #-} -- too lazy to write contexts...
<syntaxhighlight lang="haskell">{-# LANGUAGE FlexibleContexts #-} -- too lazy to write contexts...
{-# OPTIONS_GHC -O2 #-}
{-# OPTIONS_GHC -O2 #-}


Line 8,968: Line 8,968:
Mutable array of unboxed <code>Bool</code>s indexed by <code>Int</code>s, representing odds only:
Mutable array of unboxed <code>Bool</code>s indexed by <code>Int</code>s, representing odds only:


<syntaxhighlight lang=haskell>import Control.Monad (forM_, when)
<syntaxhighlight lang="haskell">import Control.Monad (forM_, when)
import Control.Monad.ST
import Control.Monad.ST
import Data.Array.ST
import Data.Array.ST
Line 8,999: Line 8,999:
The reason for this alternate version is to have an accessible version of "odds only" that uses the same optimizations and is written in the same coding style as the basic version. This can be used by just substituting the following code for the function of the same name in the first base example above. Mutable array of unboxed <code>Bool</code>s indexed by <code>Int</code>s, representing odds only:
The reason for this alternate version is to have an accessible version of "odds only" that uses the same optimizations and is written in the same coding style as the basic version. This can be used by just substituting the following code for the function of the same name in the first base example above. Mutable array of unboxed <code>Bool</code>s indexed by <code>Int</code>s, representing odds only:


<syntaxhighlight lang=haskell>primesTo :: Int -> [Int] -- generate a list of primes to given limit...
<syntaxhighlight lang="haskell">primesTo :: Int -> [Int] -- generate a list of primes to given limit...
primesTo limit
primesTo limit
| limit < 2 = []
| limit < 2 = []
Line 9,035: Line 9,035:
===Immutable arrays===
===Immutable arrays===
Monolithic sieving array. ''Even'' numbers above 2 are pre-marked as composite, and sieving is done only by ''odd'' multiples of ''odd'' primes:
Monolithic sieving array. ''Even'' numbers above 2 are pre-marked as composite, and sieving is done only by ''odd'' multiples of ''odd'' primes:
<syntaxhighlight lang=haskell>import Data.Array.Unboxed
<syntaxhighlight lang="haskell">import Data.Array.Unboxed
primesToA m = sieve 3 (array (3,m) [(i,odd i) | i<-[3..m]] :: UArray Int Bool)
primesToA m = sieve 3 (array (3,m) [(i,odd i) | i<-[3..m]] :: UArray Int Bool)
Line 9,049: Line 9,049:


Works by segments between consecutive primes' squares. Should be the fastest of non-monadic code. ''Evens'' are entirely ignored:
Works by segments between consecutive primes' squares. Should be the fastest of non-monadic code. ''Evens'' are entirely ignored:
<syntaxhighlight lang=haskell>import Data.Array.Unboxed
<syntaxhighlight lang="haskell">import Data.Array.Unboxed


primesSA = 2 : prs ()
primesSA = 2 : prs ()
Line 9,065: Line 9,065:
===Basic list-based sieve===
===Basic list-based sieve===
Straightforward implementation of the sieve of Eratosthenes in its original bounded form. This finds primes in gaps between the composites, and composites as an enumeration of each prime's multiples.
Straightforward implementation of the sieve of Eratosthenes in its original bounded form. This finds primes in gaps between the composites, and composites as an enumeration of each prime's multiples.
<syntaxhighlight lang=haskell>primesTo m = eratos [2..m] where
<syntaxhighlight lang="haskell">primesTo m = eratos [2..m] where
eratos (p : xs)
eratos (p : xs)
| p*p > m = p : xs
| p*p > m = p : xs
Line 9,083: Line 9,083:
===Unbounded list based sieve===
===Unbounded list based sieve===
Unbounded, "naive", too eager to subtract (see above for the definition of <code>minus</code>):
Unbounded, "naive", too eager to subtract (see above for the definition of <code>minus</code>):
<syntaxhighlight lang=haskell>primesE = sieve [2..]
<syntaxhighlight lang="haskell">primesE = sieve [2..]
where
where
sieve (p:xs) = p : sieve (minus xs [p, p+p..])
sieve (p:xs) = p : sieve (minus xs [p, p+p..])
Line 9,090: Line 9,090:


The number of active streams can be limited to what's strictly necessary by postponement until the square of a prime is seen, getting a massive complexity improvement to better than <i>~ n<sup>1.5</sup></i> so it can get first million primes or so in a tolerable time:
The number of active streams can be limited to what's strictly necessary by postponement until the square of a prime is seen, getting a massive complexity improvement to better than <i>~ n<sup>1.5</sup></i> so it can get first million primes or so in a tolerable time:
<syntaxhighlight lang=haskell>primesPE = 2 : sieve [3..] 4 primesPE
<syntaxhighlight lang="haskell">primesPE = 2 : sieve [3..] 4 primesPE
where
where
sieve (x:xs) q (p:t)
sieve (x:xs) q (p:t)
Line 9,100: Line 9,100:


Transposing the workflow, going by segments between the consecutive squares of primes:
Transposing the workflow, going by segments between the consecutive squares of primes:
<syntaxhighlight lang=haskell>import Data.List (inits)
<syntaxhighlight lang="haskell">import Data.List (inits)


primesSE = 2 : sieve 3 4 (tail primesSE) (inits primesSE)
primesSE = 2 : sieve 3 4 (tail primesSE) (inits primesSE)
Line 9,113: Line 9,113:
The basic gradually-deepening left-leaning <code>(((a-b)-c)- ... )</code> workflow of <code>foldl minus a bs</code> above can be rearranged into the right-leaning <code>(a-(b+(c+ ... )))</code> workflow of <code>minus a (foldr union [] bs)</code>. This is the idea behind Richard Bird's unbounded code presented in [http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf M. O'Neill's article], equivalent to:
The basic gradually-deepening left-leaning <code>(((a-b)-c)- ... )</code> workflow of <code>foldl minus a bs</code> above can be rearranged into the right-leaning <code>(a-(b+(c+ ... )))</code> workflow of <code>minus a (foldr union [] bs)</code>. This is the idea behind Richard Bird's unbounded code presented in [http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf M. O'Neill's article], equivalent to:


<syntaxhighlight lang=haskell>primesB = _Y ( (2:) . minus [3..] . foldr (\p-> (p*p :) . union [p*p+p, p*p+2*p..]) [] )
<syntaxhighlight lang="haskell">primesB = _Y ( (2:) . minus [3..] . foldr (\p-> (p*p :) . union [p*p+p, p*p+2*p..]) [] )


-- = _Y ( (2:) . minus [3..] . _LU . map(\p-> [p*p, p*p+p..]) )
-- = _Y ( (2:) . minus [3..] . _LU . map(\p-> [p*p, p*p+p..]) )
Line 9,136: Line 9,136:


This merges primes' multiples streams in a ''tree''-like fashion, as a sequence of balanced trees of <code>union</code> nodes, likely achieving theoretical time complexity only a ''log n'' factor above the optimal ''n log n log (log n)'', for ''n'' primes produced. Indeed, empirically it runs at about ''~ n<sup>1.2</sup>'' (for producing first few million primes), similarly to priority-queue&ndash;based version of M. O'Neill's, and with very low space complexity too (not counting the produced sequence of course):
This merges primes' multiples streams in a ''tree''-like fashion, as a sequence of balanced trees of <code>union</code> nodes, likely achieving theoretical time complexity only a ''log n'' factor above the optimal ''n log n log (log n)'', for ''n'' primes produced. Indeed, empirically it runs at about ''~ n<sup>1.2</sup>'' (for producing first few million primes), similarly to priority-queue&ndash;based version of M. O'Neill's, and with very low space complexity too (not counting the produced sequence of course):
<syntaxhighlight lang=haskell>primes :: () -> [Int]
<syntaxhighlight lang="haskell">primes :: () -> [Int]
primes() = 2 : _Y ((3:) . gaps 5 . _U . map(\p-> [p*p, p*p+2*p..])) where
primes() = 2 : _Y ((3:) . gaps 5 . _U . map(\p-> [p*p, p*p+2*p..])) where
_Y g = g (_Y g) -- = g (g (g ( ... ))) non-sharing multistage fixpoint combinator
_Y g = g (_Y g) -- = g (g (g ( ... ))) non-sharing multistage fixpoint combinator
Line 9,151: Line 9,151:
====With Wheel====
====With Wheel====
Using <code>_U</code> defined above,
Using <code>_U</code> defined above,
<syntaxhighlight lang=haskell>primesW :: [Int]
<syntaxhighlight lang="haskell">primesW :: [Int]
primesW = [2,3,5,7] ++ _Y ( (11:) . gapsW 13 (tail wheel) . _U .
primesW = [2,3,5,7] ++ _Y ( (11:) . gapsW 13 (tail wheel) . _U .
map (\p->
map (\p->
Line 9,172: Line 9,172:
2. Improving the means to re-generate the position on the wheel for the recursive base primes without the use of `dropWhile`, etc. The below improved code uses a copy of the place in the wheel for each found base prime for ease of use in generating the composite number to-be-culled chains.
2. Improving the means to re-generate the position on the wheel for the recursive base primes without the use of `dropWhile`, etc. The below improved code uses a copy of the place in the wheel for each found base prime for ease of use in generating the composite number to-be-culled chains.


<syntaxhighlight lang=haskell>-- autogenerates wheel primes, first sieve prime, and gaps
<syntaxhighlight lang="haskell">-- autogenerates wheel primes, first sieve prime, and gaps
wheelGen :: Int -> ([Int],Int,[Int])
wheelGen :: Int -> ([Int],Int,[Int])
wheelGen n = loop 1 3 [2] [2] where
wheelGen n = loop 1 3 [2] [2] where
Line 9,220: Line 9,220:


In order to implement a Priority Queue version with Haskell, an efficient Priority Queue, which is not part of the standard Haskell libraries, is required. A Min Heap implementation is likely best suited for this task in providing the most efficient frequently used peeks of the next item in the queue and replacement of the first item in the queue (not using a "pop" followed by a "push) with "pop" operations then not used at all, and "push" operations used relatively infrequently. Judging by O'Neill's use of an efficient "deleteMinAndInsert" operation which she states "(We provide deleteMinAndInsert becausea heap-based implementation can support this operation with considerably less rearrangement than a deleteMin followed by an insert.)", which statement is true for a Min Heap Priority Queue and not others, and her reference to a priority queue by (Paulson, 1996), the queue she used is likely the one as provided as a simple true functional [http://rosettacode.org/wiki/Priority_queue#Haskell Min Heap implementation on RosettaCode], from which the essential functions are reproduced here:
In order to implement a Priority Queue version with Haskell, an efficient Priority Queue, which is not part of the standard Haskell libraries, is required. A Min Heap implementation is likely best suited for this task in providing the most efficient frequently used peeks of the next item in the queue and replacement of the first item in the queue (not using a "pop" followed by a "push) with "pop" operations then not used at all, and "push" operations used relatively infrequently. Judging by O'Neill's use of an efficient "deleteMinAndInsert" operation which she states "(We provide deleteMinAndInsert becausea heap-based implementation can support this operation with considerably less rearrangement than a deleteMin followed by an insert.)", which statement is true for a Min Heap Priority Queue and not others, and her reference to a priority queue by (Paulson, 1996), the queue she used is likely the one as provided as a simple true functional [http://rosettacode.org/wiki/Priority_queue#Haskell Min Heap implementation on RosettaCode], from which the essential functions are reproduced here:
<syntaxhighlight lang=haskell>data PriorityQ k v = Mt
<syntaxhighlight lang="haskell">data PriorityQ k v = Mt
| Br !k v !(PriorityQ k v) !(PriorityQ k v)
| Br !k v !(PriorityQ k v) !(PriorityQ k v)
deriving (Eq, Ord, Read, Show)
deriving (Eq, Ord, Read, Show)
Line 9,254: Line 9,254:


The following code is O'Neill's original odds-only code (without wheel factorization) from her paper slightly adjusted as per the requirements of this Min Heap implementation as laid out above; note the `seq` adjustments to the "adjust" function to make the evaluation of the entry tuple more strict for better efficiency:
The following code is O'Neill's original odds-only code (without wheel factorization) from her paper slightly adjusted as per the requirements of this Min Heap implementation as laid out above; note the `seq` adjustments to the "adjust" function to make the evaluation of the entry tuple more strict for better efficiency:
<syntaxhighlight lang=haskell>-- (c) 2006-2007 Melissa O'Neill. Code may be used freely so long as
<syntaxhighlight lang="haskell">-- (c) 2006-2007 Melissa O'Neill. Code may be used freely so long as
-- this copyright message is retained and changed versions of the file
-- this copyright message is retained and changed versions of the file
-- are clearly marked.
-- are clearly marked.
Line 9,282: Line 9,282:


The following code is adjusted to reduce the amount of lazy list processing and to add a secondary base primes stream (or a succession of streams when the combinator is used) so as to overcome the above problems and reduce memory consumption to only that required for the primes below the square root of the currently sieved number; using this means that 32-bit Int's are sufficient for a reasonable range and memory requirements become relatively negligible:
The following code is adjusted to reduce the amount of lazy list processing and to add a secondary base primes stream (or a succession of streams when the combinator is used) so as to overcome the above problems and reduce memory consumption to only that required for the primes below the square root of the currently sieved number; using this means that 32-bit Int's are sufficient for a reasonable range and memory requirements become relatively negligible:
<syntaxhighlight lang=haskell>primesPQx :: () -> [Int]
<syntaxhighlight lang="haskell">primesPQx :: () -> [Int]
primesPQx() = 2 : _Y ((3 :) . sieve 5 emptyPQ 9) -- initBasePrms
primesPQx() = 2 : _Y ((3 :) . sieve 5 emptyPQ 9) -- initBasePrms
where
where
Line 9,309: Line 9,309:
Since the Tree-Folding version above includes the minor changes to work with a factorization wheel, this should have the same minor modifications for comparison purposes, with the code as follows:
Since the Tree-Folding version above includes the minor changes to work with a factorization wheel, this should have the same minor modifications for comparison purposes, with the code as follows:


<syntaxhighlight lang=haskell>-- Note: this code segment uses the same wheelGen as the Tree-Folding version...
<syntaxhighlight lang="haskell">-- Note: this code segment uses the same wheelGen as the Tree-Folding version...


primesPQWheeled :: () -> [Int]
primesPQWheeled :: () -> [Int]
Line 9,346: Line 9,346:


All of the above unbounded sieves are quite limited in practical sieving range due to the large constant factor overheads in computation, making them mostly just interesting intellectual exercises other than for small ranges of up to about the first million to ten million primes; the following '''"odds-only"''' page-segmented version using (bit-packed internally) mutable unboxed arrays is about 50 times faster than the fastest of the above algorithms for ranges of about that and higher, making it practical for the first several hundred million primes:
All of the above unbounded sieves are quite limited in practical sieving range due to the large constant factor overheads in computation, making them mostly just interesting intellectual exercises other than for small ranges of up to about the first million to ten million primes; the following '''"odds-only"''' page-segmented version using (bit-packed internally) mutable unboxed arrays is about 50 times faster than the fastest of the above algorithms for ranges of about that and higher, making it practical for the first several hundred million primes:
<syntaxhighlight lang=haskell>{-# OPTIONS_GHC -O2 -fllvm #-} -- use LLVM for about double speed!
<syntaxhighlight lang="haskell">{-# OPTIONS_GHC -O2 -fllvm #-} -- use LLVM for about double speed!


import Data.Int ( Int64 )
import Data.Int ( Int64 )
Line 9,405: Line 9,405:
To show the limitations of the individual prime enumeration, the following code has been refactored from the above to provide an alternate very fast method of counting the unset bits in the culled array (the primes = none composite) using a CPU native pop count instruction:
To show the limitations of the individual prime enumeration, the following code has been refactored from the above to provide an alternate very fast method of counting the unset bits in the culled array (the primes = none composite) using a CPU native pop count instruction:


<syntaxhighlight lang=haskell>{-# LANGUAGE FlexibleContexts #-}
<syntaxhighlight lang="haskell">{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -fllvm #-} -- use LLVM for about double speed!
{-# OPTIONS_GHC -O2 -fllvm #-} -- use LLVM for about double speed!


Line 9,524: Line 9,524:
===APL-style===
===APL-style===
Rolling set subtraction over the rolling element-wise addition on integers. Basic, slow, worse than quadratic in the number of primes produced, empirically:
Rolling set subtraction over the rolling element-wise addition on integers. Basic, slow, worse than quadratic in the number of primes produced, empirically:
<syntaxhighlight lang=haskell>zipWith (flip (!!)) [0..] -- or: take n . last . take n ...
<syntaxhighlight lang="haskell">zipWith (flip (!!)) [0..] -- or: take n . last . take n ...
. scanl1 minus
. scanl1 minus
. scanl1 (zipWith (+)) $ repeat [2..]</syntaxhighlight>
. scanl1 (zipWith (+)) $ repeat [2..]</syntaxhighlight>
Or, a wee bit faster:
Or, a wee bit faster:
<syntaxhighlight lang=haskell>unfoldr (\(a:b:t) -> Just . (head &&& (:t) . (`minus` b)
<syntaxhighlight lang="haskell">unfoldr (\(a:b:t) -> Just . (head &&& (:t) . (`minus` b)
. tail) $ a)
. tail) $ a)
. scanl1 (zipWith (+)) $ repeat [2..]</syntaxhighlight>
. scanl1 (zipWith (+)) $ repeat [2..]</syntaxhighlight>
A bit optimized, much faster, with better complexity,
A bit optimized, much faster, with better complexity,
<syntaxhighlight lang=haskell>tail . concat
<syntaxhighlight lang="haskell">tail . concat
. unfoldr (\(a:b:t) -> Just . second ((:t) . (`minus` b))
. unfoldr (\(a:b:t) -> Just . second ((:t) . (`minus` b))
. span (< head b) $ a)
. span (< head b) $ a)
Line 9,539: Line 9,539:


getting nearer to the functional equivalent of the <code>primesPE</code> above, i.e.
getting nearer to the functional equivalent of the <code>primesPE</code> above, i.e.
<syntaxhighlight lang=haskell>fix ( (2:) . concat
<syntaxhighlight lang="haskell">fix ( (2:) . concat
. unfoldr (\(a:b:t) -> Just . second ((:t) . (`minus` b))
. unfoldr (\(a:b:t) -> Just . second ((:t) . (`minus` b))
. span (< head b) $ a)
. span (< head b) $ a)
Line 9,545: Line 9,545:


An illustration:
An illustration:
<syntaxhighlight lang=haskell>> mapM_ (print . take 15) $ take 10 $ scanl1 (zipWith(+)) $ repeat [2..]
<syntaxhighlight lang="haskell">> mapM_ (print . take 15) $ take 10 $ scanl1 (zipWith(+)) $ repeat [2..]
[ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
[ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
[ 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32]
[ 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32]
Line 9,570: Line 9,570:


=={{header|HicEst}}==
=={{header|HicEst}}==
<syntaxhighlight lang=hicest>REAL :: N=100, sieve(N)
<syntaxhighlight lang="hicest">REAL :: N=100, sieve(N)


sieve = $ > 1 ! = 0 1 1 1 1 ...
sieve = $ > 1 ! = 0 1 1 1 1 ...
Line 9,586: Line 9,586:


=={{header|Hoon}}==
=={{header|Hoon}}==
<syntaxhighlight lang=hoon>:: Find primes by the sieve of Eratosthenes
<syntaxhighlight lang="hoon">:: Find primes by the sieve of Eratosthenes
!:
!:
|= end=@ud
|= end=@ud
Line 9,597: Line 9,597:


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
<syntaxhighlight lang=Icon> procedure main()
<syntaxhighlight lang="icon"> procedure main()
sieve(100)
sieve(100)
end
end
Line 9,610: Line 9,610:


Alternatively using sets
Alternatively using sets
<syntaxhighlight lang=Icon> procedure main()
<syntaxhighlight lang="icon"> procedure main()
sieve(100)
sieve(100)
end
end
Line 9,626: Line 9,626:
{{eff note|J|i.&.(p:inv) }}
{{eff note|J|i.&.(p:inv) }}


Implementation:<syntaxhighlight lang=J>sieve=: {{
Implementation:<syntaxhighlight lang="j">sieve=: {{
r=. 0#t=. y# j=.1
r=. 0#t=. y# j=.1
while. y>j=.j+1 do.
while. y>j=.j+1 do.
Line 9,638: Line 9,638:
Example:
Example:


<syntaxhighlight lang=J> sieve 100
<syntaxhighlight lang="j"> sieve 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97</syntaxhighlight>
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97</syntaxhighlight>


To see into how this works, we can change the definition:
To see into how this works, we can change the definition:


<syntaxhighlight lang=J>sieve=: {{
<syntaxhighlight lang="j">sieve=: {{
r=. 0#t=. y# j=.1
r=. 0#t=. y# j=.1
while. y>j=.j+1 do.
while. y>j=.j+1 do.
Line 9,653: Line 9,653:
}}</syntaxhighlight>
}}</syntaxhighlight>


And go:<syntaxhighlight lang=J> sieve 10
And go:<syntaxhighlight lang="j"> sieve 10
┌─┬───────────────────┬───────────────────┐
┌─┬───────────────────┬───────────────────┐
│2│1 0 1 0 1 0 1 0 1 0│0 1 0 1 0 1 0 1 0 1│
│2│1 0 1 0 1 0 1 0 1 0│0 1 0 1 0 1 0 1 0 1│
Line 9,677: Line 9,677:
This is based off the Python version.
This is based off the Python version.


<syntaxhighlight lang=janet>(defn primes-before
<syntaxhighlight lang="janet">(defn primes-before
"Gives all the primes < limit"
"Gives all the primes < limit"
[limit]
[limit]
Line 9,703: Line 9,703:
=={{header|Java}}==
=={{header|Java}}==
{{works with|Java|1.5+}}
{{works with|Java|1.5+}}
<syntaxhighlight lang=java5>import java.util.LinkedList;
<syntaxhighlight lang="java5">import java.util.LinkedList;


public class Sieve{
public class Sieve{
Line 9,727: Line 9,727:


To optimize by testing only odd numbers, replace the loop marked "unoptimized" with these lines:
To optimize by testing only odd numbers, replace the loop marked "unoptimized" with these lines:
<syntaxhighlight lang=java5>nums.add(2);
<syntaxhighlight lang="java5">nums.add(2);
for(int i = 3;i <= n;i += 2){
for(int i = 3;i <= n;i += 2){
nums.add(i);
nums.add(i);
Line 9,733: Line 9,733:


Version using List:
Version using List:
<syntaxhighlight lang=java5>
<syntaxhighlight lang="java5">
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.List;
Line 9,754: Line 9,754:
</syntaxhighlight>
</syntaxhighlight>
Version using a BitSet:
Version using a BitSet:
<syntaxhighlight lang=java5>import java.util.LinkedList;
<syntaxhighlight lang="java5">import java.util.LinkedList;
import java.util.BitSet;
import java.util.BitSet;


Line 9,773: Line 9,773:


Version using a TreeSet:
Version using a TreeSet:
<syntaxhighlight lang=java5>import java.util.Set;
<syntaxhighlight lang="java5">import java.util.Set;
import java.util.TreeSet;
import java.util.TreeSet;


Line 9,806: Line 9,806:
{{trans|Python}}
{{trans|Python}}
{{works with|Java|1.5+}}
{{works with|Java|1.5+}}
<syntaxhighlight lang=java5>import java.util.Iterator;
<syntaxhighlight lang="java5">import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.PriorityQueue;
import java.math.BigInteger;
import java.math.BigInteger;
Line 9,878: Line 9,878:
{{trans|Python}}
{{trans|Python}}
{{works with|Java|1.5+}}
{{works with|Java|1.5+}}
<syntaxhighlight lang=java5>import java.util.Iterator;
<syntaxhighlight lang="java5">import java.util.Iterator;
import java.util.HashMap;
import java.util.HashMap;
Line 9,946: Line 9,946:
{{trans|JavaScript}}
{{trans|JavaScript}}
{{works with|Java|1.5+}}
{{works with|Java|1.5+}}
<syntaxhighlight lang=java5>import java.util.Iterator;
<syntaxhighlight lang="java5">import java.util.Iterator;
import java.util.ArrayList;
import java.util.ArrayList;


Line 10,028: Line 10,028:


=={{header|JavaScript}}==
=={{header|JavaScript}}==
<syntaxhighlight lang=javascript>function eratosthenes(limit) {
<syntaxhighlight lang="javascript">function eratosthenes(limit) {
var primes = [];
var primes = [];
if (limit >= 2) {
if (limit >= 2) {
Line 10,060: Line 10,060:
Substituting the following code for the function for '''an odds-only algorithm using bit packing''' for the array produces code that is many times faster than the above:
Substituting the following code for the function for '''an odds-only algorithm using bit packing''' for the array produces code that is many times faster than the above:


<syntaxhighlight lang=javascript>function eratosthenes(limit) {
<syntaxhighlight lang="javascript">function eratosthenes(limit) {
var prms = [];
var prms = [];
if (limit >= 2) prms = [2];
if (limit >= 2) prms = [2];
Line 10,085: Line 10,085:
While the above code is quite fast especially using an efficient JavaScript engine such as Google Chrome's V8, it isn't as elegant as it could be using the features of the new EcmaScript6 specification when it comes out about the end of 2014 and when JavaScript engines including those of browsers implement that standard in that we might choose to implement an incremental algorithm iterators or generators similar to as implemented in Python or F# (yield). Meanwhile, we can emulate some of those features by using a simulation of an iterator class (which is easier than using a call-back function) for an '''"infinite" generator based on an Object dictionary''' as in the following odds-only code written as a JavaScript class:
While the above code is quite fast especially using an efficient JavaScript engine such as Google Chrome's V8, it isn't as elegant as it could be using the features of the new EcmaScript6 specification when it comes out about the end of 2014 and when JavaScript engines including those of browsers implement that standard in that we might choose to implement an incremental algorithm iterators or generators similar to as implemented in Python or F# (yield). Meanwhile, we can emulate some of those features by using a simulation of an iterator class (which is easier than using a call-back function) for an '''"infinite" generator based on an Object dictionary''' as in the following odds-only code written as a JavaScript class:


<syntaxhighlight lang=javascript>var SoEIncClass = (function () {
<syntaxhighlight lang="javascript">var SoEIncClass = (function () {
function SoEIncClass() {
function SoEIncClass() {
this.n = 0;
this.n = 0;
Line 10,130: Line 10,130:
The above code can be used to find the nth prime (which would require estimating the required range limit using the previous fixed range code) by using the following code:
The above code can be used to find the nth prime (which would require estimating the required range limit using the previous fixed range code) by using the following code:


<syntaxhighlight lang=javascript>var gen = new SoEIncClass();
<syntaxhighlight lang="javascript">var gen = new SoEIncClass();
for (var i = 1; i < 1000000; i++, gen.next());
for (var i = 1; i < 1000000; i++, gen.next());
var prime = gen.next();
var prime = gen.next();
Line 10,146: Line 10,146:
This can be implemented as '''an "infinite" odds-only generator using page segmentation''' for a considerable speed-up with the alternate JavaScript class code as follows:
This can be implemented as '''an "infinite" odds-only generator using page segmentation''' for a considerable speed-up with the alternate JavaScript class code as follows:


<syntaxhighlight lang=javascript>var SoEPgClass = (function () {
<syntaxhighlight lang="javascript">var SoEPgClass = (function () {
function SoEPgClass() {
function SoEPgClass() {
this.bi = -1; // constructor resets the enumeration to start...
this.bi = -1; // constructor resets the enumeration to start...
Line 10,209: Line 10,209:


function is copy-pasted from above to produce a webpage version for beginners:
function is copy-pasted from above to produce a webpage version for beginners:
<syntaxhighlight lang=javascript>
<syntaxhighlight lang="javascript">
<script>
<script>
function eratosthenes(limit) {
function eratosthenes(limit) {
Line 10,243: Line 10,243:


=={{header|JOVIAL}}==
=={{header|JOVIAL}}==
<syntaxhighlight lang=JOVIAL>
<syntaxhighlight lang="jovial">
START
START
FILE MYOUTPUT ... $ ''Insufficient information to complete this declaration''
FILE MYOUTPUT ... $ ''Insufficient information to complete this declaration''
Line 10,304: Line 10,304:
Short and sweet ...
Short and sweet ...


<syntaxhighlight lang=jq># Denoting the input by $n, which is assumed to be a positive integer,
<syntaxhighlight lang="jq"># Denoting the input by $n, which is assumed to be a positive integer,
# eratosthenes/0 produces an array of primes less than or equal to $n:
# eratosthenes/0 produces an array of primes less than or equal to $n:
def eratosthenes:
def eratosthenes:
Line 10,320: Line 10,320:
| map(select(.));</syntaxhighlight>
| map(select(.));</syntaxhighlight>
'''Examples''':
'''Examples''':
<syntaxhighlight lang=jq>100 | eratosthenes</syntaxhighlight>
<syntaxhighlight lang="jq">100 | eratosthenes</syntaxhighlight>
{{out}}
{{out}}


[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
<syntaxhighlight lang=jq>1e7 | eratosthenes | length</syntaxhighlight>
<syntaxhighlight lang="jq">1e7 | eratosthenes | length</syntaxhighlight>
{{out}}
{{out}}
664579
664579
Line 10,331: Line 10,331:


Started with 2 already in the array, and then test only for odd numbers and push the prime ones onto the array.
Started with 2 already in the array, and then test only for odd numbers and push the prime ones onto the array.
<syntaxhighlight lang=julia># Returns an array of positive prime numbers less than or equal to lim
<syntaxhighlight lang="julia"># Returns an array of positive prime numbers less than or equal to lim
function sieve(lim :: Int)
function sieve(lim :: Int)
if lim < 2 return [] end
if lim < 2 return [] end
Line 10,354: Line 10,354:
Alternate version using <code>findall</code> to get all primes at once in the end
Alternate version using <code>findall</code> to get all primes at once in the end


<syntaxhighlight lang=julia>function sieve(n :: Int)
<syntaxhighlight lang="julia">function sieve(n :: Int)
isprime = trues(n)
isprime = trues(n)
isprime[1] = false
isprime[1] = false
Line 10,382: Line 10,382:
If one is going to "crib" the MatLab algorithm as above, one may as well do it using odds-only as per the MatLab built-in. The following alternate code improves on the "Alternate" example above by making it sieve odds-only and adjusting the result array contents after to suit:
If one is going to "crib" the MatLab algorithm as above, one may as well do it using odds-only as per the MatLab built-in. The following alternate code improves on the "Alternate" example above by making it sieve odds-only and adjusting the result array contents after to suit:


<syntaxhighlight lang=julia>function sieve2(n :: Int)
<syntaxhighlight lang="julia">function sieve2(n :: Int)
ni = (n - 1) ÷ 2
ni = (n - 1) ÷ 2
isprime = trues(ni)
isprime = trues(ni)
Line 10,409: Line 10,409:
The creation of an output results array is not necessary if the purpose is just to scan across the resulting primes once, they can be output using an iterator (from a `BitArray`) as in the following odds-only code:
The creation of an output results array is not necessary if the purpose is just to scan across the resulting primes once, they can be output using an iterator (from a `BitArray`) as in the following odds-only code:


<syntaxhighlight lang=julia>const Prime = UInt64
<syntaxhighlight lang="julia">const Prime = UInt64


struct Primes
struct Primes
Line 10,461: Line 10,461:
for which using the following code:
for which using the following code:


<syntaxhighlight lang=julia>function bench()
<syntaxhighlight lang="julia">function bench()
@time length(Primes(100)) # warm up JIT
@time length(Primes(100)) # warm up JIT
# println(@time count(x->true, Primes(1000000000))) # about 1.5 seconds slower counting over iteration
# println(@time count(x->true, Primes(1000000000))) # about 1.5 seconds slower counting over iteration
Line 10,480: Line 10,480:
For any kind of reasonably large range such as a billion, a page segmented version should be used with the pages sized to the CPU caches for much better memory access times. As well, the following odds-only example uses a custom bit packing algorithm for a further two times speed-up, also reducing the memory allocation delays by reusing the sieve buffers when possible (usually possible):
For any kind of reasonably large range such as a billion, a page segmented version should be used with the pages sized to the CPU caches for much better memory access times. As well, the following odds-only example uses a custom bit packing algorithm for a further two times speed-up, also reducing the memory allocation delays by reusing the sieve buffers when possible (usually possible):


<syntaxhighlight lang=julia>const Prime = UInt64
<syntaxhighlight lang="julia">const Prime = UInt64
const BasePrime = UInt32
const BasePrime = UInt32
const BasePrimesArray = Array{BasePrime,1}
const BasePrimesArray = Array{BasePrime,1}
Line 10,681: Line 10,681:
When tested with the following code:
When tested with the following code:


<syntaxhighlight lang=julia>function bench()
<syntaxhighlight lang="julia">function bench()
print("( ")
print("( ")
for p in PrimesPaged() p > 100 && break; print(p, " ") end
for p in PrimesPaged() p > 100 && break; print(p, " ") end
Line 10,715: Line 10,715:
One of the best simple purely functional Sieve of Eratosthenes algorithms is the infinite tree folding sequence algorithm as implemented in Haskell. As Julia does not have a standard LazyList implementation or library and as a full memoizing lazy list is not required for this algorithm, the following odds-only code implements the rudiments of a Co-Inductive Stream (CIS) in its implementation:
One of the best simple purely functional Sieve of Eratosthenes algorithms is the infinite tree folding sequence algorithm as implemented in Haskell. As Julia does not have a standard LazyList implementation or library and as a full memoizing lazy list is not required for this algorithm, the following odds-only code implements the rudiments of a Co-Inductive Stream (CIS) in its implementation:


<syntaxhighlight lang=julia>const Thunk = Function # can't define other than as a generalized Function
<syntaxhighlight lang="julia">const Thunk = Function # can't define other than as a generalized Function


struct CIS{T}
struct CIS{T}
Line 10,761: Line 10,761:
when tested with the following:
when tested with the following:


<syntaxhighlight lang=julia>@time let count = 0; for p in treefoldingprimes() p > 1000000 && break; count += 1 end; count end</syntaxhighlight>
<syntaxhighlight lang="julia">@time let count = 0; for p in treefoldingprimes() p > 1000000 && break; count += 1 end; count end</syntaxhighlight>


it outputs the following:
it outputs the following:
Line 10,776: Line 10,776:
To gain some extra speed above the purely functional algorithm above, the Python'ish version as a mutable iterator embedding a mutable standard base Dictionary can be used. The following version uses a secondary delayed injection stream of "base" primes defined recursively to provide the successions of composite values in the Dictionary to be used for sieving:
To gain some extra speed above the purely functional algorithm above, the Python'ish version as a mutable iterator embedding a mutable standard base Dictionary can be used. The following version uses a secondary delayed injection stream of "base" primes defined recursively to provide the successions of composite values in the Dictionary to be used for sieving:


<syntaxhighlight lang=Julia>const Prime = UInt64
<syntaxhighlight lang="julia">const Prime = UInt64
abstract type PrimesDictAbstract end # used for forward reference
abstract type PrimesDictAbstract end # used for forward reference
mutable struct PrimesDict <: PrimesDictAbstract
mutable struct PrimesDict <: PrimesDictAbstract
Line 10,819: Line 10,819:


=={{header|Klingphix}}==
=={{header|Klingphix}}==
<syntaxhighlight lang=Klingphix>include ..\Utilitys.tlhy
<syntaxhighlight lang="klingphix">include ..\Utilitys.tlhy


%limit %i
%limit %i
Line 10,832: Line 10,832:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<syntaxhighlight lang=kotlin>import kotlin.math.sqrt
<syntaxhighlight lang="kotlin">import kotlin.math.sqrt


fun sieve(max: Int): List<Int> {
fun sieve(max: Int): List<Int> {
Line 10,854: Line 10,854:
The following code overcomes most of those problems: It only culls odd composites; it culls a bit-packed primitive array (also saving memory); It uses tailcall recursive functions for the loops, which are compiled into simple loops. It also outputs the results as an enumeration, which isn't fast but does not consume any more memory than the culling array. In this way, the program is only limited in sieving range by the maximum size limit of the culling array, although as it grows larger than the CPU cache sizes, it loses greatly in speed; however, that doesn't matter so much if just enumerating the results.
The following code overcomes most of those problems: It only culls odd composites; it culls a bit-packed primitive array (also saving memory); It uses tailcall recursive functions for the loops, which are compiled into simple loops. It also outputs the results as an enumeration, which isn't fast but does not consume any more memory than the culling array. In this way, the program is only limited in sieving range by the maximum size limit of the culling array, although as it grows larger than the CPU cache sizes, it loses greatly in speed; however, that doesn't matter so much if just enumerating the results.


<syntaxhighlight lang=kotlin>fun primesOdds(rng: Int): Iterable<Int> {
<syntaxhighlight lang="kotlin">fun primesOdds(rng: Int): Iterable<Int> {
val topi = (rng - 3) shr 1
val topi = (rng - 3) shr 1
val lstw = topi shr 5
val lstw = topi shr 5
Line 10,911: Line 10,911:
Ah, one might say, for such a trivial range one writes for conciseness and not for speed. Well, I say, one can still save memory and some time using odds-only and a bit-packed array, but write very clear and concise (but slower) code using nothing but higher order functions and function calling. The following code using such techniques can use the same "main" function for the same output but is about two times slower, mostly due to the extra time spent making (nested) function calls, including the function calls necessary for enumeration. Note that the effect of using the "(l .. h).forEach { .. }" is the same as the "for i in l .. h { .. }" as both use an iteration across the range but the second is just syntax sugar to make it look more imperative:
Ah, one might say, for such a trivial range one writes for conciseness and not for speed. Well, I say, one can still save memory and some time using odds-only and a bit-packed array, but write very clear and concise (but slower) code using nothing but higher order functions and function calling. The following code using such techniques can use the same "main" function for the same output but is about two times slower, mostly due to the extra time spent making (nested) function calls, including the function calls necessary for enumeration. Note that the effect of using the "(l .. h).forEach { .. }" is the same as the "for i in l .. h { .. }" as both use an iteration across the range but the second is just syntax sugar to make it look more imperative:


<syntaxhighlight lang=kotlin>fun primesOdds(rng: Int): Iterable<Int> {
<syntaxhighlight lang="kotlin">fun primesOdds(rng: Int): Iterable<Int> {
val topi = (rng - 3) / 2 //convert to nearest index
val topi = (rng - 3) / 2 //convert to nearest index
val size = topi / 32 + 1 //word size to include index
val size = topi / 32 + 1 //word size to include index
Line 10,927: Line 10,927:
The trouble with the above version is that, at least for Kotlin version 1.0, the ".filter" and ".map" extension functions for Iterable<Int> create Java "ArrayList"'s as their output (which are wrapped to return the Kotlin "List<Int>" interface), thus take a considerable amount of memory worse than the first version (using an ArrayList to store the resulting primes), since as the calculations are chained to ".map", require a second ArrayList of up to the same size while the mapping is being done. The following version uses Sequences , which aren't backed by any permanent structure, but it is another small factor slower due to the nested function calls:
The trouble with the above version is that, at least for Kotlin version 1.0, the ".filter" and ".map" extension functions for Iterable<Int> create Java "ArrayList"'s as their output (which are wrapped to return the Kotlin "List<Int>" interface), thus take a considerable amount of memory worse than the first version (using an ArrayList to store the resulting primes), since as the calculations are chained to ".map", require a second ArrayList of up to the same size while the mapping is being done. The following version uses Sequences , which aren't backed by any permanent structure, but it is another small factor slower due to the nested function calls:


<syntaxhighlight lang=kotlin>fun primesOdds(rng: Int): Iterable<Int> {
<syntaxhighlight lang="kotlin">fun primesOdds(rng: Int): Iterable<Int> {
val topi = (rng - 3) / 2 //convert to nearest index
val topi = (rng - 3) / 2 //convert to nearest index
val size = topi / 32 + 1 //word size to include index
val size = topi / 32 + 1 //word size to include index
Line 10,948: Line 10,948:


The following Sieve of Eratosthenes is not purely functional in that it uses a Mutable HashMap to store the state of succeeding composite numbers to be skipped over, but embodies the principles of an incremental implementation of the Sieve of Eratosthenes sieving odds-only and is faster than most incremental sieves due to using mutability. As with the fastest of this kind of sieve, it uses a delayed secondary primes feed as a source of base primes to generate the composite number progressions. The code as follows:
The following Sieve of Eratosthenes is not purely functional in that it uses a Mutable HashMap to store the state of succeeding composite numbers to be skipped over, but embodies the principles of an incremental implementation of the Sieve of Eratosthenes sieving odds-only and is faster than most incremental sieves due to using mutability. As with the fastest of this kind of sieve, it uses a delayed secondary primes feed as a source of base primes to generate the composite number progressions. The code as follows:
<syntaxhighlight lang=kotlin>fun primesHM(): Sequence<Int> = sequence {
<syntaxhighlight lang="kotlin">fun primesHM(): Sequence<Int> = sequence {
yield(2)
yield(2)
fun oddprms(): Sequence<Int> = sequence {
fun oddprms(): Sequence<Int> = sequence {
Line 10,983: Line 10,983:


At about 370 clock cycles per culling operation (about 3,800 cycles per prime) on my tablet class Intel CPU, this is not blazing fast but adequate for ranges of a few millions to a hundred million and thus fine for doing things like solving Euler problems. For instance, Euler Problem 10 of summing the primes to two million can be done with the following "one-liner":
At about 370 clock cycles per culling operation (about 3,800 cycles per prime) on my tablet class Intel CPU, this is not blazing fast but adequate for ranges of a few millions to a hundred million and thus fine for doing things like solving Euler problems. For instance, Euler Problem 10 of summing the primes to two million can be done with the following "one-liner":
<syntaxhighlight lang=kotlin>primesHM().takeWhile { it <= 2_000_000 }.map { it.toLong() }.sum()</syntaxhighlight>
<syntaxhighlight lang="kotlin">primesHM().takeWhile { it <= 2_000_000 }.map { it.toLong() }.sum()</syntaxhighlight>


to output the correct answer of the following in about 270 milliseconds for my Intel x5-Z8350 at 1.92 Gigahertz:
to output the correct answer of the following in about 270 milliseconds for my Intel x5-Z8350 at 1.92 Gigahertz:
Line 10,995: Line 10,995:
{{trans|Haskell}}
{{trans|Haskell}}


<syntaxhighlight lang=kotlin>data class CIS<T>(val head: T, val tailf: () -> CIS<T>) {
<syntaxhighlight lang="kotlin">data class CIS<T>(val head: T, val tailf: () -> CIS<T>) {
fun toSequence() = generateSequence(this) { it.tailf() } .map { it.head }
fun toSequence() = generateSequence(this) { it.tailf() } .map { it.head }
}
}
Line 11,039: Line 11,039:


The very fastest implementations of a primes sieve are all based on bit-packed mutable arrays which can be made unbounded by setting them up so that they are a succession of sieved bit-packed arrays that have been culled of composites. The following code is an odds=only implementation that, again, uses a secondary feed of base primes that is only expanded as necessary (in this case memoized by a rudimentary lazy list structure to avoid recalculation for every base primes sweep per page segment):
The very fastest implementations of a primes sieve are all based on bit-packed mutable arrays which can be made unbounded by setting them up so that they are a succession of sieved bit-packed arrays that have been culled of composites. The following code is an odds=only implementation that, again, uses a secondary feed of base primes that is only expanded as necessary (in this case memoized by a rudimentary lazy list structure to avoid recalculation for every base primes sweep per page segment):
<syntaxhighlight lang=kotlin>internal typealias Prime = Long
<syntaxhighlight lang="kotlin">internal typealias Prime = Long
internal typealias BasePrime = Int
internal typealias BasePrime = Int
internal typealias BasePrimeArray = IntArray
internal typealias BasePrimeArray = IntArray
Line 11,227: Line 11,227:


=={{header|Lambdatalk}}==
=={{header|Lambdatalk}}==
<syntaxhighlight lang=Scheme>
<syntaxhighlight lang="scheme">
{def sieve
{def sieve


Line 11,263: Line 11,263:
=={{header|langur}}==
=={{header|langur}}==
{{trans|D}}
{{trans|D}}
<syntaxhighlight lang=langur>val .sieve = f(.limit) {
<syntaxhighlight lang="langur">val .sieve = f(.limit) {
if .limit < 2 {
if .limit < 2 {
return []
return []
Line 11,288: Line 11,288:


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
<syntaxhighlight lang=lb> 'Notice that arrays are globally visible to functions.
<syntaxhighlight lang="lb"> 'Notice that arrays are globally visible to functions.
'The sieve() function uses the flags() array.
'The sieve() function uses the flags() array.
'This is a Sieve benchmark adapted from BYTE 1985
'This is a Sieve benchmark adapted from BYTE 1985
Line 11,315: Line 11,315:


=={{header|Limbo}}==
=={{header|Limbo}}==
<syntaxhighlight lang=Go>implement Sieve;
<syntaxhighlight lang="go">implement Sieve;


include "sys.m";
include "sys.m";
Line 11,358: Line 11,358:


=={{header|Lingo}}==
=={{header|Lingo}}==
<syntaxhighlight lang=Lingo>-- parent script "sieve"
<syntaxhighlight lang="lingo">-- parent script "sieve"
property _sieve
property _sieve


Line 11,400: Line 11,400:
end</syntaxhighlight>
end</syntaxhighlight>


<syntaxhighlight lang=Lingo>sieve = script("sieve").new()
<syntaxhighlight lang="lingo">sieve = script("sieve").new()
put sieve.getPrimes(100)</syntaxhighlight>
put sieve.getPrimes(100)</syntaxhighlight>


Line 11,409: Line 11,409:


=={{header|LiveCode}}==
=={{header|LiveCode}}==
<syntaxhighlight lang=LiveCode>function sieveE int
<syntaxhighlight lang="livecode">function sieveE int
set itemdel to comma
set itemdel to comma
local sieve
local sieve
Line 11,431: Line 11,431:
return sieve
return sieve
end sieveE</syntaxhighlight>
end sieveE</syntaxhighlight>
Example<syntaxhighlight lang=LiveCode>put sieveE(121)
Example<syntaxhighlight lang="livecode">put sieveE(121)
-- 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113</syntaxhighlight>
-- 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113</syntaxhighlight>






<syntaxhighlight lang=LiveCode># Sieve of Eratosthenes
<syntaxhighlight lang="livecode"># Sieve of Eratosthenes
# calculates prime numbers up to a given number
# calculates prime numbers up to a given number
Line 11,495: Line 11,495:
due to the use of mod (modulo = division) in the filter function.
due to the use of mod (modulo = division) in the filter function.
A coinduction based solution just for fun:
A coinduction based solution just for fun:
<syntaxhighlight lang=logtalk>
<syntaxhighlight lang="logtalk">
:- object(sieve).
:- object(sieve).


Line 11,536: Line 11,536:
</syntaxhighlight>
</syntaxhighlight>
Example query:
Example query:
<syntaxhighlight lang=logtalk>
<syntaxhighlight lang="logtalk">
?- sieve::primes(20, P).
?- sieve::primes(20, P).
P = [2, 3|_S1], % where
P = [2, 3|_S1], % where
Line 11,543: Line 11,543:


=={{header|LOLCODE}}==
=={{header|LOLCODE}}==
<syntaxhighlight lang=lolcode>HAI 1.2
<syntaxhighlight lang="lolcode">HAI 1.2
CAN HAS STDIO?
CAN HAS STDIO?


Line 11,597: Line 11,597:


=={{header|Lua}}==
=={{header|Lua}}==
<syntaxhighlight lang=lua>function erato(n)
<syntaxhighlight lang="lua">function erato(n)
if n < 2 then return {} end
if n < 2 then return {} end
local t = {0} -- clears '1'
local t = {0} -- clears '1'
Line 11,609: Line 11,609:


The following changes the code to '''odds-only''' using the same large array-based algorithm:
The following changes the code to '''odds-only''' using the same large array-based algorithm:
<syntaxhighlight lang=lua>function erato2(n)
<syntaxhighlight lang="lua">function erato2(n)
if n < 2 then return {} end
if n < 2 then return {} end
if n < 3 then return {2} end
if n < 3 then return {2} end
Line 11,626: Line 11,626:
The following code implements '''an odds-only "infinite" generator style using a table as a hash table''', including postponing adding base primes to the table:
The following code implements '''an odds-only "infinite" generator style using a table as a hash table''', including postponing adding base primes to the table:


<syntaxhighlight lang=lua>function newEratoInf()
<syntaxhighlight lang="lua">function newEratoInf()
local _cand = 0; local _lstbp = 3; local _lstsqr = 9
local _cand = 0; local _lstbp = 3; local _lstsqr = 9
local _composites = {}; local _bps = nil
local _composites = {}; local _bps = nil
Line 11,685: Line 11,685:


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang=M2000 Interpreter>
<syntaxhighlight lang="m2000 interpreter">
Module EratosthenesSieve (x) {
Module EratosthenesSieve (x) {
\\ Κόσκινο του Ερατοσθένη
\\ Κόσκινο του Ερατοσθένη
Line 11,709: Line 11,709:


=={{header|M4}}==
=={{header|M4}}==
<syntaxhighlight lang=M4>define(`lim',100)dnl
<syntaxhighlight lang="m4">define(`lim',100)dnl
define(`for',
define(`for',
`ifelse($#,0,
`ifelse($#,0,
Line 11,729: Line 11,729:
=={{header|MAD}}==
=={{header|MAD}}==


<syntaxhighlight lang=MAD> NORMAL MODE IS INTEGER
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
R TO GENERATE MORE PRIMES, CHANGE BOTH THESE NUMBERS
R TO GENERATE MORE PRIMES, CHANGE BOTH THESE NUMBERS
Line 11,777: Line 11,777:


=={{header|Maple}}==
=={{header|Maple}}==
<syntaxhighlight lang=Maple>Eratosthenes := proc(n::posint)
<syntaxhighlight lang="maple">Eratosthenes := proc(n::posint)
local numbers_to_check, i, k;
local numbers_to_check, i, k;
numbers_to_check := [seq(2 .. n)];
numbers_to_check := [seq(2 .. n)];
Line 11,798: Line 11,798:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica>Eratosthenes[n_] := Module[{numbers = Range[n]},
<syntaxhighlight lang="mathematica">Eratosthenes[n_] := Module[{numbers = Range[n]},
Do[If[numbers[[i]] != 0, Do[numbers[[i j]] = 0, {j, 2, n/i}]], {i,
Do[If[numbers[[i]] != 0, Do[numbers[[i j]] = 0, {j, 2, n/i}]], {i,
2, Sqrt[n]}];
2, Sqrt[n]}];
Line 11,806: Line 11,806:
===Slightly Optimized Version===
===Slightly Optimized Version===
The below has been improved to not require so many operations per composite number cull for about two thirds the execution time:
The below has been improved to not require so many operations per composite number cull for about two thirds the execution time:
<syntaxhighlight lang=Mathematica>Eratosthenes[n_] := Module[{numbers = Range[n]},
<syntaxhighlight lang="mathematica">Eratosthenes[n_] := Module[{numbers = Range[n]},
Do[If[numbers[[i]] != 0, Do[numbers[[j]] = 0, {j,i i,n,i}]],{i,2,Sqrt[n]}];
Do[If[numbers[[i]] != 0, Do[numbers[[j]] = 0, {j,i i,n,i}]],{i,2,Sqrt[n]}];
Select[numbers, # > 1 &]]
Select[numbers, # > 1 &]]
Line 11,813: Line 11,813:
===Sieving Odds-Only Version===
===Sieving Odds-Only Version===
The below has been further improved to only sieve odd numbers for a further reduction in execution time by a factor of over two:
The below has been further improved to only sieve odd numbers for a further reduction in execution time by a factor of over two:
<syntaxhighlight lang=Mathematica>Eratosthenes2[n_] := Module[{numbers = Range[3, n, 2], limit = (n - 1)/2},
<syntaxhighlight lang="mathematica">Eratosthenes2[n_] := Module[{numbers = Range[3, n, 2], limit = (n - 1)/2},
Do[c = numbers[[i]]; If[c != 0,
Do[c = numbers[[i]]; If[c != 0,
Do[numbers[[j]] = 0, {j,(c c - 1)/2,limit,c}]], {i,1,(Sqrt[n] - 1)/2}];
Do[numbers[[j]] = 0, {j,(c c - 1)/2,limit,c}]], {i,1,(Sqrt[n] - 1)/2}];
Line 11,822: Line 11,822:
=={{header|MATLAB}} / {{header|Octave}}==
=={{header|MATLAB}} / {{header|Octave}}==
===Somewhat optimized true Sieve of Eratosthenes===
===Somewhat optimized true Sieve of Eratosthenes===
<syntaxhighlight lang=MATLAB>function P = erato(x) % Sieve of Eratosthenes: returns all primes between 2 and x
<syntaxhighlight lang="matlab">function P = erato(x) % Sieve of Eratosthenes: returns all primes between 2 and x
P = [0 2:x]; % Create vector with all ints between 2 and x where
P = [0 2:x]; % Create vector with all ints between 2 and x where
Line 11,841: Line 11,841:
A more efficient Sieve avoids creating a large double precision vector P, instead using a logical array (which consumes 1/8 the memory of a double array of the same size) and only converting to double those values corresponding to primes.
A more efficient Sieve avoids creating a large double precision vector P, instead using a logical array (which consumes 1/8 the memory of a double array of the same size) and only converting to double those values corresponding to primes.


<syntaxhighlight lang=MATLAB>function P = sieveOfEratosthenes(x)
<syntaxhighlight lang="matlab">function P = sieveOfEratosthenes(x)
ISP = [false true(1, x-1)]; % 1 is not prime, but we start off assuming all numbers between 2 and x are
ISP = [false true(1, x-1)]; % 1 is not prime, but we start off assuming all numbers between 2 and x are
for n = 2:sqrt(x)
for n = 2:sqrt(x)
Line 11,856: Line 11,856:


=={{header|Maxima}}==
=={{header|Maxima}}==
<syntaxhighlight lang=maxima>sieve(n):=block(
<syntaxhighlight lang="maxima">sieve(n):=block(
[a:makelist(true,n),i:1,j],
[a:makelist(true,n),i:1,j],
a[1]:false,
a[1]:false,
Line 11,888: Line 11,888:


=={{header|Mercury}}==
=={{header|Mercury}}==
<syntaxhighlight lang=Mercury>:- module sieve.
<syntaxhighlight lang="mercury">:- module sieve.
:- interface.
:- interface.
:- import_module io.
:- import_module io.
Line 11,950: Line 11,950:
=={{header|Microsoft Small Basic}}==
=={{header|Microsoft Small Basic}}==
{{trans|GW-BASIC}}
{{trans|GW-BASIC}}
<syntaxhighlight lang=microsoftsmallbasic>
<syntaxhighlight lang="microsoftsmallbasic">
TextWindow.Write("Enter number to search to: ")
TextWindow.Write("Enter number to search to: ")
limit = TextWindow.ReadNumber()
limit = TextWindow.ReadNumber()
Line 11,976: Line 11,976:


=={{header|Modula-2}}==
=={{header|Modula-2}}==
<syntaxhighlight lang=modula2>MODULE Erato;
<syntaxhighlight lang="modula2">MODULE Erato;
FROM InOut IMPORT WriteCard, WriteLn;
FROM InOut IMPORT WriteCard, WriteLn;
FROM MathLib IMPORT sqrt;
FROM MathLib IMPORT sqrt;
Line 12,042: Line 12,042:
===Regular version===
===Regular version===
This version runs slow because of the way I/O is implemented in the CM3 compiler. Setting <code>ListPrimes = FALSE</code> achieves speed comparable to C on sufficiently high values of <code>LastNum</code> (e.g., 10^6).
This version runs slow because of the way I/O is implemented in the CM3 compiler. Setting <code>ListPrimes = FALSE</code> achieves speed comparable to C on sufficiently high values of <code>LastNum</code> (e.g., 10^6).
<syntaxhighlight lang=modula3>MODULE Eratosthenes EXPORTS Main;
<syntaxhighlight lang="modula3">MODULE Eratosthenes EXPORTS Main;


IMPORT IO;
IMPORT IO;
Line 12,104: Line 12,104:
===Advanced version===
===Advanced version===
This version uses more "advanced" types.
This version uses more "advanced" types.
<syntaxhighlight lang=modula3>(* From the CM3 examples folder (comments removed). *)
<syntaxhighlight lang="modula3">(* From the CM3 examples folder (comments removed). *)


MODULE Sieve EXPORTS Main;
MODULE Sieve EXPORTS Main;
Line 12,132: Line 12,132:


=={{header|MUMPS}}==
=={{header|MUMPS}}==
<syntaxhighlight lang=MUMPS>ERATO1(HI)
<syntaxhighlight lang="mumps">ERATO1(HI)
;performs the Sieve of Erotosethenes up to the number passed in.
;performs the Sieve of Erotosethenes up to the number passed in.
;This version sets an array containing the primes
;This version sets an array containing the primes
Line 12,150: Line 12,150:


=={{header|Neko}}==
=={{header|Neko}}==
<syntaxhighlight lang=ActionScript>/* The Computer Language Shootout
<syntaxhighlight lang="actionscript">/* The Computer Language Shootout
http://shootout.alioth.debian.org/
http://shootout.alioth.debian.org/


Line 12,200: Line 12,200:
=={{header|NetRexx}}==
=={{header|NetRexx}}==
===Version 1 (slow)===
===Version 1 (slow)===
<syntaxhighlight lang=Rexx>/* NetRexx */
<syntaxhighlight lang="rexx">/* NetRexx */


options replace format comments java crossref savelog symbols binary
options replace format comments java crossref savelog symbols binary
Line 12,264: Line 12,264:
</pre>
</pre>
===Version 2 (significantly, i.e. 10 times faster)===
===Version 2 (significantly, i.e. 10 times faster)===
<syntaxhighlight lang=NetRexx>/* NetRexx ************************************************************
<syntaxhighlight lang="netrexx">/* NetRexx ************************************************************
* Essential improvements:Use boolean instead of Rexx for sv
* Essential improvements:Use boolean instead of Rexx for sv
* and remove methods isTrue and isFalse
* and remove methods isTrue and isFalse
Line 12,338: Line 12,338:
Note that the lambda expression in the following script does not involve a closure; newLISP has dynamic scope, so it matters that the same variable names will not be reused for some other purpose (at runtime) before the anonymous function is called.
Note that the lambda expression in the following script does not involve a closure; newLISP has dynamic scope, so it matters that the same variable names will not be reused for some other purpose (at runtime) before the anonymous function is called.


<syntaxhighlight lang=newlisp>(set 'upper-bound 1000)
<syntaxhighlight lang="newlisp">(set 'upper-bound 1000)


; The initial sieve is a list of all the numbers starting at 2.
; The initial sieve is a list of all the numbers starting at 2.
Line 12,538: Line 12,538:


=={{header|Nim}}==
=={{header|Nim}}==
<syntaxhighlight lang=nim>from math import sqrt
<syntaxhighlight lang="nim">from math import sqrt
iterator primesUpto(limit: int): int =
iterator primesUpto(limit: int): int =
Line 12,569: Line 12,569:
The above version wastes quite a lot of memory by using a sequence of boolean values to sieve the composite numbers and sieving all numbers when two is the only even prime. The below code uses a bit-packed sequence to save a factor of eight in memory and also sieves only odd primes for another memory saving by a factor of two; it is also over two and a half times faster due to reduced number of culling operations and better use of the CPU cache as a little cache goes a lot further - this better use of cache is more than enough to make up for the extra bit-packing shifting operations:
The above version wastes quite a lot of memory by using a sequence of boolean values to sieve the composite numbers and sieving all numbers when two is the only even prime. The below code uses a bit-packed sequence to save a factor of eight in memory and also sieves only odd primes for another memory saving by a factor of two; it is also over two and a half times faster due to reduced number of culling operations and better use of the CPU cache as a little cache goes a lot further - this better use of cache is more than enough to make up for the extra bit-packing shifting operations:


<syntaxhighlight lang=nim>iterator isoe_upto(top: uint): uint =
<syntaxhighlight lang="nim">iterator isoe_upto(top: uint): uint =
let topndx = int((top - 3) div 2)
let topndx = int((top - 3) div 2)
let sqrtndx = (int(sqrt float64(top)) - 3) div 2
let sqrtndx = (int(sqrt float64(top)) - 3) div 2
Line 12,588: Line 12,588:


For many purposes, one doesn't know the exact upper limit desired to easily use the above versions; in addition, those versions use an amount of memory proportional to the range sieved. In contrast, unbounded versions continuously update their range as they progress and only use memory proportional to the secondary base primes stream, which is only proportional to the square root of the range. One of the most basic functional versions is the TreeFolding sieve which is based on merging lazy streams as per Richard Bird's contribution to incremental sieves in Haskell, but which has a much better asymptotic execution complexity due to the added tree folding. The following code is a version of that in Nim (odds-only):
For many purposes, one doesn't know the exact upper limit desired to easily use the above versions; in addition, those versions use an amount of memory proportional to the range sieved. In contrast, unbounded versions continuously update their range as they progress and only use memory proportional to the secondary base primes stream, which is only proportional to the square root of the range. One of the most basic functional versions is the TreeFolding sieve which is based on merging lazy streams as per Richard Bird's contribution to incremental sieves in Haskell, but which has a much better asymptotic execution complexity due to the added tree folding. The following code is a version of that in Nim (odds-only):
<syntaxhighlight lang=nim>import sugar
<syntaxhighlight lang="nim">import sugar
from times import epochTime
from times import epochTime


Line 12,682: Line 12,682:


To show the cost of functional forms of code, the following code is written embracing mutability, both by using a mutable hash table to store the state of incremental culling by the secondary stream of base primes and by using mutable values to store the state wherever possible, as per the following code:
To show the cost of functional forms of code, the following code is written embracing mutability, both by using a mutable hash table to store the state of incremental culling by the secondary stream of base primes and by using mutable values to store the state wherever possible, as per the following code:
<syntaxhighlight lang=nim>import tables, times
<syntaxhighlight lang="nim">import tables, times


type PrimeType = int
type PrimeType = int
Line 12,746: Line 12,746:


For the highest speeds, one needs to use page segmented mutable arrays as in the bit-packed version here:
For the highest speeds, one needs to use page segmented mutable arrays as in the bit-packed version here:
<syntaxhighlight lang=nim># a Page Segmented Odd-Only Bit-Packed Sieve of Eratosthenes...
<syntaxhighlight lang="nim"># a Page Segmented Odd-Only Bit-Packed Sieve of Eratosthenes...


from times import epochTime # for testing
from times import epochTime # for testing
Line 12,934: Line 12,934:
=={{header|Niue}}==
=={{header|Niue}}==
{{incorrect|Niue|It uses rem testing and so is a trial division algorithm, not a sieve of Eratosthenes.}}
{{incorrect|Niue|It uses rem testing and so is a trial division algorithm, not a sieve of Eratosthenes.}}
<syntaxhighlight lang=Niue>[ dup 2 < ] '<2 ;
<syntaxhighlight lang="niue">[ dup 2 < ] '<2 ;
[ 1 + 'count ; [ <2 [ , ] when ] count times ] 'fill-stack ;
[ 1 + 'count ; [ <2 [ , ] when ] count times ] 'fill-stack ;


Line 12,957: Line 12,957:


=={{header|Oberon-2}}==
=={{header|Oberon-2}}==
<syntaxhighlight lang=oberon2>MODULE Primes;
<syntaxhighlight lang="oberon2">MODULE Primes;


IMPORT Out, Math;
IMPORT Out, Math;
Line 12,994: Line 12,994:
=={{header|OCaml}}==
=={{header|OCaml}}==
===Imperative===
===Imperative===
<syntaxhighlight lang=ocaml>let sieve n =
<syntaxhighlight lang="ocaml">let sieve n =
let is_prime = Array.create n true in
let is_prime = Array.create n true in
let limit = truncate(sqrt (float (n - 1))) in
let limit = truncate(sqrt (float (n - 1))) in
Line 13,009: Line 13,009:
is_prime</syntaxhighlight>
is_prime</syntaxhighlight>


<syntaxhighlight lang=ocaml>let primes n =
<syntaxhighlight lang="ocaml">let primes n =
let primes, _ =
let primes, _ =
let sieve = sieve n in
let sieve = sieve n in
Line 13,026: Line 13,026:


===Functional===
===Functional===
<syntaxhighlight lang=ocaml>(* first define some iterators *)
<syntaxhighlight lang="ocaml">(* first define some iterators *)
let fold_iter f init a b =
let fold_iter f init a b =
let rec aux acc i =
let rec aux acc i =
Line 13,070: Line 13,070:
</syntaxhighlight>
</syntaxhighlight>
in the top-level:
in the top-level:
<syntaxhighlight lang=ocaml># primes 200 ;;
<syntaxhighlight lang="ocaml"># primes 200 ;;
- : int list =
- : int list =
[2; 3; 5; 7; 11; 13; 17; 19; 23; 29; 31; 37; 41; 43; 47; 53; 59; 61; 67; 71;
[2; 3; 5; 7; 11; 13; 17; 19; 23; 29; 31; 37; 41; 43; 47; 53; 59; 61; 67; 71;
Line 13,080: Line 13,080:
This uses zero to denote struck-out numbers. It is slightly inefficient as it strikes-out multiples above p rather than p<sup>2</sup>
This uses zero to denote struck-out numbers. It is slightly inefficient as it strikes-out multiples above p rather than p<sup>2</sup>


<syntaxhighlight lang=ocaml>let rec strike_nth k n l = match l with
<syntaxhighlight lang="ocaml">let rec strike_nth k n l = match l with
| [] -> []
| [] -> []
| h :: t ->
| h :: t ->
Line 13,100: Line 13,100:
</syntaxhighlight>
</syntaxhighlight>
in the top-level:
in the top-level:
<syntaxhighlight lang=ocaml># primes 200;;
<syntaxhighlight lang="ocaml"># primes 200;;
- : int list =
- : int list =
[2; 3; 5; 7; 11; 13; 17; 19; 23; 29; 31; 37; 41; 43; 47; 53; 59; 61; 67; 71;
[2; 3; 5; 7; 11; 13; 17; 19; 23; 29; 31; 37; 41; 43; 47; 53; 59; 61; 67; 71;
Line 13,108: Line 13,108:
=={{header|Oforth}}==
=={{header|Oforth}}==


<syntaxhighlight lang=Oforth>: eratosthenes(n)
<syntaxhighlight lang="oforth">: eratosthenes(n)
| i j |
| i j |
ListBuffer newSize(n) dup add(null) seqFrom(2, n) over addAll
ListBuffer newSize(n) dup add(null) seqFrom(2, n) over addAll
Line 13,123: Line 13,123:


=={{header|Ol}}==
=={{header|Ol}}==
<syntaxhighlight lang=scheme>
<syntaxhighlight lang="scheme">
(define all (iota 999 2))
(define all (iota 999 2))


Line 13,149: Line 13,149:
=={{header|Oz}}==
=={{header|Oz}}==
{{trans|Haskell}}
{{trans|Haskell}}
<syntaxhighlight lang=oz>declare
<syntaxhighlight lang="oz">declare
fun {Sieve N}
fun {Sieve N}
S = {Array.new 2 N true}
S = {Array.new 2 N true}
Line 13,175: Line 13,175:


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<syntaxhighlight lang=parigp>Eratosthenes(lim)={
<syntaxhighlight lang="parigp">Eratosthenes(lim)={
my(v=Vecsmall(lim\1,unused,1));
my(v=Vecsmall(lim\1,unused,1));
forprime(p=2,sqrt(lim),
forprime(p=2,sqrt(lim),
Line 13,187: Line 13,187:
An alternate version:
An alternate version:


<syntaxhighlight lang=parigp>Sieve(n)=
<syntaxhighlight lang="parigp">Sieve(n)=
{
{
v=vector(n,unused,1);
v=vector(n,unused,1);
Line 13,198: Line 13,198:
=={{header|Pascal}}==
=={{header|Pascal}}==
Note: Some Pascal implementations put quite low limits on the size of a set (e.g. Turbo Pascal doesn't allow more than 256 members). To compile on such an implementation, reduce the constant PrimeLimit accordingly.
Note: Some Pascal implementations put quite low limits on the size of a set (e.g. Turbo Pascal doesn't allow more than 256 members). To compile on such an implementation, reduce the constant PrimeLimit accordingly.
<syntaxhighlight lang=pascal>
<syntaxhighlight lang="pascal">
program primes(output)
program primes(output)


Line 13,243: Line 13,243:
Using growing wheel to fill array for sieving for minimal unmark operations.
Using growing wheel to fill array for sieving for minimal unmark operations.
Sieving only with possible-prime factors.
Sieving only with possible-prime factors.
<syntaxhighlight lang=pascal>
<syntaxhighlight lang="pascal">
program prim(output);
program prim(output);
//Sieve of Erathosthenes with fast elimination of multiples of small primes
//Sieve of Erathosthenes with fast elimination of multiples of small primes
Line 13,389: Line 13,389:


===Classic Sieve===
===Classic Sieve===
<syntaxhighlight lang=perl>sub sieve {
<syntaxhighlight lang="perl">sub sieve {
my $n = shift;
my $n = shift;
my @composite;
my @composite;
Line 13,407: Line 13,407:


===Odds only (faster)===
===Odds only (faster)===
<syntaxhighlight lang=perl>sub sieve2 {
<syntaxhighlight lang="perl">sub sieve2 {
my($n) = @_;
my($n) = @_;
return @{([],[],[2],[2,3],[2,3])[$n]} if $n <= 4;
return @{([],[],[2],[2,3],[2,3])[$n]} if $n <= 4;
Line 13,426: Line 13,426:


===Odds only, using vectors for lower memory use===
===Odds only, using vectors for lower memory use===
<syntaxhighlight lang=perl>sub dj_vector {
<syntaxhighlight lang="perl">sub dj_vector {
my($end) = @_;
my($end) = @_;
return @{([],[],[2],[2,3],[2,3])[$end]} if $end <= 4;
return @{([],[],[2],[2,3],[2,3])[$end]} if $end <= 4;
Line 13,445: Line 13,445:
===Odds only, using strings for best performance===
===Odds only, using strings for best performance===
Compared to array versions, about 2x faster (with 5.16.0 or later) and lower memory. Much faster than the experimental versions below. It's possible a mod-6 or mod-30 wheel could give more improvement, though possibly with obfuscation. The best next step for performance and functionality would be segmenting.
Compared to array versions, about 2x faster (with 5.16.0 or later) and lower memory. Much faster than the experimental versions below. It's possible a mod-6 or mod-30 wheel could give more improvement, though possibly with obfuscation. The best next step for performance and functionality would be segmenting.
<syntaxhighlight lang=perl>sub string_sieve {
<syntaxhighlight lang="perl">sub string_sieve {
my ($n, $i, $s, $d, @primes) = (shift, 7);
my ($n, $i, $s, $d, @primes) = (shift, 7);


Line 13,463: Line 13,463:


This older version uses half the memory, but at the expense of a bit of speed and code complexity:
This older version uses half the memory, but at the expense of a bit of speed and code complexity:
<syntaxhighlight lang=perl>sub dj_string {
<syntaxhighlight lang="perl">sub dj_string {
my($end) = @_;
my($end) = @_;
return @{([],[],[2],[2,3],[2,3])[$end]} if $end <= 4;
return @{([],[],[2],[2,3],[2,3])[$end]} if $end <= 4;
Line 13,490: Line 13,490:


Golfing a bit, at the expense of speed:
Golfing a bit, at the expense of speed:
<syntaxhighlight lang=perl>sub sieve{ my (@s, $i);
<syntaxhighlight lang="perl">sub sieve{ my (@s, $i);
grep { not $s[ $i = $_ ] and do
grep { not $s[ $i = $_ ] and do
{ $s[ $i += $_ ]++ while $i <= $_[0]; 1 }
{ $s[ $i += $_ ]++ while $i <= $_[0]; 1 }
Line 13,499: Line 13,499:


Or with bit strings (much slower than the vector version above):
Or with bit strings (much slower than the vector version above):
<syntaxhighlight lang=perl>sub sieve{ my ($s, $i);
<syntaxhighlight lang="perl">sub sieve{ my ($s, $i);
grep { not vec $s, $i = $_, 1 and do
grep { not vec $s, $i = $_, 1 and do
{ (vec $s, $i += $_, 1) = 1 while $i <= $_[0]; 1 }
{ (vec $s, $i += $_, 1) = 1 while $i <= $_[0]; 1 }
Line 13,508: Line 13,508:


A short recursive version:
A short recursive version:
<syntaxhighlight lang=perl>sub erat {
<syntaxhighlight lang="perl">sub erat {
my $p = shift;
my $p = shift;
return $p, $p**2 > $_[$#_] ? @_ : erat(grep $_%$p, @_)
return $p, $p**2 > $_[$#_] ? @_ : erat(grep $_%$p, @_)
Line 13,515: Line 13,515:
print join ', ' => erat 2..100000;</syntaxhighlight>
print join ', ' => erat 2..100000;</syntaxhighlight>


Regexp (purely an example -- the regex engine limits it to only 32769):<syntaxhighlight lang=perl>sub sieve {
Regexp (purely an example -- the regex engine limits it to only 32769):<syntaxhighlight lang="perl">sub sieve {
my ($s, $p) = "." . ("x" x shift);
my ($s, $p) = "." . ("x" x shift);


Line 13,531: Line 13,531:


Here are two incremental versions, which allows one to create a tied array of primes:
Here are two incremental versions, which allows one to create a tied array of primes:
<syntaxhighlight lang=perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
package Tie::SieveOfEratosthenes;
package Tie::SieveOfEratosthenes;
Line 13,638: Line 13,638:
1;</syntaxhighlight>
1;</syntaxhighlight>
This one is based on the vector sieve shown earlier, but adds to a list as needed, just sieving in the segment. Slightly faster and half the memory vs. the previous incremental sieve. It uses the same API -- arguably we should be offset by one so $primes[$n] returns the $n'th prime.
This one is based on the vector sieve shown earlier, but adds to a list as needed, just sieving in the segment. Slightly faster and half the memory vs. the previous incremental sieve. It uses the same API -- arguably we should be offset by one so $primes[$n] returns the $n'th prime.
<syntaxhighlight lang=perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
package Tie::SieveOfEratosthenes;
package Tie::SieveOfEratosthenes;
Line 13,695: Line 13,695:
=={{header|Phix}}==
=={{header|Phix}}==
{{Trans|Euphoria}}
{{Trans|Euphoria}}
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1000</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1000</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">primes</span> <span style="color: #0000FF;">=</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: #0000FF;">{}</span>
Line 13,729: Line 13,729:


=={{header|Phixmonti}}==
=={{header|Phixmonti}}==
<syntaxhighlight lang=Phixmonti>include ..\Utilitys.pmt
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt


def sequence /# ( ini end [step] ) #/
def sequence /# ( ini end [step] ) #/
Line 13,749: Line 13,749:


Another solution
Another solution
<syntaxhighlight lang=Phixmonti>include ..\Utilitys.pmt
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt
1000
1000
Line 13,772: Line 13,772:


=={{header|PHP}}==
=={{header|PHP}}==
<syntaxhighlight lang=php>
<syntaxhighlight lang="php">
function iprimes_upto($limit)
function iprimes_upto($limit)
{
{
Line 13,813: Line 13,813:
=={{header|Picat}}==
=={{header|Picat}}==
The SoE is provided in the standard library, defined as follows:
The SoE is provided in the standard library, defined as follows:
<syntaxhighlight lang=Picat>
<syntaxhighlight lang="picat">
primes(N) = L =>
primes(N) = L =>
A = new_array(N),
A = new_array(N),
Line 13,832: Line 13,832:
</pre>
</pre>
=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp>(de sieve (N)
<syntaxhighlight lang="picolisp">(de sieve (N)
(let Sieve (range 1 N)
(let Sieve (range 1 N)
(set Sieve)
(set Sieve)
Line 13,845: Line 13,845:
===Alternate Version Using a 2x3x5x7 Wheel===
===Alternate Version Using a 2x3x5x7 Wheel===
This works by destructively modifying the CDR of the previous cell when it finds a composite number. For sieving large sets (e.g. 1,000,000) it's much faster than the above.
This works by destructively modifying the CDR of the previous cell when it finds a composite number. For sieving large sets (e.g. 1,000,000) it's much faster than the above.
<syntaxhighlight lang=PicoLisp>
<syntaxhighlight lang="picolisp">
(setq WHEEL-2357
(setq WHEEL-2357
(2 4 2 4 6 2 6 4
(2 4 2 4 6 2 6 4
Line 13,890: Line 13,890:


=={{header|PL/I}}==
=={{header|PL/I}}==
<syntaxhighlight lang=pli>eratos: proc options (main) reorder;
<syntaxhighlight lang="pli">eratos: proc options (main) reorder;


dcl i fixed bin (31);
dcl i fixed bin (31);
Line 13,929: Line 13,929:


=={{header|PL/M}}==
=={{header|PL/M}}==
<syntaxhighlight lang=plm>100H:
<syntaxhighlight lang="plm">100H:


DECLARE PRIME$MAX LITERALLY '5000';
DECLARE PRIME$MAX LITERALLY '5000';
Line 14,004: Line 14,004:


=={{header|PL/SQL}}==
=={{header|PL/SQL}}==
<syntaxhighlight lang=plsql>create or replace package sieve_of_eratosthenes as
<syntaxhighlight lang="plsql">create or replace package sieve_of_eratosthenes as
type array_of_booleans is varray(100000000) of boolean;
type array_of_booleans is varray(100000000) of boolean;
type table_of_integers is table of integer;
type table_of_integers is table of integer;
Line 14,044: Line 14,044:
Usage:
Usage:


<syntaxhighlight lang=sql>select column_value as prime_number
<syntaxhighlight lang="sql">select column_value as prime_number
from table(sieve_of_eratosthenes.find_primes(30));
from table(sieve_of_eratosthenes.find_primes(30));


Line 14,074: Line 14,074:


=={{header|Pony}}==
=={{header|Pony}}==
<syntaxhighlight lang=pony>use "time" // for testing
<syntaxhighlight lang="pony">use "time" // for testing
use "collections"
use "collections"


Line 14,149: Line 14,149:
It is a waste not to do the trivial changes to the above code to sieve odds-only, which is about two and a half times faster due to the decreased number of culling operations; it doesn't really do much about the huge array problem though, other than to reduce it by a factor of two.
It is a waste not to do the trivial changes to the above code to sieve odds-only, which is about two and a half times faster due to the decreased number of culling operations; it doesn't really do much about the huge array problem though, other than to reduce it by a factor of two.


<syntaxhighlight lang=pony>use "time" // for testing
<syntaxhighlight lang="pony">use "time" // for testing
use "collections"
use "collections"


Line 14,230: Line 14,230:
===Basic procedure===
===Basic procedure===
It outputs immediately so that the number can be used by the pipeline.
It outputs immediately so that the number can be used by the pipeline.
<syntaxhighlight lang=PowerShell>function Sieve ( [int] $num )
<syntaxhighlight lang="powershell">function Sieve ( [int] $num )
{
{
$isprime = @{}
$isprime = @{}
Line 14,243: Line 14,243:
}</syntaxhighlight>
}</syntaxhighlight>
===Another implementation===
===Another implementation===
<syntaxhighlight lang=PowerShell>
<syntaxhighlight lang="powershell">
function eratosthenes ($n) {
function eratosthenes ($n) {
if($n -ge 1){
if($n -ge 1){
Line 14,270: Line 14,270:
=={{header|Processing}}==
=={{header|Processing}}==
Calculate the primes up to 1000000 with Processing, including a visualisation of the process.
Calculate the primes up to 1000000 with Processing, including a visualisation of the process.
<syntaxhighlight lang=java>int i=2;
<syntaxhighlight lang="java">int i=2;
int maxx;
int maxx;
int maxy;
int maxy;
Line 14,324: Line 14,324:
==={{header|Processing Python mode}}===
==={{header|Processing Python mode}}===


<syntaxhighlight lang=python>from __future__ import print_function
<syntaxhighlight lang="python">from __future__ import print_function


i = 2
i = 2
Line 14,371: Line 14,371:
===Using lists===
===Using lists===
====Basic bounded sieve====
====Basic bounded sieve====
<syntaxhighlight lang=Prolog>primes(N, L) :- numlist(2, N, Xs),
<syntaxhighlight lang="prolog">primes(N, L) :- numlist(2, N, Xs),
sieve(Xs, L).
sieve(Xs, L).


Line 14,399: Line 14,399:
This is actually the Euler's variant of the sieve of Eratosthenes, generating (and thus removing) each multiple only once, though a sub-optimal implementation.
This is actually the Euler's variant of the sieve of Eratosthenes, generating (and thus removing) each multiple only once, though a sub-optimal implementation.


<syntaxhighlight lang=Prolog>primes(X, PS) :- X > 1, range(2, X, R), sieve(R, PS).
<syntaxhighlight lang="prolog">primes(X, PS) :- X > 1, range(2, X, R), sieve(R, PS).


range(X, X, [X]) :- !.
range(X, X, [X]) :- !.
Line 14,426: Line 14,426:
We can stop early, with massive improvement in complexity (below ~ <i>n<sup>1.5</sup></i> inferences, empirically, vs. the ~ <i>n<sup>2</sup></i> of the above, in ''n'' primes produced; showing only the modified predicates):
We can stop early, with massive improvement in complexity (below ~ <i>n<sup>1.5</sup></i> inferences, empirically, vs. the ~ <i>n<sup>2</sup></i> of the above, in ''n'' primes produced; showing only the modified predicates):


<syntaxhighlight lang=Prolog>primes(X, PS) :- X > 1, range(2, X, R), sieve(X, R, PS).
<syntaxhighlight lang="prolog">primes(X, PS) :- X > 1, range(2, X, R), sieve(X, R, PS).


sieve(X, [H | T], [H | T]) :- H*H > X, !.
sieve(X, [H | T], [H | T]) :- H*H > X, !.
Line 14,442: Line 14,442:
Optimized by stopping early, traditional sieve of Eratosthenes generating multiples by iterated addition.
Optimized by stopping early, traditional sieve of Eratosthenes generating multiples by iterated addition.


<syntaxhighlight lang=Prolog>primes(X, PS) :- X > 1, range(2, X, R), sieve(X, R, PS).
<syntaxhighlight lang="prolog">primes(X, PS) :- X > 1, range(2, X, R), sieve(X, R, PS).


range(X, X, [X]) :- !.
range(X, X, [X]) :- !.
Line 14,468: Line 14,468:
====Sift the Two's and Sift the Three's====
====Sift the Two's and Sift the Three's====
Another version, based on Cloksin&Mellish p.175, modified to stop early as well as to work with odds only and use addition in the removing predicate, instead of the <code>mod</code> testing as the original was doing:
Another version, based on Cloksin&Mellish p.175, modified to stop early as well as to work with odds only and use addition in the removing predicate, instead of the <code>mod</code> testing as the original was doing:
<syntaxhighlight lang=Prolog>primes(N,[]):- N < 2, !.
<syntaxhighlight lang="prolog">primes(N,[]):- N < 2, !.
primes(N,[2|R]):- ints(3,N,L), sift(N,L,R).
primes(N,[2|R]):- ints(3,N,L), sift(N,L,R).
ints(A,B,[A|C]):- A=<B -> D is A+2, ints(D,B,C).
ints(A,B,[A|C]):- A=<B -> D is A+2, ints(D,B,C).
Line 14,490: Line 14,490:
====Basic variant====
====Basic variant====


<syntaxhighlight lang=prolog>primes(PS):- count(2, 1, NS), sieve(NS, PS).
<syntaxhighlight lang="prolog">primes(PS):- count(2, 1, NS), sieve(NS, PS).


count(N, D, [N|T]):- freeze(T, (N2 is N+D, count(N2, D, T))).
count(N, D, [N|T]):- freeze(T, (N2 is N+D, count(N2, D, T))).
Line 14,512: Line 14,512:
====Optimized by postponed removal====
====Optimized by postponed removal====
Showing only changed predicates.
Showing only changed predicates.
<syntaxhighlight lang=prolog>primes([2|PS]):-
<syntaxhighlight lang="prolog">primes([2|PS]):-
freeze(PS, (primes(BPS), count(3, 1, NS), sieve(NS, BPS, 4, PS))).
freeze(PS, (primes(BPS), count(3, 1, NS), sieve(NS, BPS, 4, PS))).


Line 14,539: Line 14,539:
to record integers that are found to be composite.
to record integers that are found to be composite.


<syntaxhighlight lang=Prolog>% %sieve( +N, -Primes ) is true if Primes is the list of consecutive primes
<syntaxhighlight lang="prolog">% %sieve( +N, -Primes ) is true if Primes is the list of consecutive primes
% that are less than or equal to N
% that are less than or equal to N
sieve( N, [2|Rest]) :-
sieve( N, [2|Rest]) :-
Line 14,577: Line 14,577:
The above has been tested with SWI-Prolog and gprolog.
The above has been tested with SWI-Prolog and gprolog.


<syntaxhighlight lang=Prolog>% SWI-Prolog:
<syntaxhighlight lang="prolog">% SWI-Prolog:


?- time( (sieve(100000,P), length(P,N), writeln(N), last(P, LP), writeln(LP) )).
?- time( (sieve(100000,P), length(P,N), writeln(N), last(P, LP), writeln(LP) )).
Line 14,589: Line 14,589:
[http://ideone.com/WDC7z Works with SWI-Prolog].
[http://ideone.com/WDC7z Works with SWI-Prolog].


<syntaxhighlight lang=Prolog>sieve(N, [2|PS]) :- % PS is list of odd primes up to N
<syntaxhighlight lang="prolog">sieve(N, [2|PS]) :- % PS is list of odd primes up to N
retractall(mult(_)),
retractall(mult(_)),
sieve_O(3,N,PS).
sieve_O(3,N,PS).
Line 14,622: Line 14,622:
Running it produces
Running it produces


<syntaxhighlight lang=Prolog>%% stdout copy
<syntaxhighlight lang="prolog">%% stdout copy
[9592, 99991]
[9592, 99991]
[78498, 999983]
[78498, 999983]
Line 14,636: Line 14,636:
Uses a ariority queue, from the paper "The Genuine Sieve of Eratosthenes" by Melissa O'Neill. Works with YAP (Yet Another Prolog)
Uses a ariority queue, from the paper "The Genuine Sieve of Eratosthenes" by Melissa O'Neill. Works with YAP (Yet Another Prolog)


<syntaxhighlight lang=Prolog>?- use_module(library(heaps)).
<syntaxhighlight lang="prolog">?- use_module(library(heaps)).


prime(2).
prime(2).
Line 14,666: Line 14,666:


===Basic procedure===
===Basic procedure===
<syntaxhighlight lang=PureBasic>For n=2 To Sqr(lim)
<syntaxhighlight lang="purebasic">For n=2 To Sqr(lim)
If Nums(n)=0
If Nums(n)=0
m=n*n
m=n*n
Line 14,677: Line 14,677:


===Working example===
===Working example===
<syntaxhighlight lang=PureBasic>Dim Nums.i(0)
<syntaxhighlight lang="purebasic">Dim Nums.i(0)
Define l, n, m, lim
Define l, n, m, lim


Line 14,738: Line 14,738:
avoids explicit iteration in the interpreter, giving a further speed improvement.
avoids explicit iteration in the interpreter, giving a further speed improvement.


<syntaxhighlight lang=python>def eratosthenes2(n):
<syntaxhighlight lang="python">def eratosthenes2(n):
multiples = set()
multiples = set()
for i in range(2, n+1):
for i in range(2, n+1):
Line 14,749: Line 14,749:
===Using array lookup===
===Using array lookup===
The version below uses array lookup to test for primality. The function <tt>primes_upto()</tt> is a straightforward implementation of [http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Algorithm Sieve of Eratosthenes]algorithm. It returns prime numbers less than or equal to <tt>limit</tt>.
The version below uses array lookup to test for primality. The function <tt>primes_upto()</tt> is a straightforward implementation of [http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Algorithm Sieve of Eratosthenes]algorithm. It returns prime numbers less than or equal to <tt>limit</tt>.
<syntaxhighlight lang=python>def primes_upto(limit):
<syntaxhighlight lang="python">def primes_upto(limit):
is_prime = [False] * 2 + [True] * (limit - 1)
is_prime = [False] * 2 + [True] * (limit - 1)
for n in range(int(limit**0.5 + 1.5)): # stop at ``sqrt(limit)``
for n in range(int(limit**0.5 + 1.5)): # stop at ``sqrt(limit)``
Line 14,759: Line 14,759:
===Using generator===
===Using generator===
The following code may be slightly slower than using the array/list as above, but uses no memory for output:
The following code may be slightly slower than using the array/list as above, but uses no memory for output:
<syntaxhighlight lang=python>def iprimes_upto(limit):
<syntaxhighlight lang="python">def iprimes_upto(limit):
is_prime = [False] * 2 + [True] * (limit - 1)
is_prime = [False] * 2 + [True] * (limit - 1)
for n in xrange(int(limit**0.5 + 1.5)): # stop at ``sqrt(limit)``
for n in xrange(int(limit**0.5 + 1.5)): # stop at ``sqrt(limit)``
Line 14,766: Line 14,766:
is_prime[i] = False
is_prime[i] = False
for i in xrange(limit + 1):
for i in xrange(limit + 1):
if is_prime[i]: yield i</syntaxhighlight>{{out|Example}}<syntaxhighlight lang=python>>>> list(iprimes_upto(15))
if is_prime[i]: yield i</syntaxhighlight>{{out|Example}}<syntaxhighlight lang="python">>>> list(iprimes_upto(15))
[2, 3, 5, 7, 11, 13]</syntaxhighlight>
[2, 3, 5, 7, 11, 13]</syntaxhighlight>


===Odds-only version of the array sieve above===
===Odds-only version of the array sieve above===
The following code is faster than the above array version using only odd composite operations (for a factor of over two) and because it has been optimized to use slice operations for composite number culling to avoid extra work by the interpreter:
The following code is faster than the above array version using only odd composite operations (for a factor of over two) and because it has been optimized to use slice operations for composite number culling to avoid extra work by the interpreter:
<syntaxhighlight lang=python>def primes2(limit):
<syntaxhighlight lang="python">def primes2(limit):
if limit < 2: return []
if limit < 2: return []
if limit < 3: return [2]
if limit < 3: return [2]
Line 14,788: Line 14,788:
The following code is faster than the above generator version using only odd composite operations (for a factor of over two) and because it has been optimized to use slice operations for composite number culling to avoid extra work by the interpreter:
The following code is faster than the above generator version using only odd composite operations (for a factor of over two) and because it has been optimized to use slice operations for composite number culling to avoid extra work by the interpreter:


<syntaxhighlight lang=python>def iprimes2(limit):
<syntaxhighlight lang="python">def iprimes2(limit):
yield 2
yield 2
if limit < 3: return
if limit < 3: return
Line 14,808: Line 14,808:
This uses a 235 factorial wheel for further reductions in operations; the same techniques can be applied to the array version as well; it runs slightly faster and uses slightly less memory as compared to the odds-only algorithms:
This uses a 235 factorial wheel for further reductions in operations; the same techniques can be applied to the array version as well; it runs slightly faster and uses slightly less memory as compared to the odds-only algorithms:


<syntaxhighlight lang=python>def primes235(limit):
<syntaxhighlight lang="python">def primes235(limit):
yield 2; yield 3; yield 5
yield 2; yield 3; yield 5
if limit < 7: return
if limit < 7: return
Line 14,838: Line 14,838:
{{libheader|NumPy}}
{{libheader|NumPy}}
Below code adapted from [http://en.literateprograms.org/Sieve_of_Eratosthenes_(Python,_arrays)#simple_implementation literateprograms.org] using [http://numpy.scipy.org/ numpy]
Below code adapted from [http://en.literateprograms.org/Sieve_of_Eratosthenes_(Python,_arrays)#simple_implementation literateprograms.org] using [http://numpy.scipy.org/ numpy]
<syntaxhighlight lang=python>import numpy
<syntaxhighlight lang="python">import numpy
def primes_upto2(limit):
def primes_upto2(limit):
is_prime = numpy.ones(limit + 1, dtype=numpy.bool)
is_prime = numpy.ones(limit + 1, dtype=numpy.bool)
Line 14,851: Line 14,851:
===Using wheels with numpy===
===Using wheels with numpy===
Version with wheel based optimization:
Version with wheel based optimization:
<syntaxhighlight lang=python>from numpy import array, bool_, multiply, nonzero, ones, put, resize
<syntaxhighlight lang="python">from numpy import array, bool_, multiply, nonzero, ones, put, resize
#
#
def makepattern(smallprimes):
def makepattern(smallprimes):
Line 14,873: Line 14,873:


Examples:
Examples:
<syntaxhighlight lang=python>>>> primes_upto3(10**6, smallprimes=(2,3)) # Wall time: 0.17
<syntaxhighlight lang="python">>>> primes_upto3(10**6, smallprimes=(2,3)) # Wall time: 0.17
array([ 2, 3, 5, ..., 999961, 999979, 999983])
array([ 2, 3, 5, ..., 999961, 999979, 999983])
>>> primes_upto3(10**7, smallprimes=(2,3)) # Wall time: '''2.13'''
>>> primes_upto3(10**7, smallprimes=(2,3)) # Wall time: '''2.13'''
Line 14,891: Line 14,891:


{{works with|Python|2.6+, 3.x}}
{{works with|Python|2.6+, 3.x}}
<syntaxhighlight lang=python>import heapq
<syntaxhighlight lang="python">import heapq


# generates all prime numbers
# generates all prime numbers
Line 14,939: Line 14,939:
The adding of each discovered prime's incremental step info to the mapping should be '''''postponed''''' until the prime's ''square'' is seen amongst the candidate numbers, as it is useless before that point. This drastically reduces the space complexity from <i>O(n)</i> to <i>O(sqrt(n/log(n)))</i>, in ''<code>n</code>'' primes produced, and also lowers the run time complexity quite low ([http://ideone.com/VXep9F this test entry in Python 2.7] and [http://ideone.com/muuS4H this test entry in Python 3.x] shows about <i>~ n<sup>1.08</sup></i> [http://en.wikipedia.org/wiki/Analysis_of_algorithms#Empirical_orders_of_growth empirical order of growth] which is very close to the theoretical value of <i>O(n log(n) log(log(n)))</i>, in ''<code>n</code>'' primes produced):
The adding of each discovered prime's incremental step info to the mapping should be '''''postponed''''' until the prime's ''square'' is seen amongst the candidate numbers, as it is useless before that point. This drastically reduces the space complexity from <i>O(n)</i> to <i>O(sqrt(n/log(n)))</i>, in ''<code>n</code>'' primes produced, and also lowers the run time complexity quite low ([http://ideone.com/VXep9F this test entry in Python 2.7] and [http://ideone.com/muuS4H this test entry in Python 3.x] shows about <i>~ n<sup>1.08</sup></i> [http://en.wikipedia.org/wiki/Analysis_of_algorithms#Empirical_orders_of_growth empirical order of growth] which is very close to the theoretical value of <i>O(n log(n) log(log(n)))</i>, in ''<code>n</code>'' primes produced):
{{works with|Python|2.6+, 3.x}}
{{works with|Python|2.6+, 3.x}}
<syntaxhighlight lang=python>def primes():
<syntaxhighlight lang="python">def primes():
yield 2; yield 3; yield 5; yield 7;
yield 2; yield 3; yield 5; yield 7;
bps = (p for p in primes()) # separate supply of "base" primes (b.p.)
bps = (p for p in primes()) # separate supply of "base" primes (b.p.)
Line 14,967: Line 14,967:
Although theoretically over three times faster than odds-only, the following code using a 2/3/5/7 wheel is only about 1.5 times faster than the above odds-only code due to the extra overheads in code complexity. The [http://ideone.com/LFaRnT test link for Python 2.7] and [http://ideone.com/ZAY0T2 test link for Python 3.x] show about the same empirical order of growth as the odds-only implementation above once the range grows enough so the dict operations become amortized to a constant factor.
Although theoretically over three times faster than odds-only, the following code using a 2/3/5/7 wheel is only about 1.5 times faster than the above odds-only code due to the extra overheads in code complexity. The [http://ideone.com/LFaRnT test link for Python 2.7] and [http://ideone.com/ZAY0T2 test link for Python 3.x] show about the same empirical order of growth as the odds-only implementation above once the range grows enough so the dict operations become amortized to a constant factor.
{{works with|Python|2.6+, 3.x}}
{{works with|Python|2.6+, 3.x}}
<syntaxhighlight lang=python>def primes():
<syntaxhighlight lang="python">def primes():
for p in [2,3,5,7]: yield p # base wheel primes
for p in [2,3,5,7]: yield p # base wheel primes
gaps1 = [ 2,4,2,4,6,2,6,4,2,4,6,6,2,6,4,2,6,4,6,8,4,2,4,2,4,8 ]
gaps1 = [ 2,4,2,4,6,2,6,4,2,4,6,6,2,6,4,2,6,4,6,8,4,2,4,2,4,8 ]
Line 14,999: Line 14,999:


Further gains of about 1.5 times in speed can be made using the same code by only changing the tables and a few constants for a further constant factor gain of about 1.5 times in speed by using a 2/3/5/7/11/13/17 wheel (with the gaps list 92160 elements long) computed for a slight constant overhead time as per the [http://ideone.com/4Ld26g test link for Python 2.7] and [http://ideone.com/72Dmyt test link for Python 3.x]. Further wheel factorization will not really be worth it as the gains will be small (if any and not losses) and the gaps table huge - it is already too big for efficient use by 32-bit Python 3 and the wheel should likely be stopped at 13:
Further gains of about 1.5 times in speed can be made using the same code by only changing the tables and a few constants for a further constant factor gain of about 1.5 times in speed by using a 2/3/5/7/11/13/17 wheel (with the gaps list 92160 elements long) computed for a slight constant overhead time as per the [http://ideone.com/4Ld26g test link for Python 2.7] and [http://ideone.com/72Dmyt test link for Python 3.x]. Further wheel factorization will not really be worth it as the gains will be small (if any and not losses) and the gaps table huge - it is already too big for efficient use by 32-bit Python 3 and the wheel should likely be stopped at 13:
<syntaxhighlight lang=python>def primes():
<syntaxhighlight lang="python">def primes():
whlPrms = [2,3,5,7,11,13,17] # base wheel primes
whlPrms = [2,3,5,7,11,13,17] # base wheel primes
for p in whlPrms: yield p
for p in whlPrms: yield p
Line 15,046: Line 15,046:


=={{header|Quackery}}==
=={{header|Quackery}}==
<syntaxhighlight lang=Quackery> [ dup 1
<syntaxhighlight lang="quackery"> [ dup 1
[ 2dup > while
[ 2dup > while
+ 1 >>
+ 1 >>
Line 15,085: Line 15,085:


=={{header|R}}==
=={{header|R}}==
<syntaxhighlight lang=rsplus>sieve <- function(n) {
<syntaxhighlight lang="rsplus">sieve <- function(n) {
if (n < 2) integer(0)
if (n < 2) integer(0)
else {
else {
Line 15,115: Line 15,115:
'''Alternate Odds-Only Version'''
'''Alternate Odds-Only Version'''


<syntaxhighlight lang=rsplus>sieve <- function(n) {
<syntaxhighlight lang="rsplus">sieve <- function(n) {
if (n < 2) return(integer(0))
if (n < 2) return(integer(0))
lmt <- (sqrt(n) - 1) / 2
lmt <- (sqrt(n) - 1) / 2
Line 15,141: Line 15,141:
===Imperative versions===
===Imperative versions===
Ugly imperative version:
Ugly imperative version:
<syntaxhighlight lang=Racket>#lang racket
<syntaxhighlight lang="racket">#lang racket


(define (sieve n)
(define (sieve n)
Line 15,156: Line 15,156:


A little nicer, but still imperative:
A little nicer, but still imperative:
<syntaxhighlight lang=Racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(define (sieve n)
(define (sieve n)
(define primes (make-vector (add1 n) #t))
(define primes (make-vector (add1 n) #t))
Line 15,169: Line 15,169:


Imperative version using a bit vector:
Imperative version using a bit vector:
<syntaxhighlight lang=Racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(require data/bit-vector)
(require data/bit-vector)
;; Returns a list of prime numbers up to natural number limit
;; Returns a list of prime numbers up to natural number limit
Line 15,191: Line 15,191:
These examples use infinite lists (streams) to implement the sieve of Eratosthenes in a functional way, and producing all prime numbers. The following functions are used as a prefix for pieces of code that follow:
These examples use infinite lists (streams) to implement the sieve of Eratosthenes in a functional way, and producing all prime numbers. The following functions are used as a prefix for pieces of code that follow:


<syntaxhighlight lang=Racket>#lang lazy
<syntaxhighlight lang="racket">#lang lazy
(define (ints-from i d) (cons i (ints-from (+ i d) d)))
(define (ints-from i d) (cons i (ints-from (+ i d) d)))
(define (after n l f)
(define (after n l f)
Line 15,208: Line 15,208:
==== Basic sieve ====
==== Basic sieve ====


<syntaxhighlight lang=Racket>(define (sieve l)
<syntaxhighlight lang="racket">(define (sieve l)
(define x (car l))
(define x (car l))
(cons x (sieve (diff (cdr l) (ints-from (+ x x) x)))))
(cons x (sieve (diff (cdr l) (ints-from (+ x x) x)))))
Line 15,219: Line 15,219:
Note that the first number, 2, and its multiples stream <code>(ints-from 4 2)</code> are handled separately to ensure that the non-primes list is never empty, which simplifies the code for <code>union</code> which assumes non-empty infinite lists.
Note that the first number, 2, and its multiples stream <code>(ints-from 4 2)</code> are handled separately to ensure that the non-primes list is never empty, which simplifies the code for <code>union</code> which assumes non-empty infinite lists.


<syntaxhighlight lang=Racket>(define (sieve l non-primes)
<syntaxhighlight lang="racket">(define (sieve l non-primes)
(let ([x (car l)] [np (car non-primes)])
(let ([x (car l)] [np (car non-primes)])
(cond [(= x np) (sieve (cdr l) (cdr non-primes))] ; else x < np
(cond [(= x np) (sieve (cdr l) (cdr non-primes))] ; else x < np
Line 15,228: Line 15,228:
==== Basic sieve Optimized with postponed processing ====
==== Basic sieve Optimized with postponed processing ====
Since a prime's multiples that count start from its square, we should only start removing them when we reach that square.
Since a prime's multiples that count start from its square, we should only start removing them when we reach that square.
<syntaxhighlight lang=Racket>(define (sieve l prs)
<syntaxhighlight lang="racket">(define (sieve l prs)
(define p (car prs))
(define p (car prs))
(define q (* p p))
(define q (* p p))
Line 15,239: Line 15,239:
Since prime's multiples that matter start from its square, we should only add them when we reach that square.
Since prime's multiples that matter start from its square, we should only add them when we reach that square.


<syntaxhighlight lang=Racket>(define (composites l q primes)
<syntaxhighlight lang="racket">(define (composites l q primes)
(after q l
(after q l
(λ(t)
(λ(t)
Line 15,253: Line 15,253:
Appears in [http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf M.O'Neill's paper]. Achieves on its own the proper postponement that is specifically arranged for in the version above (with <code>after</code>), and is yet more efficient, because it folds to the right and so builds the right-leaning structure of merges at run time, where the more frequently-producing streams of multiples appear <i>higher</i> in that structure, so the composite numbers produced by them have less <code>merge</code> nodes to percolate through:
Appears in [http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf M.O'Neill's paper]. Achieves on its own the proper postponement that is specifically arranged for in the version above (with <code>after</code>), and is yet more efficient, because it folds to the right and so builds the right-leaning structure of merges at run time, where the more frequently-producing streams of multiples appear <i>higher</i> in that structure, so the composite numbers produced by them have less <code>merge</code> nodes to percolate through:


<syntaxhighlight lang=Racket>(define primes
<syntaxhighlight lang="racket">(define primes
(cons 2 (diff (ints-from 3 1)
(cons 2 (diff (ints-from 3 1)
(foldr (λ(p r) (define q (* p p))
(foldr (λ(p r) (define q (* p p))
Line 15,263: Line 15,263:
Same algorithm as [[#With merged composites|"merged composites" above]] (without the postponement optimization), but now using threads and channels to produce a channel of all prime numbers (similar to newsqueak). The macro at the top is a convenient wrapper around definitions of channels using a thread that feeds them.
Same algorithm as [[#With merged composites|"merged composites" above]] (without the postponement optimization), but now using threads and channels to produce a channel of all prime numbers (similar to newsqueak). The macro at the top is a convenient wrapper around definitions of channels using a thread that feeds them.


<syntaxhighlight lang=Racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(define-syntax (define-thread-loop stx)
(define-syntax (define-thread-loop stx)
(syntax-case stx ()
(syntax-case stx ()
Line 15,295: Line 15,295:
Yet another variation of the same algorithm as above, this time using generators.
Yet another variation of the same algorithm as above, this time using generators.


<syntaxhighlight lang=Racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(require racket/generator)
(require racket/generator)
(define (ints-from i d)
(define (ints-from i d)
Line 15,320: Line 15,320:
(formerly Perl 6)
(formerly Perl 6)


<syntaxhighlight lang=perl6>sub sieve( Int $limit ) {
<syntaxhighlight lang="raku" line>sub sieve( Int $limit ) {
my @is-prime = False, False, slip True xx $limit - 1;
my @is-prime = False, False, slip True xx $limit - 1;


Line 15,338: Line 15,338:


More or less the same as the first Python example:
More or less the same as the first Python example:
<syntaxhighlight lang=perl6>sub eratsieve($n) {
<syntaxhighlight lang="raku" line>sub eratsieve($n) {
# Requires n(1 - 1/(log(n-1))) storage
# Requires n(1 - 1/(log(n-1))) storage
my $multiples = set();
my $multiples = set();
Line 15,360: Line 15,360:
''Note: while this is "incorrect" by a strict interpretation of the rules, it is being left as an interesting example''
''Note: while this is "incorrect" by a strict interpretation of the rules, it is being left as an interesting example''


<syntaxhighlight lang=perl6>sub primes ( UInt $n ) {
<syntaxhighlight lang="raku" line>sub primes ( UInt $n ) {
gather {
gather {
# create an iterator from 2 to $n (inclusive)
# create an iterator from 2 to $n (inclusive)
Line 15,386: Line 15,386:


=={{header|RATFOR}}==
=={{header|RATFOR}}==
<syntaxhighlight lang=RATFOR>
<syntaxhighlight lang="ratfor">


program prime
program prime
Line 15,438: Line 15,438:


=={{header|Red}}==
=={{header|Red}}==
<syntaxhighlight lang=Red>
<syntaxhighlight lang="red">
primes: function [n [integer!]][
primes: function [n [integer!]][
poke prim: make bitset! n 1 true
poke prim: make bitset! n 1 true
Line 15,458: Line 15,458:
As the stemmed array gets heavily populated, the number of entries ''may'' slow down the REXX interpreter substantially,
As the stemmed array gets heavily populated, the number of entries ''may'' slow down the REXX interpreter substantially,
<br>depending upon the efficacy of the hashing technique being used for REXX variables (setting/retrieving).
<br>depending upon the efficacy of the hashing technique being used for REXX variables (setting/retrieving).
<syntaxhighlight lang=REXX>/*REXX program generates and displays primes via the sieve of Eratosthenes algorithm.*/
<syntaxhighlight lang="rexx">/*REXX program generates and displays primes via the sieve of Eratosthenes algorithm.*/
parse arg H .; if H=='' | H=="," then H= 200 /*optain optional argument from the CL.*/
parse arg H .; if H=='' | H=="," then H= 200 /*optain optional argument from the CL.*/
w= length(H); @prime= right('prime', 20) /*W: is used for aligning the output.*/
w= length(H); @prime= right('prime', 20) /*W: is used for aligning the output.*/
Line 15,528: Line 15,528:


Also added is a final message indicating the number of primes found.
Also added is a final message indicating the number of primes found.
<syntaxhighlight lang=rexx>/*REXX program generates primes via a wheeled sieve of Eratosthenes algorithm. */
<syntaxhighlight lang="rexx">/*REXX program generates primes via a wheeled sieve of Eratosthenes algorithm. */
parse arg H .; if H=='' then H=200 /*let the highest number be specified. */
parse arg H .; if H=='' then H=200 /*let the highest number be specified. */
tell=h>0; H=abs(H); w=length(H) /*a negative H suppresses prime listing*/
tell=h>0; H=abs(H); w=length(H) /*a negative H suppresses prime listing*/
Line 15,629: Line 15,629:


It also uses a short-circuit test for striking out composites &nbsp; ≤ &nbsp; &radic;{{overline|&nbsp;target&nbsp;}}
It also uses a short-circuit test for striking out composites &nbsp; ≤ &nbsp; &radic;{{overline|&nbsp;target&nbsp;}}
<syntaxhighlight lang=rexx>/*REXX pgm generates and displays primes via a wheeled sieve of Eratosthenes algorithm. */
<syntaxhighlight lang="rexx">/*REXX pgm generates and displays primes via a wheeled sieve of Eratosthenes algorithm. */
parse arg H .; if H=='' | H=="," then H= 200 /*obtain the optional argument from CL.*/
parse arg H .; if H=='' | H=="," then H= 200 /*obtain the optional argument from CL.*/
w= length(H); @prime= right('prime', 20) /*w: is used for aligning the output. */
w= length(H); @prime= right('prime', 20) /*w: is used for aligning the output. */
Line 15,650: Line 15,650:


===Wheel Version restructured===
===Wheel Version restructured===
<syntaxhighlight lang=rexx>/*REXX program generates primes via sieve of Eratosthenes algorithm.
<syntaxhighlight lang="rexx">/*REXX program generates primes via sieve of Eratosthenes algorithm.
* 21.07.2012 Walter Pachl derived from above Rexx version
* 21.07.2012 Walter Pachl derived from above Rexx version
* avoid symbols @ and # (not supported by ooRexx)
* avoid symbols @ and # (not supported by ooRexx)
Line 15,682: Line 15,682:


=={{header|Ring}}==
=={{header|Ring}}==
<syntaxhighlight lang=ring>
<syntaxhighlight lang="ring">
limit = 100
limit = 100
sieve = list(limit)
sieve = list(limit)
Line 15,699: Line 15,699:
=={{header|Ruby}}==
=={{header|Ruby}}==
''eratosthenes'' starts with <code>nums = [nil, nil, 2, 3, 4, 5, ..., n]</code>, then marks ( the nil setting ) multiples of <code>2, 3, 5, 7, ...</code> there, then returns all non-nil numbers which are the primes.
''eratosthenes'' starts with <code>nums = [nil, nil, 2, 3, 4, 5, ..., n]</code>, then marks ( the nil setting ) multiples of <code>2, 3, 5, 7, ...</code> there, then returns all non-nil numbers which are the primes.
<syntaxhighlight lang=ruby>def eratosthenes(n)
<syntaxhighlight lang="ruby">def eratosthenes(n)
nums = [nil, nil, *2..n]
nums = [nil, nil, *2..n]
(2..Math.sqrt(n)).each do |i|
(2..Math.sqrt(n)).each do |i|
Line 15,718: Line 15,718:
* Both inner loops skip multiples of 2 and 3.
* Both inner loops skip multiples of 2 and 3.


<syntaxhighlight lang=ruby>def eratosthenes2(n)
<syntaxhighlight lang="ruby">def eratosthenes2(n)
# For odd i, if i is prime, nums[i >> 1] is true.
# For odd i, if i is prime, nums[i >> 1] is true.
# Set false for all multiples of 3.
# Set false for all multiples of 3.
Line 15,764: Line 15,764:
This simple benchmark compares ''eratosthenes'' with ''eratosthenes2''.
This simple benchmark compares ''eratosthenes'' with ''eratosthenes2''.


<syntaxhighlight lang=ruby>require 'benchmark'
<syntaxhighlight lang="ruby">require 'benchmark'
Benchmark.bmbm {|x|
Benchmark.bmbm {|x|
x.report("eratosthenes") { eratosthenes(1_000_000) }
x.report("eratosthenes") { eratosthenes(1_000_000) }
Line 15,775: Line 15,775:
[[MRI]] 1.9.x implements the sieve of Eratosthenes at file [http://redmine.ruby-lang.org/projects/ruby-19/repository/entry/lib/prime.rb prime.rb], <code>class EratosthensesSeive</code> (around [http://redmine.ruby-lang.org/projects/ruby-19/repository/entry/lib/prime.rb#L421 line 421]). This implementation optimizes for space, by packing the booleans into 16-bit integers. It also hardcodes all primes less than 256.
[[MRI]] 1.9.x implements the sieve of Eratosthenes at file [http://redmine.ruby-lang.org/projects/ruby-19/repository/entry/lib/prime.rb prime.rb], <code>class EratosthensesSeive</code> (around [http://redmine.ruby-lang.org/projects/ruby-19/repository/entry/lib/prime.rb#L421 line 421]). This implementation optimizes for space, by packing the booleans into 16-bit integers. It also hardcodes all primes less than 256.


<syntaxhighlight lang=ruby>require 'prime'
<syntaxhighlight lang="ruby">require 'prime'
p Prime::EratosthenesGenerator.new.take_while {|i| i <= 100}</syntaxhighlight>
p Prime::EratosthenesGenerator.new.take_while {|i| i <= 100}</syntaxhighlight>


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<syntaxhighlight lang=runbasic>input "Gimme the limit:"; limit
<syntaxhighlight lang="runbasic">input "Gimme the limit:"; limit
dim flags(limit)
dim flags(limit)
for i = 2 to limit
for i = 2 to limit
Line 15,797: Line 15,797:
A slightly more idiomatic, optimized and modern iterator output example.
A slightly more idiomatic, optimized and modern iterator output example.


<syntaxhighlight lang=rust>fn primes(n: usize) -> impl Iterator<Item = usize> {
<syntaxhighlight lang="rust">fn primes(n: usize) -> impl Iterator<Item = usize> {
const START: usize = 2;
const START: usize = 2;
if n < START {
if n < START {
Line 15,825: Line 15,825:


===Sieve of Eratosthenes - No optimization===
===Sieve of Eratosthenes - No optimization===
<syntaxhighlight lang=rust>fn simple_sieve(limit: usize) -> Vec<usize> {
<syntaxhighlight lang="rust">fn simple_sieve(limit: usize) -> Vec<usize> {


let mut is_prime = vec![true; limit+1];
let mut is_prime = vec![true; limit+1];
Line 15,856: Line 15,856:
The above code doesn't even do the basic optimizing of only culling composites by primes up to the square root of the range as allowed in the task; it also outputs a vector of resulting primes, which consumes memory. The following code fixes both of those, outputting the results as an Iterator:
The above code doesn't even do the basic optimizing of only culling composites by primes up to the square root of the range as allowed in the task; it also outputs a vector of resulting primes, which consumes memory. The following code fixes both of those, outputting the results as an Iterator:


<syntaxhighlight lang=rust>use std::iter::{empty, once};
<syntaxhighlight lang="rust">use std::iter::{empty, once};
use std::time::Instant;
use std::time::Instant;


Line 15,912: Line 15,912:
The following code improves the above code by sieving only odd composite numbers as 2 is the only even prime for a reduction in number of operations by a factor of about two and a half with reduction of memory use by a factor of two, and bit-packs the composite sieving array for a further reduction of memory use by a factor of eight and with some saving in time due to better CPU cache use for a given sieving range; it also demonstrates how to eliminate the redundant array bounds check:
The following code improves the above code by sieving only odd composite numbers as 2 is the only even prime for a reduction in number of operations by a factor of about two and a half with reduction of memory use by a factor of two, and bit-packs the composite sieving array for a further reduction of memory use by a factor of eight and with some saving in time due to better CPU cache use for a given sieving range; it also demonstrates how to eliminate the redundant array bounds check:


<syntaxhighlight lang=rust>fn optimized_sieve(limit: usize) -> Box<Iterator<Item = usize>> {
<syntaxhighlight lang="rust">fn optimized_sieve(limit: usize) -> Box<Iterator<Item = usize>> {
if limit < 3 {
if limit < 3 {
return if limit < 2 { Box::new(empty()) } else { Box::new(once(2)) }
return if limit < 2 { Box::new(empty()) } else { Box::new(once(2)) }
Line 15,952: Line 15,952:
While that above code is quite fast, as the range increases above the 10's of millions it begins to lose efficiency due to loss of CPU cache associativity as the size of the one-large-array used for culling composites grows beyond the limits of the various CPU caches. Accordingly the following page-segmented code where each culling page can be limited to not larger than the L1 CPU cache is about four times faster than the above for the range of one billion:
While that above code is quite fast, as the range increases above the 10's of millions it begins to lose efficiency due to loss of CPU cache associativity as the size of the one-large-array used for culling composites grows beyond the limits of the various CPU caches. Accordingly the following page-segmented code where each culling page can be limited to not larger than the L1 CPU cache is about four times faster than the above for the range of one billion:


<syntaxhighlight lang=rust>use std::iter::{empty, once};
<syntaxhighlight lang="rust">use std::iter::{empty, once};
use std::rc::Rc;
use std::rc::Rc;
use std::cell::RefCell;
use std::cell::RefCell;
Line 16,139: Line 16,139:


=={{header|S-BASIC}}==
=={{header|S-BASIC}}==
<syntaxhighlight lang=basic>
<syntaxhighlight lang="basic">
comment
comment
Find primes up to the specified limit (here 1,000) using
Find primes up to the specified limit (here 1,000) using
Line 16,202: Line 16,202:
The following defines an IML routine to compute the sieve, and as an example stores the primes below 1000 in a dataset.
The following defines an IML routine to compute the sieve, and as an example stores the primes below 1000 in a dataset.


<lang>proc iml;
<syntaxhighlight lang="text">proc iml;
start sieve(n);
start sieve(n);
a = J(n,1);
a = J(n,1);
Line 16,223: Line 16,223:
{{incorrect|SASL|These use REM (division) testing and so are Trial Division algorithms, not Sieve of Eratosthenes.}}
{{incorrect|SASL|These use REM (division) testing and so are Trial Division algorithms, not Sieve of Eratosthenes.}}
Copied from SASL manual, top of page 36. This provides an infinite list.
Copied from SASL manual, top of page 36. This provides an infinite list.
<syntaxhighlight lang=SASL>
<syntaxhighlight lang="sasl">
show primes
show primes
WHERE
WHERE
Line 16,232: Line 16,232:


The limited list for the first 1000 numbers
The limited list for the first 1000 numbers
<syntaxhighlight lang=SASL>
<syntaxhighlight lang="sasl">
show primes
show primes
WHERE
WHERE
Line 16,243: Line 16,243:


=== Genuine Eratosthenes sieve===
=== Genuine Eratosthenes sieve===
<syntaxhighlight lang=Scala>import scala.annotation.tailrec
<syntaxhighlight lang="scala">import scala.annotation.tailrec
import scala.collection.parallel.mutable
import scala.collection.parallel.mutable
import scala.compat.Platform
import scala.compat.Platform
Line 16,275: Line 16,275:
The following [['''odds-only''']] code is written in a very concise functional style (no mutable state other than the contents of the composites buffer and "higher order functions" for clarity), in this case using a Scala mutable BitSet:
The following [['''odds-only''']] code is written in a very concise functional style (no mutable state other than the contents of the composites buffer and "higher order functions" for clarity), in this case using a Scala mutable BitSet:


<syntaxhighlight lang=Scala>object SoEwithBitSet {
<syntaxhighlight lang="scala">object SoEwithBitSet {
def makeSoE_PrimesTo(top: Int): Iterator[Int] = {
def makeSoE_PrimesTo(top: Int): Iterator[Int] = {
val topNdx = (top - 3) / 2 //odds composite BitSet buffer offset down to 3
val topNdx = (top - 3) / 2 //odds composite BitSet buffer offset down to 3
Line 16,290: Line 16,290:
The below [['''odds-only''']] code using a primitive array (bit packed) and tail recursion to avoid some of the enumeration delays due to nested complex "higher order" function calls is almost eight times faster than the above more functional code:
The below [['''odds-only''']] code using a primitive array (bit packed) and tail recursion to avoid some of the enumeration delays due to nested complex "higher order" function calls is almost eight times faster than the above more functional code:


<syntaxhighlight lang=Scala>object SoEwithArray {
<syntaxhighlight lang="scala">object SoEwithArray {
def makeSoE_PrimesTo(top: Int) = {
def makeSoE_PrimesTo(top: Int) = {
import scala.annotation.tailrec
import scala.annotation.tailrec
Line 16,321: Line 16,321:
It can be tested with the following code:
It can be tested with the following code:


<syntaxhighlight lang=Scala>object Main extends App {
<syntaxhighlight lang="scala">object Main extends App {
import SoEwithArray._
import SoEwithArray._
val top_num = 100000000
val top_num = 100000000
Line 16,342: Line 16,342:
The above code still uses an amount of memory proportional to the range of the sieve (although bit-packed as 8 values per byte). As well as only sieving odd candidates, the following code uses a fixed range buffer that is about the size of the CPU L2 cache plus only storage for the base primes up to the square root of the range for a large potential saving in RAM memory used as well as greatly reducing memory access times. The use of innermost tail recursive loops for critical loops where the majority of the execution time is spent rather than "higher order" functions from iterators also greatly reduces execution time, with much of the remaining time used just to enumerate the primes output:
The above code still uses an amount of memory proportional to the range of the sieve (although bit-packed as 8 values per byte). As well as only sieving odd candidates, the following code uses a fixed range buffer that is about the size of the CPU L2 cache plus only storage for the base primes up to the square root of the range for a large potential saving in RAM memory used as well as greatly reducing memory access times. The use of innermost tail recursive loops for critical loops where the majority of the execution time is spent rather than "higher order" functions from iterators also greatly reduces execution time, with much of the remaining time used just to enumerate the primes output:


<syntaxhighlight lang=Scala>object APFSoEPagedOdds {
<syntaxhighlight lang="scala">object APFSoEPagedOdds {
import scala.annotation.tailrec
import scala.annotation.tailrec
Line 16,437: Line 16,437:
As the above and all following sieves are "infinite", they all require an extra range limiting condition to produce a finite output, such as the addition of ".takeWhile(_ <= topLimit)" where "topLimit" is the specified range as is done in the following code:
As the above and all following sieves are "infinite", they all require an extra range limiting condition to produce a finite output, such as the addition of ".takeWhile(_ <= topLimit)" where "topLimit" is the specified range as is done in the following code:


<syntaxhighlight lang=Scala>object MainSoEPagedOdds extends App {
<syntaxhighlight lang="scala">object MainSoEPagedOdds extends App {
import APFSoEPagedOdds._
import APFSoEPagedOdds._
countSoEPrimesTo(100)
countSoEPrimesTo(100)
Line 16,463: Line 16,463:
The following code uses delayed recursion via Streams to implement the Richard Bird algorithm mentioned in the last part (the Epilogue) of [http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf M.O'Neill's paper], which is '''a true incremental Sieve of Eratosthenes'''. It is nowhere near as fast as the array based solutions due to the overhead of functionally chasing the merging of the prime multiple streams; this also means that the empirical performance is not according to the usual Sieve of Eratosthenes approximations due to this overhead increasing as the log of the sieved range, but it is much better than [[Primality_by_trial_division#Odds-Only_.22infinite.22_primes_generator_using_Streams_and_Co-Inductive_Streams|the "unfaithful" sieve]].
The following code uses delayed recursion via Streams to implement the Richard Bird algorithm mentioned in the last part (the Epilogue) of [http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf M.O'Neill's paper], which is '''a true incremental Sieve of Eratosthenes'''. It is nowhere near as fast as the array based solutions due to the overhead of functionally chasing the merging of the prime multiple streams; this also means that the empirical performance is not according to the usual Sieve of Eratosthenes approximations due to this overhead increasing as the log of the sieved range, but it is much better than [[Primality_by_trial_division#Odds-Only_.22infinite.22_primes_generator_using_Streams_and_Co-Inductive_Streams|the "unfaithful" sieve]].


<syntaxhighlight lang=Scala> def birdPrimes() = {
<syntaxhighlight lang="scala"> def birdPrimes() = {
def oddPrimes: Stream[Int] = {
def oddPrimes: Stream[Int] = {
def merge(xs: Stream[Int], ys: Stream[Int]): Stream[Int] = {
def merge(xs: Stream[Int], ys: Stream[Int]): Stream[Int] = {
Line 16,486: Line 16,486:
}</syntaxhighlight>
}</syntaxhighlight>


Now this algorithm doesn't really need the memoization and full laziness as offered by Streams, so an implementation and use of a Co-Inductive Stream (CIS) class is sufficient and reduces execution time by almost a factor of two:<syntaxhighlight lang=scala> class CIS[A](val start: A, val continue: () => CIS[A])
Now this algorithm doesn't really need the memoization and full laziness as offered by Streams, so an implementation and use of a Co-Inductive Stream (CIS) class is sufficient and reduces execution time by almost a factor of two:<syntaxhighlight lang="scala"> class CIS[A](val start: A, val continue: () => CIS[A])


def primesBirdCIS: Iterator[Int] = {
def primesBirdCIS: Iterator[Int] = {
Line 16,525: Line 16,525:
As per [http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf the "unfaithful sieve" article linked above], the incremental "infinite" Sieve of Eratosthenes can be implemented using a hash table instead of a Priority Queue or Map (Binary Heap) as were used in that article. The following implementation postpones the adding of base prime representations to the hash table until necessary to keep the hash table small:
As per [http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf the "unfaithful sieve" article linked above], the incremental "infinite" Sieve of Eratosthenes can be implemented using a hash table instead of a Priority Queue or Map (Binary Heap) as were used in that article. The following implementation postpones the adding of base prime representations to the hash table until necessary to keep the hash table small:


<syntaxhighlight lang=scala> def SoEInc: Iterator[Int] = {
<syntaxhighlight lang="scala"> def SoEInc: Iterator[Int] = {
val nextComposites = scala.collection.mutable.HashMap[Int, Int]()
val nextComposites = scala.collection.mutable.HashMap[Int, Int]()
def oddPrimes: Iterator[Int] = {
def oddPrimes: Iterator[Int] = {
Line 16,560: Line 16,560:
===Tail-recursive solution===
===Tail-recursive solution===
{{Works with|Scheme|R<math>^5</math>RS}}
{{Works with|Scheme|R<math>^5</math>RS}}
<syntaxhighlight lang=scheme>; Tail-recursive solution :
<syntaxhighlight lang="scheme">; Tail-recursive solution :
(define (sieve n)
(define (sieve n)
(define (aux u v)
(define (aux u v)
Line 16,583: Line 16,583:


===Simpler, non-tail-recursive solution===
===Simpler, non-tail-recursive solution===
<syntaxhighlight lang=scheme>; Simpler solution, with the penalty that none of 'iota, 'strike or 'sieve is tail-recursive :
<syntaxhighlight lang="scheme">; Simpler solution, with the penalty that none of 'iota, 'strike or 'sieve is tail-recursive :
(define (iota start stop stride)
(define (iota start stop stride)
(if (> start stop)
(if (> start stop)
Line 16,607: Line 16,607:
(newline)</syntaxhighlight>
(newline)</syntaxhighlight>
Output:
Output:
<lang>(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97)</syntaxhighlight>
<syntaxhighlight lang="text">(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97)</syntaxhighlight>
===Optimised using an odds-wheel===
===Optimised using an odds-wheel===
Optimised using a pre-computed wheel based on 2 (i.e. odds only):
Optimised using a pre-computed wheel based on 2 (i.e. odds only):
<syntaxhighlight lang=scheme>(define (primes-wheel-2 limit)
<syntaxhighlight lang="scheme">(define (primes-wheel-2 limit)
(let ((stop (sqrt limit)))
(let ((stop (sqrt limit)))
(define (sieve lst)
(define (sieve lst)
Line 16,622: Line 16,622:
(newline)</syntaxhighlight>
(newline)</syntaxhighlight>
Output:
Output:
<lang>(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97)</syntaxhighlight>
<syntaxhighlight lang="text">(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97)</syntaxhighlight>


===Vector-based===
===Vector-based===
Vector-based (faster), works with R<math>^5</math>RS:
Vector-based (faster), works with R<math>^5</math>RS:
<syntaxhighlight lang=scheme>; initialize v to vector of sequential integers
<syntaxhighlight lang="scheme">; initialize v to vector of sequential integers
(define (initialize! v)
(define (initialize! v)
(define (iter v n) (if (>= n (vector-length v))
(define (iter v n) (if (>= n (vector-length v))
Line 16,673: Line 16,673:
Using SICP-style ''head''-forced streams. Works with MIT-Scheme, Chez Scheme, &ndash; or any other Scheme, if writing out by hand the expansion of the only macro here, <code>s-cons</code>, with explicit lambda. Common functions:
Using SICP-style ''head''-forced streams. Works with MIT-Scheme, Chez Scheme, &ndash; or any other Scheme, if writing out by hand the expansion of the only macro here, <code>s-cons</code>, with explicit lambda. Common functions:


<syntaxhighlight lang=scheme> ;;;; Stream Implementation
<syntaxhighlight lang="scheme"> ;;;; Stream Implementation
(define (head s) (car s))
(define (head s) (car s))
(define (tail s) ((cdr s)))
(define (tail s) ((cdr s)))
Line 16,708: Line 16,708:
====The simplest, naive sieve====
====The simplest, naive sieve====
Very slow, running at ~ <i>n<sup>2.2</sup></i>, empirically, and worsening:
Very slow, running at ~ <i>n<sup>2.2</sup></i>, empirically, and worsening:
<syntaxhighlight lang=scheme> (define (sieve s)
<syntaxhighlight lang="scheme"> (define (sieve s)
(let ((p (head s)))
(let ((p (head s)))
(s-cons p
(s-cons p
Line 16,717: Line 16,717:
Stops at the square root of the upper limit ''m'', running at about ~ <i>n<sup>1.4</sup></i> in ''n'' primes produced, empirically. Returns infinite stream
Stops at the square root of the upper limit ''m'', running at about ~ <i>n<sup>1.4</sup></i> in ''n'' primes produced, empirically. Returns infinite stream
of numbers which is only valid up to ''m'', includes composites above it:
of numbers which is only valid up to ''m'', includes composites above it:
<syntaxhighlight lang=scheme> (define (primes-To m)
<syntaxhighlight lang="scheme"> (define (primes-To m)
(define (sieve s)
(define (sieve s)
(let ((p (head s)))
(let ((p (head s)))
Line 16,728: Line 16,728:
====Combined multiples sieve====
====Combined multiples sieve====
Archetypal, straightforward approach by Richard Bird, presented in [http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf Melissa E. O'Neill article]. Uses <code>s-linear-join</code>, i.e. right fold, which is less efficient and of worse time complexity than the ''tree''-folding that follows. Does not attempt to conserve space by arranging for the additional inner feedback loop, as is done in the tree-folding variant below.
Archetypal, straightforward approach by Richard Bird, presented in [http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf Melissa E. O'Neill article]. Uses <code>s-linear-join</code>, i.e. right fold, which is less efficient and of worse time complexity than the ''tree''-folding that follows. Does not attempt to conserve space by arranging for the additional inner feedback loop, as is done in the tree-folding variant below.
<syntaxhighlight lang=scheme> (define (primes-stream-ala-Bird)
<syntaxhighlight lang="scheme"> (define (primes-stream-ala-Bird)
(define (mults p) (from-By (* p p) p))
(define (mults p) (from-By (* p p) p))
(define primes ;; primes are
(define primes ;; primes are
Line 16,743: Line 16,743:
Here is a version of the same sieve, which is self contained with all the requisite functions wrapped in the overall function; optimized further. It works with odd primes only, and arranges for a separate primes feed for the base primes separate from the output stream, ''calculated recursively'' by the recursive call to "oddprms" in forming "cmpsts". It also ''"fuses"'' two functions, <code>s-diff</code> and <code>from-By</code>, into one, <code>minusstrtat</code>:
Here is a version of the same sieve, which is self contained with all the requisite functions wrapped in the overall function; optimized further. It works with odd primes only, and arranges for a separate primes feed for the base primes separate from the output stream, ''calculated recursively'' by the recursive call to "oddprms" in forming "cmpsts". It also ''"fuses"'' two functions, <code>s-diff</code> and <code>from-By</code>, into one, <code>minusstrtat</code>:


<syntaxhighlight lang=scheme>(define (birdPrimes)
<syntaxhighlight lang="scheme">(define (birdPrimes)
(define (mltpls p)
(define (mltpls p)
(define pm2 (* p 2))
(define pm2 (* p 2))
Line 16,769: Line 16,769:
It can be tested with the following code:
It can be tested with the following code:


<syntaxhighlight lang=scheme>(define (nthPrime n)
<syntaxhighlight lang="scheme">(define (nthPrime n)
(let nxtprm ((cnt 0) (ps (birdPrimes)))
(let nxtprm ((cnt 0) (ps (birdPrimes)))
(if (< cnt n) (nxtprm (+ cnt 1) ((cdr ps))) (car ps))))
(if (< cnt n) (nxtprm (+ cnt 1) ((cdr ps))) (car ps))))
Line 16,782: Line 16,782:
The most efficient. Finds composites as a tree of unions of each prime's multiples.
The most efficient. Finds composites as a tree of unions of each prime's multiples.


<syntaxhighlight lang=scheme> ;;;; all primes' multiples are removed, merged through a tree of unions
<syntaxhighlight lang="scheme"> ;;;; all primes' multiples are removed, merged through a tree of unions
;;;; runs in ~ n^1.15 run time in producing n = 100K .. 1M primes
;;;; runs in ~ n^1.15 run time in producing n = 100K .. 1M primes
(define (primes-stream)
(define (primes-stream)
Line 16,814: Line 16,814:
This can be also accomplished by the following self contained code which follows the format of the <code>birdPrimes</code> code above with the added "pairs" function integrated into the "mrgmltpls" function:
This can be also accomplished by the following self contained code which follows the format of the <code>birdPrimes</code> code above with the added "pairs" function integrated into the "mrgmltpls" function:


<syntaxhighlight lang=scheme>(define (treemergePrimes)
<syntaxhighlight lang="scheme">(define (treemergePrimes)
(define (mltpls p)
(define (mltpls p)
(define pm2 (* p 2))
(define pm2 (* p 2))
Line 16,846: Line 16,846:
===Generators===
===Generators===


<syntaxhighlight lang=scheme>(define (integers n)
<syntaxhighlight lang="scheme">(define (integers n)
(lambda ()
(lambda ()
(let ((ans n))
(let ((ans n))
Line 16,873: Line 16,873:


=={{header|Scilab}}==
=={{header|Scilab}}==
<syntaxhighlight lang=scliab>function a = sieve(n)
<syntaxhighlight lang="scliab">function a = sieve(n)
a = ~zeros(n, 1)
a = ~zeros(n, 1)
a(1) = %f
a(1) = %f
Line 16,895: Line 16,895:


=={{header|Scratch}}==
=={{header|Scratch}}==
<syntaxhighlight lang=Scratch>
<syntaxhighlight lang="scratch">
when clicked
when clicked
broadcast: fill list with zero (0) and wait
broadcast: fill list with zero (0) and wait
Line 16,953: Line 16,953:
=={{header|Seed7}}==
=={{header|Seed7}}==
The program below computes the number of primes between 1 and 10000000:
The program below computes the number of primes between 1 and 10000000:
<syntaxhighlight lang=seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";


const func set of integer: eratosthenes (in integer: n) is func
const func set of integer: eratosthenes (in integer: n) is func
Line 16,981: Line 16,981:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Raku}}
{{trans|Raku}}
<syntaxhighlight lang=ruby>func sieve(limit) {
<syntaxhighlight lang="ruby">func sieve(limit) {
var sieve_arr = [false, false, (limit-1).of(true)...]
var sieve_arr = [false, false, (limit-1).of(true)...]
gather {
gather {
Line 17,002: Line 17,002:


Alternative implementation:
Alternative implementation:
<syntaxhighlight lang=ruby>func sieve(limit) {
<syntaxhighlight lang="ruby">func sieve(limit) {
var composite = []
var composite = []
for n in (2 .. limit.isqrt) {
for n in (2 .. limit.isqrt) {
Line 17,016: Line 17,016:
=={{header|Simula}}==
=={{header|Simula}}==
{{works with|Simula-67}}
{{works with|Simula-67}}
<syntaxhighlight lang=simula>BEGIN
<syntaxhighlight lang="simula">BEGIN
INTEGER ARRAY t(0:1000);
INTEGER ARRAY t(0:1000);
INTEGER i,j,k;
INTEGER i,j,k;
Line 17,067: Line 17,067:
997</pre>
997</pre>
===A Concurrent Prime Sieve===
===A Concurrent Prime Sieve===
<syntaxhighlight lang=simula>
<syntaxhighlight lang="simula">
! A CONCURRENT PRIME SIEVE ;
! A CONCURRENT PRIME SIEVE ;


Line 17,224: Line 17,224:
=={{header|Smalltalk}}==
=={{header|Smalltalk}}==
A simple implementation that you can run in a workspace. It finds all the prime numbers up to and including <i>limit</i>—for the sake of example, up to and including 100.
A simple implementation that you can run in a workspace. It finds all the prime numbers up to and including <i>limit</i>—for the sake of example, up to and including 100.
<syntaxhighlight lang=smalltalk>| potentialPrimes limit |
<syntaxhighlight lang="smalltalk">| potentialPrimes limit |
limit := 100.
limit := 100.
potentialPrimes := Array new: limit.
potentialPrimes := Array new: limit.
Line 17,245: Line 17,245:
Using strings instead of arrays, and the square/sqrt optimizations.
Using strings instead of arrays, and the square/sqrt optimizations.


<syntaxhighlight lang=SNOBOL4> define('sieve(n)i,j,k,p,str,res') :(sieve_end)
<syntaxhighlight lang="snobol4"> define('sieve(n)i,j,k,p,str,res') :(sieve_end)
sieve i = lt(i,n - 1) i + 1 :f(sv1)
sieve i = lt(i,n - 1) i + 1 :f(sv1)
str = str (i + 1) ' ' :(sieve)
str = str (i + 1) ' ' :(sieve)
Line 17,268: Line 17,268:
=={{header|Standard ML}}==
=={{header|Standard ML}}==
Works with SML/NJ. This uses BitArrays which are available in SML/NJ. The algorithm is the one on wikipedia, referred to above. Limit: Memory, normally. When more than 20 petabyte of memory available, this code will have its limitation at a maximum integer around 1.44*10E17, due to the maximum list length in SMLNJ. Using two extra loops, however, bit arrays can simply be stored to disk and processed in multiple lists. With a tail recursive wrapper function as well, the upper limit will be determined by available disk space only.
Works with SML/NJ. This uses BitArrays which are available in SML/NJ. The algorithm is the one on wikipedia, referred to above. Limit: Memory, normally. When more than 20 petabyte of memory available, this code will have its limitation at a maximum integer around 1.44*10E17, due to the maximum list length in SMLNJ. Using two extra loops, however, bit arrays can simply be stored to disk and processed in multiple lists. With a tail recursive wrapper function as well, the upper limit will be determined by available disk space only.
<syntaxhighlight lang=Standard ML>
<syntaxhighlight lang="standard ml">
val segmentedSieve = fn N =>
val segmentedSieve = fn N =>
(* output : list of {segment=bit segment, start=number at startposition segment} *)
(* output : list of {segment=bit segment, start=number at startposition segment} *)
Line 17,379: Line 17,379:
</syntaxhighlight>
</syntaxhighlight>
Example, segment size 120 million, prime numbers up to 2.5 billion:
Example, segment size 120 million, prime numbers up to 2.5 billion:
<syntaxhighlight lang=Standard ML>
<syntaxhighlight lang="standard ml">
-val writeSegment = fn L : {segment:BitArray.array, start:IntInf.int} list => fn NthSegment =>
-val writeSegment = fn L : {segment:BitArray.array, start:IntInf.int} list => fn NthSegment =>
let
let
Line 17,405: Line 17,405:
=={{header|Stata}}==
=={{header|Stata}}==
A program to create a dataset with a variable p containing primes up to a given number.
A program to create a dataset with a variable p containing primes up to a given number.
<syntaxhighlight lang=stata>prog def sieve
<syntaxhighlight lang="stata">prog def sieve
args n
args n
clear
clear
Line 17,427: Line 17,427:


Example call
Example call
<syntaxhighlight lang=stata>sieve 100
<syntaxhighlight lang="stata">sieve 100
list in 1/10 // show only the first ten primes
list in 1/10 // show only the first ten primes


Line 17,448: Line 17,448:
=== Mata ===
=== Mata ===


<syntaxhighlight lang=stata>mata
<syntaxhighlight lang="stata">mata
real colvector sieve(real scalar n) {
real colvector sieve(real scalar n) {
real colvector a
real colvector a
Line 17,475: Line 17,475:


=={{header|Swift}}==
=={{header|Swift}}==
<syntaxhighlight lang=swift>import Foundation // for sqrt() and Date()
<syntaxhighlight lang="swift">import Foundation // for sqrt() and Date()


let max = 1_000_000
let max = 1_000_000
Line 17,509: Line 17,509:


The most obvious two improvements are to sieve for only odds as two is the only even prime and to make the sieving array bit-packed so that instead of a whole 8-bit byte per number representation there, each is represented by just one bit; these two changes improved memory use by a factor of 16 and the better CPU cache locality more than compensates for the extra code required to implement bit packing as per the following code:
The most obvious two improvements are to sieve for only odds as two is the only even prime and to make the sieving array bit-packed so that instead of a whole 8-bit byte per number representation there, each is represented by just one bit; these two changes improved memory use by a factor of 16 and the better CPU cache locality more than compensates for the extra code required to implement bit packing as per the following code:
<syntaxhighlight lang=swift>func soePackedOdds(_ n: Int) ->
<syntaxhighlight lang="swift">func soePackedOdds(_ n: Int) ->
LazyMapSequence<UnfoldSequence<Int, (Int?, Bool)>, Int> {
LazyMapSequence<UnfoldSequence<Int, (Int?, Bool)>, Int> {
Line 17,540: Line 17,540:


To use Swift's "higher order functions" on the generated `Sequence`'s effectively, one needs unbounded (or only by the numeric range chosen for the implementation) sieves. Many of these are incremental sieves that, instead of buffering a series of culled arrays, records the culling structure of the culling base primes (which should be a secondary stream of primes for efficiency) and produces the primes incrementally through reference and update of that structure. Various structures may be chosen for this, as in a MinHeap Priority Queue, a Hash Dictionary, or a simple List tree structure as used in the following code:
To use Swift's "higher order functions" on the generated `Sequence`'s effectively, one needs unbounded (or only by the numeric range chosen for the implementation) sieves. Many of these are incremental sieves that, instead of buffering a series of culled arrays, records the culling structure of the culling base primes (which should be a secondary stream of primes for efficiency) and produces the primes incrementally through reference and update of that structure. Various structures may be chosen for this, as in a MinHeap Priority Queue, a Hash Dictionary, or a simple List tree structure as used in the following code:
<syntaxhighlight lang=swift>import Foundation
<syntaxhighlight lang="swift">import Foundation


func soeTreeFoldingOdds() -> UnfoldSequence<Int, (Int?, Bool)> {
func soeTreeFoldingOdds() -> UnfoldSequence<Int, (Int?, Bool)> {
Line 17,623: Line 17,623:


As the above code is slow due to memory allocations/de-allocations and the inherent extra "log n" term in the complexity, the following code uses a Hash Dictionary which has an average of O(1) access time (without the "log n" and uses mutability for increased seed so is in no way purely functional:
As the above code is slow due to memory allocations/de-allocations and the inherent extra "log n" term in the complexity, the following code uses a Hash Dictionary which has an average of O(1) access time (without the "log n" and uses mutability for increased seed so is in no way purely functional:
<syntaxhighlight lang=swift>func soeDictOdds() -> UnfoldSequence<Int, Int> {
<syntaxhighlight lang="swift">func soeDictOdds() -> UnfoldSequence<Int, Int> {
var bp = 5; var q = 25
var bp = 5; var q = 25
var bps: UnfoldSequence<Int, Int>.Iterator? = nil
var bps: UnfoldSequence<Int, Int>.Iterator? = nil
Line 17,656: Line 17,656:


An unbounded array-based algorithm can be written that combines the excellent cache locality of the second bounded version above but is unbounded by producing a sequence of sieved bit-packed arrays that are CPU cache size as required with a secondary stream of base primes used in culling produced in the same fashion, as in the following code:
An unbounded array-based algorithm can be written that combines the excellent cache locality of the second bounded version above but is unbounded by producing a sequence of sieved bit-packed arrays that are CPU cache size as required with a secondary stream of base primes used in culling produced in the same fashion, as in the following code:
<syntaxhighlight lang=swift>import Foundation
<syntaxhighlight lang="swift">import Foundation


typealias Prime = UInt64
typealias Prime = UInt64
Line 17,982: Line 17,982:


=={{header|Tailspin}}==
=={{header|Tailspin}}==
<syntaxhighlight lang=tailspin>
<syntaxhighlight lang="tailspin">
templates sieve
templates sieve
def limit: $;
def limit: $;
Line 18,013: Line 18,013:


Better version using the mutability of the @-state to just update a primality flag
Better version using the mutability of the @-state to just update a primality flag
<syntaxhighlight lang=tailspin>
<syntaxhighlight lang="tailspin">
templates sieve
templates sieve
def limit: $;
def limit: $;
Line 18,034: Line 18,034:


=={{header|Tcl}}==
=={{header|Tcl}}==
<syntaxhighlight lang=tcl>package require Tcl 8.5
<syntaxhighlight lang="tcl">package require Tcl 8.5


proc sieve n {
proc sieve n {
Line 18,065: Line 18,065:


=={{header|TI-83 BASIC}}==
=={{header|TI-83 BASIC}}==
<syntaxhighlight lang=ti83b>Input "Limit:",N
<syntaxhighlight lang="ti83b">Input "Limit:",N
N→Dim(L1)
N→Dim(L1)
For(I,2,N)
For(I,2,N)
Line 18,091: Line 18,091:
{{works with|Korn Shell}}
{{works with|Korn Shell}}
{{works with|Zsh}}
{{works with|Zsh}}
<syntaxhighlight lang=bash>function primes {
<syntaxhighlight lang="bash">function primes {
typeset -a a
typeset -a a
typeset i j
typeset i j
Line 18,120: Line 18,120:


{{works with|Bourne Shell}}
{{works with|Bourne Shell}}
<syntaxhighlight lang=bash>#! /bin/sh
<syntaxhighlight lang="bash">#! /bin/sh


LIMIT=1000
LIMIT=1000
Line 18,174: Line 18,174:


{{works with|Bourne Shell}}
{{works with|Bourne Shell}}
<syntaxhighlight lang=bash>sourc() {
<syntaxhighlight lang="bash">sourc() {
seq 2 1000
seq 2 1000
}
}
Line 18,197: Line 18,197:


{{works with|Bourne Shell}}
{{works with|Bourne Shell}}
<syntaxhighlight lang=bash># Fill $1 characters with $2.
<syntaxhighlight lang="bash"># Fill $1 characters with $2.
fill () {
fill () {
# This pipeline would begin
# This pipeline would begin
Line 18,233: Line 18,233:
==={{header|C Shell}}===
==={{header|C Shell}}===
{{trans|CMake}}
{{trans|CMake}}
<syntaxhighlight lang=csh># Sieve of Eratosthenes: Echoes all prime numbers through $limit.
<syntaxhighlight lang="csh"># Sieve of Eratosthenes: Echoes all prime numbers through $limit.
@ limit = 80
@ limit = 80


Line 18,265: Line 18,265:
{{incorrect|Ursala|It probably (remainder) uses rem testing and so is a trial division algorithm, not a sieve of Eratosthenes.}}
{{incorrect|Ursala|It probably (remainder) uses rem testing and so is a trial division algorithm, not a sieve of Eratosthenes.}}
with no optimizations
with no optimizations
<syntaxhighlight lang=Ursala>#import nat
<syntaxhighlight lang="ursala">#import nat


sieve = ~<{0,1}&& iota; @NttPX ~&r->lx ^/~&rhPlC remainder@rlX~|@r</syntaxhighlight>
sieve = ~<{0,1}&& iota; @NttPX ~&r->lx ^/~&rhPlC remainder@rlX~|@r</syntaxhighlight>
test program:
test program:
<syntaxhighlight lang=Ursala>#cast %nL
<syntaxhighlight lang="ursala">#cast %nL


example = sieve 50</syntaxhighlight>{{out}}
example = sieve 50</syntaxhighlight>{{out}}
Line 18,276: Line 18,276:
=={{header|Vala}}==
=={{header|Vala}}==
{{libheader|Gee}}Without any optimizations:
{{libheader|Gee}}Without any optimizations:
<syntaxhighlight lang=vala>using Gee;
<syntaxhighlight lang="vala">using Gee;


ArrayList<int> primes(int limit){
ArrayList<int> primes(int limit){
Line 18,317: Line 18,317:


=={{header|VAX Assembly}}==
=={{header|VAX Assembly}}==
<syntaxhighlight lang=VAX Assembly> 000F4240 0000 1 n=1000*1000
<syntaxhighlight lang="vax assembly"> 000F4240 0000 1 n=1000*1000
0000 0000 2 .entry main,0
0000 0000 2 .entry main,0
7E 7CFD 0002 3 clro -(sp) ;result buffer
7E 7CFD 0002 3 clro -(sp) ;result buffer
Line 18,348: Line 18,348:


=={{header|VBA}}==
=={{header|VBA}}==
Using Excel<syntaxhighlight lang=vb> Sub primes()
Using Excel<syntaxhighlight lang="vb"> Sub primes()
'BRRJPA
'BRRJPA
'Prime calculation for VBA_Excel
'Prime calculation for VBA_Excel
Line 18,382: Line 18,382:
=={{header|VBScript}}==
=={{header|VBScript}}==
To run in console mode with cscript.
To run in console mode with cscript.
<syntaxhighlight lang=vb>
<syntaxhighlight lang="vb">
Dim sieve()
Dim sieve()
If WScript.Arguments.Count>=1 Then
If WScript.Arguments.Count>=1 Then
Line 18,443: Line 18,443:
=={{header|Visual Basic}}==
=={{header|Visual Basic}}==
'''Works with:''' VB6
'''Works with:''' VB6
<syntaxhighlight lang=vb>Sub Eratost()
<syntaxhighlight lang="vb">Sub Eratost()
Dim sieve() As Boolean
Dim sieve() As Boolean
Dim n As Integer, i As Integer, j As Integer
Dim n As Integer, i As Integer, j As Integer
Line 18,464: Line 18,464:


=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
<syntaxhighlight lang=vbnet>Dim n As Integer, k As Integer, limit As Integer
<syntaxhighlight lang="vbnet">Dim n As Integer, k As Integer, limit As Integer
Console.WriteLine("Enter number to search to: ")
Console.WriteLine("Enter number to search to: ")
limit = Console.ReadLine
limit = Console.ReadLine
Line 18,484: Line 18,484:
===Alternate===
===Alternate===
Since the sieves are being removed only above the current iteration, the separate loop for display is unnecessary. And no '''Math.Sqrt()''' needed. Also, input is from command line parameter instead of Console.ReadLine(). Consolidated ''If'' block with ''For'' statement into two ''Do'' loops.
Since the sieves are being removed only above the current iteration, the separate loop for display is unnecessary. And no '''Math.Sqrt()''' needed. Also, input is from command line parameter instead of Console.ReadLine(). Consolidated ''If'' block with ''For'' statement into two ''Do'' loops.
<syntaxhighlight lang=vbnet>Module Module1
<syntaxhighlight lang="vbnet">Module Module1
Sub Main(args() As String)
Sub Main(args() As String)
Dim lmt As Integer = 500, n As Integer = 2, k As Integer
Dim lmt As Integer = 500, n As Integer = 2, k As Integer
Line 18,504: Line 18,504:
{{trans|go}}
{{trans|go}}
===Basic sieve of array of booleans===
===Basic sieve of array of booleans===
<syntaxhighlight lang=vlang>fn main() {
<syntaxhighlight lang="vlang">fn main() {
limit := 201 // means sieve numbers < 201
limit := 201 // means sieve numbers < 201
Line 18,557: Line 18,557:


=={{header|Vorpal}}==
=={{header|Vorpal}}==
<syntaxhighlight lang=vorpal>self.print_primes = method(m){
<syntaxhighlight lang="vorpal">self.print_primes = method(m){
p = new()
p = new()
p.fill(0, m, 1, true)
p.fill(0, m, 1, true)
Line 18,629: Line 18,629:
=={{header|Xojo}}==
=={{header|Xojo}}==
Place the following in the '''Run''' event handler of a Console application:
Place the following in the '''Run''' event handler of a Console application:
<syntaxhighlight lang=Xojo>Dim limit, prime, i As Integer
<syntaxhighlight lang="xojo">Dim limit, prime, i As Integer
Dim s As String
Dim s As String
Dim t As Double
Dim t As Double
Line 18,690: Line 18,690:
This version uses a dynamic array and can use (a lot) less memory. It's (a lot) slower too.
This version uses a dynamic array and can use (a lot) less memory. It's (a lot) slower too.
Since Booleans are manually set to True, the algorithm makes more sense.
Since Booleans are manually set to True, the algorithm makes more sense.
<syntaxhighlight lang=Xojo>Dim limit, prime, i As Integer
<syntaxhighlight lang="xojo">Dim limit, prime, i As Integer
Dim s As String
Dim s As String
Dim t As Double
Dim t As Double
Line 18,758: Line 18,758:
=={{header|Woma}}==
=={{header|Woma}}==


<syntaxhighlight lang=woma>(sieve(n = /0 -> int; limit = /0 -> int; is_prime = [/0] -> *)) *
<syntaxhighlight lang="woma">(sieve(n = /0 -> int; limit = /0 -> int; is_prime = [/0] -> *)) *
i<@>range(n*n, limit+1, n)
i<@>range(n*n, limit+1, n)
is_prime = is_prime[$]i,False
is_prime = is_prime[$]i,False
Line 18,781: Line 18,781:


=={{header|Wren}}==
=={{header|Wren}}==
<syntaxhighlight lang=ecmascript>var sieveOfE = Fn.new { |n|
<syntaxhighlight lang="ecmascript">var sieveOfE = Fn.new { |n|
if (n < 2) return []
if (n < 2) return []
var comp = List.filled(n-1, false)
var comp = List.filled(n-1, false)
Line 18,813: Line 18,813:


=={{header|XPL0}}==
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
int Size, Prime, I, Kill;
int Size, Prime, I, Kill;
char Flag;
char Flag;
Line 18,840: Line 18,840:


=={{header|Yabasic}}==
=={{header|Yabasic}}==
<syntaxhighlight lang=Yabasic>#!/usr/bin/yabasic
<syntaxhighlight lang="yabasic">#!/usr/bin/yabasic


// ---------------------------
// ---------------------------
Line 18,895: Line 18,895:


=={{header|Zig}}==
=={{header|Zig}}==
<syntaxhighlight lang=Zig>
<syntaxhighlight lang="zig">
const std = @import("std");
const std = @import("std");
const stdout = std.io.getStdOut().outStream();
const stdout = std.io.getStdOut().outStream();
Line 18,951: Line 18,951:
{{trans|BCPL}}
{{trans|BCPL}}
Includes the iterator, as with the BCPL Odds-only bit packed sieve. Since it's not much extra code, the sieve object also includes methods for getting the size and testing for membership.
Includes the iterator, as with the BCPL Odds-only bit packed sieve. Since it's not much extra code, the sieve object also includes methods for getting the size and testing for membership.
<syntaxhighlight lang=Zig>
<syntaxhighlight lang="zig">
const std = @import("std");
const std = @import("std");
const heap = std.heap;
const heap = std.heap;
Line 19,135: Line 19,135:
</pre>
</pre>
===Optimized version===
===Optimized version===
<syntaxhighlight lang=Zig>
<syntaxhighlight lang="zig">
const stdout = @import("std").io.getStdOut().writer();
const stdout = @import("std").io.getStdOut().writer();


Line 19,190: Line 19,190:


=={{header|zkl}}==
=={{header|zkl}}==
<syntaxhighlight lang=zkl>fcn sieve(limit){
<syntaxhighlight lang="zkl">fcn sieve(limit){
composite:=Data(limit+1).fill(1); // bucket of bytes set to 1 (prime)
composite:=Data(limit+1).fill(1); // bucket of bytes set to 1 (prime)
(2).pump(limit.toFloat().sqrt()+1, Void, // Void==no results, just loop
(2).pump(limit.toFloat().sqrt()+1, Void, // Void==no results, just loop