An Unreasonably Deep Dive Into Project Euler Problem 4

August 16, 2019

Or: How to make your solution 8000x faster with math and short-circuit evaluation.

Introduction

It has been a couple of years since my last Project Eueler effort, An Unreasonably Deep Dive into Project Euler Problem 3, and since I’ve also been wanting to do more work with Rust recently, I thought it would be a good opportunity to do both things at once by doing Project Euler problem 4 in Rust instead of my default of Go as before.

Problem Statement

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.

The general approach to the solution will be to find a working version first (as always), then focus on making it faster by reducing the search space. Along the way, there are a couple of programming techniques we can employ for great speedups as well.

All of this results in the final version being about 8000x faster than the naive version.

Helper function(s)

To start with, we will need a function that tells us if a number is palindromic. This is not the most efficient way to write this helper function, but it works.

// We will need a helper function to determine if a number is palindromic
//
// is_palindromic_v1() will convert the number to a string, reverse the string, and compare that with the
// string representation of the number.
fnis_palindromic_v1(i: i32)-> bool {returni.to_string().chars().rev().collect::<String>()==i.to_string();}

v0() - time required: ~102,000,000ns

As a naive/brute-force first version, let’s just multiply all three-digit numbers, check each time if the product is palindromic, keep it if it is, and continue that process until we have checked all products of three-digit numbers.

This is slow but it works.

// v0() is the naive attempt: multiply three-digit numbers. When a palindromic number is found,
// if it's larger than the most recent palindromic number found then keep it. Iterate until the end 999.
fnv0()-> i32 {letmutres=0;foriin100..=999{forjin100..=999{letprod=i*j;ifis_palindromic_v1(prod)&&prod>res{res=prod;}}}returnres;}

v1() - time required: 7,300,000ns (~14x speedup from v0())

This isn’t really an algorithm change, but it is a slight change in the code that makes a massive performance difference. We are taking advantage of the Short-circuit evaluation present in Rust. This means that for a given sequence of arguments chained together with certain operators, like &&, the evaluation will take place from left to right and will terminate as soon as possible. This prevents the remaining elements in the sequence from being evaluated unnecessarily. In our case, that makes a big difference since we are iterating through many numbers and therefore would be executing is_palindromic_v1() many times. Since prod > res has a much lower computational cost than is_palindromic_v1(prod) and since prod > res happens sufficiently frequently, this cuts down runtime by a factor of 14.

// v1(): FUN FACT - Rust has short-circuit evaluation.
fnv1()-> i32 {letmutres=0;foriin100..=999{forjin100..=999{letprod=i*j;// Note that this order changed.
// Consequently, the right-hand side of the && is only evaluated if needed.
// This is important since string casting and reversing is so slow and the search
// space is so large. Many languages have short-circuit/lazy evaluation.
// This matters less as we start restricting the search space.
ifprod>res&&is_palindromic_v1(prod){res=prod;}}}returnres;}

v2() - time required: ~1,800,000ns (~57x speedup from v0())

If you think about it, starting at the lower end of the range and iterating all the way to the higher end does not make very much sense when looking for the largest product. The chances that the largest product will come from the lower end of the range are very small, so we should start the iteration from the large numbers and work our way down instead. This results in a much smaller search space since we break out as soon as we find the large palindromic number.

// v2(): Start from the biggest numbers, which makes a lot more sense.
fnv2()-> i32 {letmutres=0;foriin(100..=999).rev(){forjin(100..=999).rev(){letprod=i*j;ifprod>res&&is_palindromic_v1(prod){res=prod;break;}}}returnres;}

v3() - time required: ~1,050,000ns (~97x speedup from v0())

Now we start to take advantage of some convenient math. Since we are multiplying two numbers, and multiplication is a commutative binary operation, we know that we do not need to check p * q and q * p since they are the same. So if we get to a point in the evaluation where our product is less than the result we would currently return then we can also skip checking any smaller numbers that would come after that on the inner loop. This additional restriction of the search space gives us even more speedup.

// v3(): Now stop if result is greater than current product. If you
// are multiplying and the product is smaller than the current number, you won't be
// able to get around the fact that p * q = q * p, so you can stop.
fnv3()-> i32 {letmutres=0;foriin(100..=999).rev(){forjin(100..=999).rev(){letprod=i*j;ifprod>res&&is_palindromic_v1(prod){res=prod;break;}elseifres>prod{break;}}}returnres;}

v4() - time required: ~195,000ns (~517x speedup from v0())

Now we can reduce search space a little more by examining the general shape of our palindromic number in question, and doing some factoring. This leads us to the conclusion that we don’t need to check every factor, but only cases where one of the factors is divisible by 11. For details, see the code comment below.

// v4(): Note that the number is probably 6 digits, i.e. abccba = p * q where each letter is some number between 1 and 9
// then we can rewrite as a * 100000 + b * 10000 + c * 1000 + c * 100 + b * 10 + a * 1 = p * q
// which we can rewrite as a * 100001 + b * 10010 + c * 1100 = p * q
// which can be factored to 11 * (a * 9091 + b * 910 + c * 100) = p * q
// therefore p * q must be divisible by 11
// therefore either p or q must be divisible by 11.
//
// Since p or q must be divisible by 11, just start the iteration at 990 because it is
// the largest possible number in the given range that is also divisible by 11 and
// work our way down in steps of 11.
fnv4()-> i32 {letmutres=0;foriin(100..=990).rev().step_by(11){forjin(100..=999).rev(){letprod=i*j;ifprod>res&&is_palindromic_v1(prod){res=prod;break;}elseifres>prod{break;}}}returnres;}

Aside: finding palindromic integers

It has to cast our integer to a string, reverse that string to make a new string, and then compare that to another string-casted version of our original integer.

Fortunately, there is a much faster way to determine if an integer is palindromic. We can just continually divide the original number by 10 and append the remainder on the right side of a new number each time to construct the reverse of the original number. If the new number and the original number match, then the number is palindromic.

This version is a ~16x speedup over the version that uses string reversal and comparison.

// is_palindromic_v2() will build up a new number `reverse` by shifting `reverse` one decimal place
// and adding the 10s place of the candidate number until no places are left in the candidate
// number. In this way, `reverse` is constructed as the sequence of successive tens place numbers
// from the original candidate number. This version is ~16x faster than is_palindromic_v1().
fnis_palindromic_v2(i: i32)-> bool {letmutreversed=0;letmutnumber=i;whilenumber>0{reversed=reversed*10+number%10;number=number/10;}returnreversed==i||i==number/10;}

v5() - time required: ~13,000ns (~8000x speedup from v0())

This version loops 1627 times, a ~500x reduction from v0() and v1(). The ~500x overall reduction in search space, the short-circuit evaluation change, plus the ~16x reduction due to the improved method of palindromic number checking is what gets us to the ~8000x reduction relative to the initial version.

// v5(): Get rid of the slow string reversing and use the numerical palindrome helper function.
fnv5()-> i32 {letmutres=0;foriin(100..=990).rev().step_by(11){forjin(100..=999).rev(){letprod=i*j;ifprod>res&&is_palindromic_v2(prod){res=prod;break;}elseifres>prod{break;}}}returnres;}

Summary

While there are still some performance gains likely to be had, they would probably be nominal and require extensive effort. By simply pursuing our general problem solving strategy of reducing the search space, combined with a couple of domain knowledge improvements in the form of taking advantage of short-circuit evaluation and a different helper function to check if a number is palindromic, we were able to get an approximately 8000x speedup over the original naive version.

Adam Drake leads technical business transformations in global and multi-cultural environments. He has a passion for helping companies become more productive by improving internal leadership capabilities, and accelerating product development through technology and data architecture guidance. Adam is currently a White House Presidential Innovation Fellow and IEEE Senior Member.