There is an algorithm called Sieve of Eratosthenes for finding prime number upto n
number. Asymptotic complexity is O(nlog(logn)).
The pseudo code is something like:
- Create an array from 0..max
- Starting at 2, delete every multiple of 2 from the array.
- Then, go back to the beginning, and delete every multiple of 3.
- Repeat this starting from the next available number at the beginning of the array.
- Do this until the square of number you are checking is greater than your max number.
- Finally, compact the original array.
This array will then contain only the prime numbers up to your max number. You'll find that it's really, really efficient. So efficient that you can use it as a helper method to determine whether or not a number is prime. Want to know if the number 105557 is prime? Only takes 66 steps.
Ruby Code:
def sieve(max)
# Set up an array with all the numbers from 0 to the max
primes = (0..max).to_a
# Set both the first and second positions (i.e., 0 and 1) to nil, as they
# aren't prime.
primes[0] = primes[1] = nil
# Iterate through primes array
counter = 0
primes.each do |p|
# Skip if nil
next unless p
# Break if we are past the square root of the max value
break if p*p > max
counter += 1
# Start at the square of the current number, and step through.
# Go up to the max value, by multiples of the current number, and replace
# that value with nil in the primes array
(p*p).step(max,p) { |m| primes[m] = nil }
end
# Finally, return the compacted array.
puts "Solved for #{max} in #{counter} steps."
primes.compact
end
To check whether a number is prime or not:
def prime?(num)
sieve(num).include?(num)
end