taking the log of $a^b$ (Project Euler problem 29)

705 Views Asked by At

I've been stuck on Project Euler problem 29 and thus asked a friend who solved it how to do it.

What he basically did was for each power was: $\left(\frac{\log_{10}(a)}{\log_{10}(2)}\right)\cdot b$ and adding this to a set (as sets' values are distinct the duplicates are automatically removed).

Now he could not remember why this formula works, I've tried to find it out myself but I'm not that good in maths.. If you remove the devision of $\log_{10}(2)$ the answer is also incorrect.

Can someone explain me why this formule works, how it works and why you must include the dividing by $\log_{10}(2)$.

Cheers

1

There are 1 best solutions below

2
On

You have $\log_{10} a^b=b \cdot \log_{10} a$. Your friend's expression is just dividing this by the constant $\log_{10} 2$, which is not needed.

It is better to do integer problems with integers. As the logs are not exact, you can get fooled when testing for equality of floating point numbers. You can overcome this by analyzing how different the logs of different answers can be, making sure the logs are accurate enough, and checking for almost equality of the floats.