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] :=
{
// Make an array containing the numbers from 0 to n.
array = array[0 to n]
array@1 = 0 // Don't consider 1 prime.
for i = 2 to ceil[sqrt[n]]
if array@i != 0
for j = i^2 to n step i
array@j = 0
// Return the remaining elements where the element is not zero.
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, eliasen@mindspring.com