Lucky Ticket

While on the bus in Russia, they give you a ticket with a 6 digit number.

The Russian I was with closely read the numbers, and I asked “Why do you care about the number?”

She said, “If the first three numbers equal the last three numbers, it’s a ‘Happy Ticket’ and I keep it.”

I tried to figure the odds on getting a “Happy Ticket” and just gave up.

I’m a programmer, not a mathematician.

So I wrote a Ruby script to iterate thru the numbers and do a comparison of the first three and the last three.

#!/usr/bin/env ruby
i = 0
happy = 0
theCount = Array.new
while i < 999999
i += 1
f = sprintf '%06d', i # f is a six digit number

theCount = f.chars.to_a

first = theCount.fetch(0).to_i + theCount.fetch(1).to_i + theCount.fetch(2).to_i
last = theCount.fetch(3).to_i + theCount.fetch(4).to_i + theCount.fetch(5).to_i

     if first == last
          happy += 1
          print("Happy Number ", happy," is ", theCount[0..2], theCount[3..5], "\n") 
      end

end

The end result is that there are 55,252 ‘Happy Numbers’, meaning your odds of getting one is about 5.5%

Author: heinrich

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.