Til Death Due Us Part – Project Euler – Problem 3
Project Euler "is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems."
Lot's of programmers recently have been using Project Euler as a form of 'Code Kata:' a philosophy that believes that greatness comes through diligent practicing. Steve Yegge written on the subject as well as Kathy Sierra. The idea is that you can be insanely great at anything if you practice at it. Sure, some people are natural geniuses — and you will never be better then them. But if you want to, you can be in the top 1% of learned people on any particular subject.
I'm not sure if I believe them, but heck, i think that the idea inspires lots of mediocre programmers ( like myself ) to wake up each day and learn new stuff. And that in itself is worth something, right? I do believe that if you just set aside one hour per day to learn something new, you can become quite proficient in anything at all in a shorter period of time then you even imagined. Most people have a hard time with this though. They have a seeing that just one hour per day can lead to incredible results. I myself was in this category until I learned the 'Til Death Due Us Part' productivity method. On my Google homepage, I have the following widget on display:
The 'Countdown' widget shows me the (estimated) number of days I have until I die. Instead of brimming over with horrible sadness everyday, the huge number of days I have left actually motivates me. I just think about this simple fomula:
Number of Hours Per Day * Number of Days Til Death = Total Number of Hours
If I started today learning Ruby and practiced Ruby one hour per day, by the end of my life I will have practiced Ruby 17,193 hours. What are you NOT going to be good at after practicing for that long? With that in mind, you have no excuses. It is never too late for you to learn anything… well, provided you are not already 83 years old. Then I'd stick to shuffle board.
I've been teaching myself Ruby the last couple months and just recently started going through the problems on Project Euler. Problem 3 posed the following question:
The prime factors of 13195 are 5,7,13, and 29
What is the largest prime factor of the number 600851475143?
From wikipedia, "In number theory, the prime factors of a positive integer are the prime numbers that divide into that integer exactly, without leaving a remainder."
My solution to the problem in Ruby is below:
The first dilemma I faced while solving the problem was generating a list of prime numbers. I'm going to be honest, I didn't generate squat. I kind of cheated. I found a list of 10,000 prime number online. When the class PrimeFactorFinder is initialized, the list of numbers is opened, the prime numbers are "scanned" in using Regular Expressions and added to the array @PrimeNumbers. There is an way to actually generate prime numbers in Ruby I discovered. One can use the mathn standard ruby library as seen below:
require 'mathn' primes = Prime.new factor = primes.next
The next hurdle was finding the list of prime factors. This was found by modulo(ing) the numberToFactor by each prime number. If the former expression yields '0′ then the prime number divides into the numberToFactor exactly and is a 'Prime Factor.'
All in all a pretty simple exercise, yet it required me to use Ruby's Arrays, File IO, and Regular Expressions. Code Kata indeed.







