Wednesday, September 28, 2011

Project Euler - Problem Three

Let this be a lesson in why reinventing the wheel isn't always best.

I'll start with the problem:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

Now, that isn't too hard.  In fact, I had code that would do it fairly quickly. My downfall, however, was that I wasn't looking for any sort of help. I'd decided that I wanted to do it completely and all by myself. That's an honorable pursuit, and one that everyone should do at some point. Always relying on other people for help gets old.

There comes a time when you've got to realize that asking for help, or at least advice, isn't always bad though. My program ran so slow. It was rather clumsy and dumb, to be quite honest. I really didn't like it at all. So I started googling. I ended up finding a website that had complete and working C++ (and Java) code for running the Sieve of Eratosthenes. I picked the code up off of here, and it runs at least a bajillion times better than mine.

Now, is it mine?  Not even a little bit. The most I put into my problem three solution was really a few lines to print out a menu, and that isn't even necessary. Was it something worth doing though?  Yeah, it definitely was. Now, I know a whole new way to do prime numbers.

And trust me. It's cool. Read the link and give it a shot.

No comments:

Post a Comment