# Duncan's blog

## October 15, 2014

### Project Euler: problem 48 (PHP) – Self powers

Filed under: PHP,Project Euler — duncan @ 8:00 am

I previously blogged about this Project Euler puzzle nearly 6 years ago, using Python.  This is my approach using PHP as a simple practical exercise for myself, and I’d appreciate any feedback on my PHP code.

Problem 48:

The series, 11 + 22 + 33 + … + 1010 = 10405071317.

Find the last ten digits of the series, 11 + 22 + 33 + … + 10001000.

Code:

```<?php
\$limit = 1000;
\$sum = 0;

foreach (range(1, \$limit) as \$i) {
\$sum = bcadd(\$sum, bcpow(\$i, \$i));
}

echo substr(\$sum, -10);
```

Short question, short answer.  Using the BC Math functions due to the size of the numbers involved… not quite as fast to spit out the answer as it was with Python though.  Here using PHP’s substr() function which seems more useful than ColdFusion’s Mid() as by specifying a negative number it allows us to start 10 characters from the end of the string… and the third parameter is optional for the length required.