What am I missing here, the Self powers problem on Project euler

Viewed 75

Question: The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317. Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000.

My code was

sum=0
for x in range(1001):
    k=x**x
    sum=sum+k
print(sum)

Yet the answer was wrong. What am I missing?

1 Answers

Running your program gives a huge number. You need to return the last 10 digits so replace print(sum) with print(sum%10**10).

Also the problem starts from 1 but you are starting from 0. replace the loop with range(1, 1001) since 0^0=1 so your answer will be off by one.

Related