Sieve of Eratosthenes: Difference between revisions

m
imported>Polarit
Line 6,647:
Found 78498 primes to 1000000 in 192 milliseconds.</pre>
 
 
==={{header|Elm Sieve of Eratosthenes function with range of primes}}===
{{incorrect|Bash|This version uses remainder testing (modBy) and so is a trial division algorithm, not a sieve of Eratosthenes.}}
Note: The David Turner algorithm is incorrect as per [https://en.m.wikipedia.org/wiki/Sieve_of_Eratosthenes Sieve of Eratosthenes article on Wikipedia]
 
The range of primes output is defined by the lower and upper limit.
This module exposes the function sieve to be imported by the module Main.
The test case is performed in elm repl.
 
<syntaxhighlight lang="elm">module Sieve exposing (sieve)
 
sieve : Int -> Int -> List Int
sieve min limit =
let
numbers : List Int
numbers =
List.range 2 limit
 
last : Int
last =
limit
|> toFloat
|> sqrt
|> round
 
isMultiple : Int -> Int -> Bool
isMultiple n m =
n /= m && modBy m n == 0
in
List.range 2 last
|> List.foldl
(\current result ->
List.filter
(\elem -> not (isMultiple elem current))
result
)
numbers
 
|> List.filter (\el -> el >= min)
 
</syntaxhighlight>
{{out}}
<pre>
% elm repl
---- Elm 0.19.1 ----------------------------------------------------------------
Say :help for help and :exit to exit! More at <https://elm-lang.org/0.19.1/repl>
--------------------------------------------------------------------------------
> import Sieve exposing (sieve)
> a= sieve 1000200 1000600
[1000211,1000213,1000231,1000249,1000253,1000273,1000289,1000291,1000303,1000313,1000333,1000357,1000367,1000381,1000393,1000397,1000403,1000409,1000423,1000427,1000429,1000453,1000457,1000507,1000537,1000541,1000547,1000577,1000579,1000589]
: List Int
>
</pre>
 
=={{header|Emacs Lisp}}==
Anonymous user