All Euler problems
Project Euler

Self Powers

The series 1^1 + 2^2 + 3^3 +... + 10^10 = 10,405,071,317. Find the last ten digits of the series 1^1 + 2^2 + 3^3 +... + 1000^1000.

Source sync Apr 19, 2026
Problem #0048
Level Level 00
Solved By 122,029
Languages C++, Python
Answer 9110846700
Length 361 words
modular_arithmeticbrute_forcesequence

Problem Statement

This archive keeps the full statement, math, and original media on the page.

The series, \(1^1 + 2^2 + 3^3 + \cdots + 10^{10} = 10405071317\).

Find the last ten digits of the series, \(1^1 + 2^2 + 3^3 + \cdots + 1000^{1000}\).

Problem 48: Self Powers

Mathematical Development

Formal Development

Definition 1. The self-power sum of order NN is SN:=k=1NkkS_N := \sum_{k=1}^{N} k^k. We seek S1000mod1010S_{1000} \bmod 10^{10}.

Theorem 1 (Compatibility of modular reduction with ring operations). Let m>0m > 0 be an integer. The map ϕ:ZZ/mZ\phi : \mathbb{Z} \to \mathbb{Z}/m\mathbb{Z} defined by ϕ(a)=amodm\phi(a) = a \bmod m is a ring homomorphism. In particular, for all a,bZa, b \in \mathbb{Z}:

  1. (a+b)modm=((amodm)+(bmodm))modm(a + b) \bmod m = ((a \bmod m) + (b \bmod m)) \bmod m,
  2. (ab)modm=((amodm)(bmodm))modm(a \cdot b) \bmod m = ((a \bmod m) \cdot (b \bmod m)) \bmod m.

Proof. Write a=q1m+r1a = q_1 m + r_1, b=q2m+r2b = q_2 m + r_2 with ri=aimodmr_i = a_i \bmod m.

(1) a+b=(q1+q2)m+(r1+r2)a + b = (q_1 + q_2)m + (r_1 + r_2), so (a+b)modm=(r1+r2)modm(a+b) \bmod m = (r_1 + r_2) \bmod m.

(2) ab=(q1m+r1)(q2m+r2)=m(q1q2m+q1r2+q2r1)+r1r2ab = (q_1 m + r_1)(q_2 m + r_2) = m(q_1 q_2 m + q_1 r_2 + q_2 r_1) + r_1 r_2, so (ab)modm=(r1r2)modm(ab) \bmod m = (r_1 r_2) \bmod m. \square

Corollary 1. SNmodm=(k=1N(kkmodm))modmS_N \bmod m = \left(\sum_{k=1}^{N} (k^k \bmod m)\right) \bmod m.

Proof. Apply the additive property of Theorem 1 inductively over NN summands. \square

Theorem 2 (Binary exponentiation). For integers aa, b0b \geq 0, and modulus m>1m > 1, the value abmodma^b \bmod m can be computed in O(logb)O(\log b) modular multiplications.

Proof. Write bb in binary: b=i=0Lbi2ib = \sum_{i=0}^{L} b_i \cdot 2^i where L=log2bL = \lfloor \log_2 b \rfloor and bi{0,1}b_i \in \{0, 1\}. Then:

ab=i=0Labi2i=i:bi=1a2i.a^b = \prod_{i=0}^{L} a^{b_i \cdot 2^i} = \prod_{i:\, b_i = 1} a^{2^i}.

The sequence a20modm,a21modm,,a2Lmodma^{2^0} \bmod m, \, a^{2^1} \bmod m, \, \ldots, \, a^{2^L} \bmod m is computed by repeated squaring:

a2i+1(a2i)2(modm),a^{2^{i+1}} \equiv \left(a^{2^i}\right)^2 \pmod{m},

requiring one modular multiplication per step (LL total). An accumulator is initialized to 11 and multiplied by a2imodma^{2^i} \bmod m whenever bi=1b_i = 1 (at most L+1L + 1 multiplications). The total number of modular multiplications is at most 2L+1=O(logb)2L + 1 = O(\log b).

