NOTE: This is an archive of my old blog. Go to http://gonium.net for my current website.

Code Kata: Project Euler #4, Finding Palindromes

Posted by md on April 19, 2008

This weekend, I solved problem 4 of Project Euler:
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers.
I designed a routine that checks whether a given number is a palindrome. This routine is then used inside two loops iterating from 999 downto 0. The first hit is the largest palindrome. The core functionality is here: bool Problem4::isPalindrome(long number) { bool retval = false; std::vector digits; while (number > 0) { long lastdigit = number % 10; digits.push_back(lastdigit); number = number / 10; } reverse(digits.begin(), digits.end()); std::vector::iterator front = digits.begin(); std::vector::iterator back = –digits.end(); retval = false; while (! (front >= back)) { if ((*front) == (*back)) { retval = true; // move iterators to the next digits front++; back–; } else { // cannot be a palindrome, back out retval = false; break; } } return retval; } This implementation reverses the vector. By using the front and back operators in the other direction, this could be left out. But it serves its purpose for now. You can get the full sourcecode from the download page. Please note that the code in the tarball calculates *all* palindromes smaller than 1000000. The picture was CCed on flickr by TisseurDeToile -[mangerait bien un petit chat]-.

Code Kata: Project Euler #3 + CMake distributions

Posted by md on April 13, 2008

11 is a prime This weekend, I tackled the Project Euler problem #3:
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?
Pretty straightforward: Just test and recurse. Continue reading...

New Vim config voodoo

Posted by md on April 11, 2008

Voodoo

I just added some new features to my vim configuration – check out these extensions:

So, go and install some voodoo. Yum. Picture CC’ed by chaceofspades on flickr, thanks!