Finding Numbers for Which the Sum of the Squares of the Digits Is a Perfect Square
For a positive integer n, let f(n) denote the sum of the squares of the digits of n in base 10. Find the last nine digits of sum_(0 < n < 10^(20 f(n) is a perfect square)) n.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
For a positive integer \(n\), let \(f(n)\) be the sum of the squares of the digits (in base \(10\)) of \(n\), e.g. \begin {align*} f(3) &= 3^2 = 9,\\ f(25) &= 2^2 + 5^2 = 4 + 25 = 29,\\ f(442) &= 4^2 + 4^2 + 2^2 = 16 + 16 + 4 = 36\\ \end {align*}
Find the last nine digits of the sum of all \(n\), \(0 \lt n \lt 10^{20}\), such that \(f(n)\) is a perfect square.
Problem 171: Finding Numbers for Which the Sum of the Squares of the Digits Is a Perfect Square
Mathematical Development
Definition 1. For with base-10 digits , define .
Theorem 1 (Digit-square-sum bound). For any non-negative integer representable with at most decimal digits, . Consequently, the set of attainable perfect-square values of is .
Proof. Each digit satisfies . Summing over digits yields . The perfect squares in are exactly , giving the claimed set.
Corollary 1. For , we have and . The target set of perfect-square sums is .
Lemma 1 (Digit-append identity). Let be any set of non-negative integers and let . Then
Proof. By linearity of finite sums:
Theorem 2 (Digit DP correctness). Define the following quantities for and :
- := the number of -character strings over whose digit-square-sum equals .
- := the sum, modulo , of the numerical values of all such strings.
Initialize and , with all other entries zero. The recurrence for appending digit is:
Then the answer to the problem is
Proof. We represent every integer uniquely as a 20-character zero-padded decimal string. The DP constructs these strings one character at a time, left to right. At layer , counts strings of length with digit-square-sum , and holds their aggregate numerical value modulo .
The correctness of the transition for follows from Lemma 1: when we extend every string in by appending digit , the new numerical value of each extended string is , and the new digit-square-sum is . The sum over of these new values is , as required.
After processing all positions, accumulates the sum of all 20-character strings with digit-square-sum . Summing over yields the desired total modulo .
Finally, the string representing has , which is a perfect square (). However, since contributes to the sum, the exclusion in the problem statement requires no correction.
Editorial
The key observation is that the condition depends only on the sum of squared digits, not on the exact arrangement of those digits. That makes a digit DP natural: for each processed prefix we only need to know how many strings lead to a given square-sum total, and what the sum of the corresponding numeric values is modulo .
Appending one digit updates both pieces of information at once. The count table tells us how many prefixes reach a given sum, while the total table uses the digit-append identity to update the accumulated numeric sum when a new digit is attached on the right. After 20 positions, we simply collect the states whose digit-square-sum is one of the attainable perfect squares up to 1600.
Pseudocode
Let the state be the current digit-square-sum.
Store for each state:
how many digit strings reach it,
and the sum of those digit strings modulo $10^9$.
Start with the empty string: count 1 at square-sum 0, total 0.
Repeat 20 times:
create fresh count and total arrays.
For every reachable square-sum:
try appending each digit from 0 to 9.
Move to the new square-sum by adding the digit square.
Add the old count into the new count array.
Update the new total array with
ten times the previous total plus the appended digit times the previous count.
After all 20 digits are processed, sum the total-array entries
whose indices are perfect squares from $1^2$ through $40^2$.
Return the result modulo $10^9$.
Complexity Analysis
- Time: The triple loop executes iterations in the worst case. With and , this gives operations. Hence .
- Space: Two arrays of size suffice (rolling-array technique), giving .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
/*
* Problem 171: Last 9 digits of sum of all n in (0, 10^20) with
* f(n) = sum of squared digits being a perfect square.
*
* Digit DP: track (count, value_sum) keyed by digit-square-sum.
*/
const long long MOD = 1000000000LL;
const int D = 20;
const int SMAX = D * 81; // 1620
vector<long long> cnt(SMAX + 1, 0), tot(SMAX + 1, 0);
cnt[0] = 1;
for (int k = 0; k < D; k++) {
vector<long long> nc(SMAX + 1, 0), nt(SMAX + 1, 0);
for (int s = 0; s <= SMAX; s++) {
if (cnt[s] == 0) continue;
for (int d = 0; d <= 9; d++) {
int ns = s + d * d;
if (ns > SMAX) break;
nc[ns] = (nc[ns] + cnt[s]) % MOD;
nt[ns] = (nt[ns] + 10 * tot[s] % MOD + (long long)d * (cnt[s] % MOD)) % MOD;
}
}
cnt = nc;
tot = nt;
}
long long ans = 0;
for (int j = 1; j * j <= SMAX; j++)
ans = (ans + tot[j * j]) % MOD;
cout << ans << endl;
return 0;
}
"""
Problem 171: Finding Numbers for Which the Sum of the Squares of the Digits
Is a Perfect Square
Find the last nine digits of the sum of all n, 0 < n < 10^20, such that
f(n) = sum of squares of digits of n is a perfect square.
Method: Digit DP tracking (count, value_sum) keyed by digit-square-sum.
"""
def solve():
MOD = 10**9
D = 20
S_MAX = D * 81 # 1620
cnt = [0] * (S_MAX + 1)
tot = [0] * (S_MAX + 1)
cnt[0] = 1
for _ in range(D):
nc = [0] * (S_MAX + 1)
nt = [0] * (S_MAX + 1)
for s in range(S_MAX + 1):
if cnt[s] == 0:
continue
for d in range(10):
ns = s + d * d
if ns > S_MAX:
break
nc[ns] = (nc[ns] + cnt[s]) % MOD
nt[ns] = (nt[ns] + 10 * tot[s] + d * cnt[s]) % MOD
cnt, tot = nc, nt
ans = 0
j = 1
while j * j <= S_MAX:
ans = (ans + tot[j * j]) % MOD
j += 1
print(ans)
if __name__ == "__main__":
solve()