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.

### Like this:

Like Loading...

*Related*