Project Euler Solution 48: Self powers

Project Euler Problem 48: Self powers is quite fun because one can finally only work with lower digits.

The series, $1^1 + 2^2 + 3^3 + \ldots + 10^{10} = 10,405,071,317$.

Find the last ten digits of the series, $1^1 + 2^2 + 3^3 + \ldots + 1000^{1000}$.

One can directly compute this in Python using the arbitrary large integers:

def solution_big_integers() -> int:
    return sum(number**number for number in range(1, 1001)) % 10**10

This finishes in 11 s.

If we didn't have these integers, we would need to truncate them after every operation to make sure that they don't overflow. This way we could use them with 64 bit unsigned integers, for instance.

def solution_remainder() -> int:
    cutoff = 10**10
    result = 0
    for number in range(1, 1001):
        summand = 1
        for _ in range(number):
            summand = (summand * number) % cutoff
        result = (result + summand) % cutoff
    return result

This takes 62 ms to run, so Python can more easily run with the big integers than with the modulo construct. Well, this was pretty easy then.