Project Euler 14

I haven’t written any code in almost 2 weeks, but I did read a few pages of Eloquent Javascript (I highly recommend it, along with Javascript: The Good Parts).  I started Euler 14 on 7/30, felt a bit overwhelmed, wrote a rough sketch, and left it.  I felt inspired tonight (read: nothing else to do) so I revisited the problem.  The problem made so much more sense to me this time.  I guess the first time around I kept looking for a pattern so as not to brute force it, but this time I said, “Screw it,” and just brute forced it.  (I also re-read this comic)  It took just a few seconds for my computer to run the code.

When you’re approaching this problem, don’t start with 1,000,000.  Actually, when dealing with large numbers, never start with the large number you want to start with; start with a very small number, like 10, and make sure your code works.  Then, increase by a factor of 2 and make sure your code works again.  With these small numbers, you should be able to give a very rough guess of what the answer would be, and if it’s wildly off, desk check it.

Oh, and don’t do what I did.  I spent 15 minutes trying to figure out what was wrong with my code, only to realize I was outputting the wrong number.  Euler wants to know the first number of the Collatz sequence, but I was outputting the length of the Collatz sequence.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s