# # # # # # # # #
# /30/euler-007.rb
#
# by               Jan Lelis
# e-mail:          mail@janlelis.de
# type/version:    ruby 
# snippet url:     http://rbJL.net/30/euler-007.rb
# original post:   http://rbJL.net/30-project-euler-6-7-8-9-10-11-12-13-14-15-16-17-18-ruby
# license:         CC-BY-SA (DE)
#
# (c) 2010 Jan Lelis.

# http://projecteuler.net/index.php?section=problems&id=7
#  calculate the 10001st prime

# it is about a fixed size, so do not try to reach Infinity
MAX  = 111_111
FIND =  10_001

# very simple and inefficient prime test
class Integer
  def prime?
    (2..(self/3)).each{ |ele|
      return false if self%ele == 0
    }
    true
  end
end

# initialize possible primes to sieve them out
candidates = (3..MAX).step(2).to_a
count = 2

# take next prime and increase counter if it is a prime + sieve
ret = while candidate = candidates.shift
  if candidate.prime?
    candidates.keep_if{ |ele|
      ele % candidate != 0
    }

    if count == FIND
      break candidate
    else
      count += 1
    end
  end
end

puts ret