Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eratosthenes sieve #6

Open
asei-proxima opened this issue Mar 28, 2024 · 0 comments
Open

eratosthenes sieve #6

asei-proxima opened this issue Mar 28, 2024 · 0 comments

Comments

@asei-proxima
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant