# Project Euler #44 in Ruby

My solution to this problem is definitely not the fastest. In fact, I’m certain it’s not. It takes about a few minutes to fully run. BUT more importantly it gives us the correct answer and it makes me feel a little better about myself for having done it. Here is the question text:

Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2. The first ten pentagonal numbers are: 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, … It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference, 70 − 22 = 48, is not pentagonal.Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference are pentagonal and D = |Pk − Pj| is minimised; what is the value of D?

## Solution:

Here’s my solution. Basically I wrote three functions: one which generates a list of pentagonal numbers up to a limit, one to check if the sum of any two numbers is pentagonal, and one to check if the absolute value of the difference between any two is pentagonal. Their powers unite to generate this:

It’s so slow because it’s running through the entire list each time. I’m sure there are some simple ways to improve the speed of the code but for now, I’m happy to just have it done.