// you’re reading...

Project Euler Solutions

Project Euler Problem 48 Solution

1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 3.00 out of 5)
Loading ... Loading ...

Problem Description

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

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

Analysis

Without Python’s built-in pow() function and native large integer support this may have been more of a challenge. In fact, before revisiting this problem we solved it in Perl with the following code.

$max_n = 1000;
$m = 1e10;
$sum = 0;
 
for $n (1..$max_n) { 
	$nx = 1; 
	$nx = ($nx * $n) % $m  for (1..$n); 
	$sum = ($sum + $nx) % $m; 
}
 
print "PE 48 = $sum";

Solution

Runs < 1 second in Python.

def PE48(limit, m):
    sum = 0
    while limit:
        sum += pow(limit, limit, 10**m)
        limit -= 1
    return sum % 10**m
 
print "Answer to PE48 = ",PE48(1000, 10)

Comments

Note: (a*a*a) % n = ( (a%n) * (a%n) * (a%n) ) % n

  • See also problem 97 & problem 188.
  • Don’t forget that leading zeros are not printed.
  • The last iteration, 1000, would not affect the last 10 digits of the sum, but was included to adhere to the problem description.
  • The full answer is 3001 digits.

Discussion

2 comments for “Project Euler Problem 48 Solution”

  1. [...] also problem 48 & problem [...]

    Posted by Dreamshire | Project Euler Problem 97 Solution | April 27, 2009, 1:13 am
  2. [...] problem 48 & problem [...]

    Posted by Dreamshire | Project Euler Problem 188 Solution | April 27, 2009, 1:18 am

Post a comment