Sieve of Eratosthenes: Difference between revisions

imported>Polarit
(new elm version)
imported>Polarit
Line 6,647:
Found 78498 primes to 1000000 in 192 milliseconds.</pre>
 
=={{header|=Elm with immutable arrays}}===
<syntaxhighlight lang="elm">
module PrimeArray exposing (main)
Line 6,654:
import Html exposing (div, h1, p, text)
import Html.Attributes exposing (style)
 
 
 
{-
The Eratosthenes sieve task in Rosetta Code does not allowaccept
the use of modulo function (allthough Elm functions modBy orand remainderBy work always correctly as they require type Int excluding type Float).
Thus the worksolution list is converted intoneeds an indexed work array
as Elm has no indexes for lists.
In this method we need no division remainder calculations,
Line 6,666 ⟶ 6,665:
We need the indexes that we know, where the marking of the non-primes
shall be set.
Because everything is immutable in Elm, every change of array values will create a new array save the original array unchanged. That makes the program running slower or consuming more space of memory than with non-functional imperative languages. All conventional loops (for, while, until) are excluded in Elm because immutability requirement.
 
Live: https://ellie-app.com/pTHJyqXcHtpa1
Line 6,722:
 
 
-{- As Elm has no loops (for, while, until)
-- we must use recursion instead!
-- The outer loopsearch of prime searchstarts withallways increasingsaving variablethe i
--first Thefound searchvalue of(not primesetting startszero) allwaysand saving (notcontinues setting the multiples of prime to zero).
Zero is no integer and may thus be used as marking of non-prime numbers. At the end, only the primes remain in the array and the zeroes are removed from the resulted array to be shown in Html.
-- the first location of the found prime as the later found values are
-}
-- multiples and set zero
-- The recursive loop of variable i follows:
 
-- The recursiverecursion loop ofincreasing variable i follows:
 
loopI : Int -> Int -> Array Int -> Array Int
Line 6,818:
Found 35 primes
</pre>
 
 
=={{header|Emacs Lisp}}==
Anonymous user