sieve.frink

Download or view sieve.frink in plain text format


// Sieve of Eratosthenes

n = eval[input["Enter highest number: "]]
results = array[sieve[n]]
println[results]
println[length[results] + " prime numbers less than or equal to " + n]

sieve[n] :=
{
   // Initialize array
   array = array[0 to n]
   array@1 = 0

   for i = 2 to ceil[sqrt[n]]
      if array@i != 0
         for j = i^2 to n step i
            array@j = 0

   return select[array, { |x| x != 0 }]
}


Download or view sieve.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 20218 days, 0 hours, 13 minutes ago.