mgroves

Project Euler update

I've finished some more Project Euler problems. Here's some notes:

Problem 3. So, I finally had to break down and write some unit tests for this one. I also had to brush up on what 'prime factors' are, because it's not exactly something I use every day. My solution involved recursion: find the lowest number that divides evenly, divide by it, and repeat using the dividend. If that number is ever itself, it's prime, which is an ending condition for the recursion.

At this point, I also refactored the structure of the project code. Each problem will still be in its own class, but each of those classes implements a common interface. This way, I can simply loop through all solved problems and output the answer without having to repeat formatting and stuff like that.

Problem 4. This one was fun, because it involves palindromes! I again used unit tests, because I knew I would have to watch out for off-by-one errors. I also created a string extension method to reverse the string. I could have created a "IsPalindrome" extension, but generally speaking, the use of such a method is pretty narrow, and thus doesn't really fit as an extension method. My first approach to this problem was to gradually reduce two integers from 999 on down until I got a palindrome from the product. This turned out to not work as expected, and I ended up with a brute force O(n2) algorithm instead. It runs pretty fast, but if the problem called for 4,5,6,etc digit numbers, it would start to get really slow.

As before, you can check out my ongoing source code on CodePlex.com.

2 Responses to Project Euler update Article comments Feed

1

JMitchem

Unrelated, but how come you're using mbUnit here, and nUnit at work? Any particular preference or just trying it out?

Also, it's interesting that [Microsoft's] Codeplex supports SVN. I wonder how they got it to support both SVN and TFS though. Can you use both to access the same repository?

Posted at January 18, 2010 on 9:47am

2

mgroves of http://mgroves.com/

No particular reason. The first TDD intro I went to years ago used MbUnit, and I've just not gotten around to installing nunit. I know there are some relatively minor differences/features, but I really don't have a preference yet.

Also, I don't have VSTS at home, so I know nothing about TFS with CodePlex, and I really have no interest in finding out, so long as CodePlex supports SVN.

Posted at January 18, 2010 on 1:05pm

Write a response

Play nice. Be constructive. Allowed HTML tags are: <a>, <strong>, <em> and <blockquote>

« Web form validation,… Turning an NAS drive… »