When mm fits in a machine word, each modular multiplication is O(1)O(1), giving O(logb)O(\log b) overall time. \square

Lemma 1 (Last ten digits). The last ten digits of a non-negative integer NN are given by Nmod1010N \bmod 10^{10}.

Proof. In decimal representation, N=q1010+rN = q \cdot 10^{10} + r with 0r<10100 \leq r < 10^{10}. The remainder rr consists of the last ten digits (padded with leading zeros if r<109r < 10^9). \square

Theorem 3. The last ten digits of S1000=k=11000kkS_{1000} = \sum_{k=1}^{1000} k^k are 91108467009110846700.

Proof. Set m=1010m = 10^{10}. By Corollary 1:

S1000modm=(k=11000(kkmodm))modm.S_{1000} \bmod m = \left(\sum_{k=1}^{1000} (k^k \bmod m)\right) \bmod m.

Each term kkmodmk^k \bmod m is computed via binary exponentiation (Theorem 2) using O(logk)O(\log k) modular multiplications. Since m=1010<234m = 10^{10} < 2^{34}, all intermediate products fit in 64-bit integers, so each multiplication is O(1)O(1).

The computation yields S1000mod1010=9110846700S_{1000} \bmod 10^{10} = 9110846700.

Verification. For N=10N = 10: S10=10,405,071,317S_{10} = 10{,}405{,}071{,}317, whose last 10 digits are 04050713170405071317. Our modular computation agrees: k=110(kkmod1010)mod1010=0405071317\sum_{k=1}^{10} (k^k \bmod 10^{10}) \bmod 10^{10} = 0405071317. \square

Editorial

We carry out the entire computation modulo 101010^{10}, since only the last ten digits are required. For each kk from 11 to 10001000, the term kkmod1010k^k \bmod 10^{10} is computed by modular exponentiation and added to an accumulator that is itself reduced modulo 101010^{10} after every step. The final residue is exactly the desired suffix of the full sum.

Pseudocode

Algorithm: Last Ten Digits of the Self-power Sum
Require: The modulus M ← 10^10.
Ensure: The final ten digits of ∑_{k=1}^1000 k^k.
1: Initialize total ← 0.
2: For each k in {1, 2, ..., 1000}, compute t ← k^k mod M by modular exponentiation.
3: Update total ← (total + t) mod M.
4: Return total.

Complexity Analysis

Proposition (Time complexity). The algorithm runs in O(NlogN)O(N \log N) time, where N=1000N = 1000.

Proof. The outer loop executes NN iterations. In iteration kk, the call to MODPOW performs O(logk)O(\log k) modular multiplications, each costing O(1)O(1) (since m=1010m = 10^{10} fits in 64 bits). The total work is:

k=1NO(logk)=O ⁣(k=1Nlogk)=O(NlogN).\sum_{k=1}^{N} O(\log k) = O\!\left(\sum_{k=1}^{N} \log k\right) = O(N \log N).

For N=1000N = 1000, this is approximately 10,00010{,}000 operations. \square

Proposition (Space complexity). The algorithm uses O(1)O(1) auxiliary space.

Proof. Only the accumulator total and the local variables of MODPOW (result, base, exp) are maintained. No arrays or dynamic data structures are required. \square

Answer

9110846700\boxed{9110846700}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_048/solution.cpp
#include <bits/stdc++.h>
using namespace std;

long long modpow(long long base, long long exp, long long mod) {
    long long result = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1)
            result = ((__int128)result * base) % mod;
        base = ((__int128)base * base) % mod;
        exp >>= 1;
    }
    return result;
}

int main() {
    const long long MOD = 10000000000LL;
    long long answer = 0;
    for (int k = 1; k <= 1000; k++)
        answer = (answer + modpow(k, k, MOD)) % MOD;

    printf("%010lld\n", answer);
    return 0;
}