Problem 12

Posted on June 22nd, 2008

To save you going all the way to Project Euler to read it, I have copied problem 12 here for your puzzle solving convenience…

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28.

The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, …

Let us list the factors of the first seven triangle numbers:

1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28

We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?

In case you were wondering, the answer to problem 10 is


primes = Primes.new
puts primes.find_primes_less_than(2000000).inject{|s,n| s+n}

How come inject and collect haven’t caught on in other languages? They are awesome.

Forgetful me

Posted on June 21st, 2008

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.

Stuck on Stage 6

Posted on April 6th, 2008

I Stumbled Upon a cool site for kids at the BBC. They have a whole bunch of games but I am playing this one: Questionaut Key Stage 2.

It’s a bit like the Python Challenge for little kids.

I am stuck on Stage 6 and was feeling silly because the quiz had been pretty easy up to that point. I tried to google the answer but it turns out that a lot of people are stuck on stage 3 :-)

I decided to stay stuck for a while and not look at the answer. Maybe my son will come to my rescue?

There are more puzzles there. Some easier, some harder. Fantastic site.