All Euler problems
Project Euler

Power Digit Sum

Let S(m) denote the sum of the decimal digits of a positive integer m. Compute S(2^1000).

Source sync Apr 19, 2026
Problem #0016
Level Level 00
Solved By 248,047
Languages C++, Python
Answer 1366
Length 286 words
modular_arithmeticbrute_forcelinear_algebra

Problem Statement

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

\(2^{15} = 32768\) and the sum of its digits is \(3 + 2 + 7 + 6 + 8 = 26\).

What is the sum of the digits of the number \(2^{1000}\)?

Problem 16: Power Digit Sum

Mathematical Development

Definition 1. For a positive integer mm with base-10 representation m=k=0d1ak10km = \sum_{k=0}^{d-1} a_k \cdot 10^k, where 0ak90 \le a_k \le 9 and ad10a_{d-1} \ne 0, define the digit sum S(m)=k=0d1akS(m) = \sum_{k=0}^{d-1} a_k.

Definition 2. The digit length of a positive integer mm is d(m)=log10m+1d(m) = \lfloor \log_{10} m \rfloor + 1.

Theorem 1 (Digit length of 2n2^n). For all n0n \ge 0,

d(2n)=nlog102+1.d(2^n) = \lfloor n \log_{10} 2 \rfloor + 1.

Proof. By Definition 2, d(2n)=log10(2n)+1=nlog102+1d(2^n) = \lfloor \log_{10}(2^n) \rfloor + 1 = \lfloor n \log_{10} 2 \rfloor + 1, where the second equality follows from the logarithmic identity log10(2n)=nlog102\log_{10}(2^n) = n \log_{10} 2. \square

Corollary 1. d(21000)=10000.30102999+1=301.029+1=302d(2^{1000}) = \lfloor 1000 \cdot 0.30102999\ldots \rfloor + 1 = \lfloor 301.029\ldots \rfloor + 1 = 302.

Theorem 2 (Digit sum congruence). For every positive integer mm,

S(m)m(mod9).S(m) \equiv m \pmod{9}.

Proof. Write m=k=0d1ak10km = \sum_{k=0}^{d-1} a_k \cdot 10^k. Since 101(mod9)10 \equiv 1 \pmod{9}, we have 10k1k=1(mod9)10^k \equiv 1^k = 1 \pmod{9} for all k0k \ge 0. Therefore mk=0d1ak1=S(m)(mod9)m \equiv \sum_{k=0}^{d-1} a_k \cdot 1 = S(m) \pmod{9}. \square

Lemma 1 (Order of 2 modulo 9). The multiplicative order of 22 in Z/9Z\mathbb{Z}/9\mathbb{Z} is ord9(2)=6\mathrm{ord}_9(2) = 6.

Proof. We compute successive powers: 2122^1 \equiv 2, 2242^2 \equiv 4, 2382^3 \equiv 8, 2472^4 \equiv 7, 2552^5 \equiv 5, 261(mod9)2^6 \equiv 1 \pmod{9}. Since 2k≢1(mod9)2^k \not\equiv 1 \pmod{9} for 1k51 \le k \le 5 and 2612^6 \equiv 1, the order is exactly 66. \square

Proposition 1 (Residue of 210002^{1000} modulo 9). 210007(mod9)2^{1000} \equiv 7 \pmod{9}.

Proof. By Lemma 1, 2100021000mod6(mod9)2^{1000} \equiv 2^{1000 \bmod 6} \pmod{9}. Since 1000=1666+41000 = 166 \cdot 6 + 4, we have 2100024=167(mod9)2^{1000} \equiv 2^4 = 16 \equiv 7 \pmod{9}. \square

Theorem 3 (Bounds on S(2n)S(2^n)). For all n1n \ge 1,

1S(2n)9d(2n)=9(nlog102+1).1 \le S(2^n) \le 9 \cdot d(2^n) = 9\bigl(\lfloor n \log_{10} 2 \rfloor + 1\bigr).

Proof. The lower bound holds because 2n22^n \ge 2, so at least one digit is nonzero and S(2n)1S(2^n) \ge 1. The upper bound follows from ak9a_k \le 9 for each of the d(2n)d(2^n) digits. \square

Corollary 2. 1S(21000)9302=27181 \le S(2^{1000}) \le 9 \cdot 302 = 2718.

Verification. The computed answer S(21000)=1366S(2^{1000}) = 1366 satisfies 1366=1519+71366 = 151 \cdot 9 + 7, hence S(21000)7(mod9)S(2^{1000}) \equiv 7 \pmod{9}, consistent with Proposition 1 and Theorem 2.

Editorial

We compute bnb^n exactly and then sum its decimal digits. The reference pseudocode uses a digit array in base 10: each multiplication by bb propagates carries across the current digits, and the final digit array is summed. This is sufficient because repeated exact multiplication constructs the decimal expansion of bnb^n without losing information.

Pseudocode

Algorithm: Digit Sum of an Exact Power
Require: Integers b ≥ 2 and n ≥ 1.
Ensure: The sum of the decimal digits of b^n.
1: Represent the current power by a decimal digit array A initialized to the value 1.
2: Repeat n times:
3:     Multiply A by b and normalize carries across its decimal digits.
4: After the final multiplication, compute S ← the sum of the digits stored in A.
5: Return S.

Complexity Analysis

Proposition 2 (Time complexity). The algorithm runs in Θ(nd)\Theta(n \cdot d) time, where d=d(bn)=Θ(nlogb)d = d(b^n) = \Theta(n \log b).

Proof. The outer loop executes nn iterations. At iteration ii, the digit array has length d(bi)=ilog10b+1d(b^i) = \lfloor i \log_{10} b \rfloor + 1. The inner loop over digits at iteration ii performs Θ(d(bi))\Theta(d(b^i)) constant-time operations. The total work is

i=1nΘ(ilog10b)=Θ ⁣(log10bn(n+1)2)=Θ(n2logb).\sum_{i=1}^{n} \Theta(i \log_{10} b) = \Theta\!\left(\log_{10} b \cdot \frac{n(n+1)}{2}\right) = \Theta(n^2 \log b).

For b=2b = 2, this is Θ(n2)\Theta(n^2). \square

Proposition 3 (Space complexity). The algorithm uses Θ(d)=Θ(nlogb)\Theta(d) = \Theta(n \log b) space.

Proof. The digit array stores at most d(bn)=Θ(nlogb)d(b^n) = \Theta(n \log b) digits. No other data structure grows with nn. \square

Answer

1366\boxed{1366}

Code

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

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

int main() {
    vector<int> digits = {1};

    for (int i = 0; i < 1000; i++) {
        int carry = 0;
        for (int j = 0; j < (int)digits.size(); j++) {
            int val = digits[j] * 2 + carry;
            digits[j] = val % 10;
            carry = val / 10;
        }
        while (carry > 0) {
            digits.push_back(carry % 10);
            carry /= 10;
        }
    }

    int sum = 0;
    for (int d : digits) sum += d;
    cout << sum << endl;
    return 0;
}