Skip to content

eratosthenes sieve #6

Open
Open
@asei-proxima

Description

@asei-proxima
def eratosthenes (n : Nat) : Array Bool := Id.run do
  let mut isPrime := Array.mkArray (n + 1) true

  isPrime := isPrime.set! 0 false
  isPrime := isPrime.set! 1 false

  for p in [2 : n + 1] do
    if not isPrime[p]! then
      continue

    let mut q := 2 * p
    while q <= n do
      isPrime := isPrime.set! q false 
      q := q + p

  return isPrime

def runTest (n : Nat) : IO Unit := do
  let l := eratosthenes n
  for i in [:l.size] do
    if l[i]! then
      IO.print s!"{i} "

#eval runTest 19

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions