Ragged Clown

It's just a shadow you're seeing that he's chasing…


Jun
21
2008

Forgetful me

According to Kurzweil, the singularity (the moment when we will start to invent things instantaneously) will occur in 2045. According to me the singularity (the moment when I forget things fast than I can learn things) occurs in 2009.

Every time I start over with Ruby (or XSLT or …) I find that I have forgotten the most basic things (like how to construct an object).

Anyway, thanks to Project Euler (according to which, I am 4% genius), I had an excuse to go go back and learn Ruby all over again.

Here’s my prime number generator (which is about a third of the size of my Java version):


class Primes
  def initialize
    @primes = []
    @next_candidate = 2
  end

  def prime? number
    root = Math.sqrt number
    find_primes_less_than root

    @primes.each do |prime|
      return true if prime > root
      return false if number % prime == 0
    end
  end

  def find_primes_less_than limit
    until @next_candidate > limit
      @primes << @next_candidate if prime? @next_candidate
      @next_candidate += 1
    end
  end

  def [] index
    until @primes.size > index
      find_primes_less_than @next_candidate + 100
    end
    return @primes[index]
  end
end

The answer to problem #7 is @primes[10000], in case you were wondering.