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
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.
Pseudocode
MOD = 10^9
S_MAX = 1620
C[0..S_MAX] = 0; T[0..S_MAX] = 0
C[0] = 1
For k from 1 to 20:
C'[0..S_MAX] = 0; T'[0..S_MAX] = 0
For s from 0 to S_MAX:
If C[s] == 0 then continue
For d from 0 to 9:
s' = s + d*d
if s' > S_MAX: break // digits are in order, so d^2 increases
C'[s'] += C[s]
T'[s'] = (T'[s'] + 10*T[s] + d*C[s]) mod MOD
C = C'; T = T'
answer = 0
For j from 1 to 40:
answer = (answer + T[j*j]) mod MOD
Return answer
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()