Project Euler Problem 684

Define s(n) to be the smallest number that has a digit sum of n.

Project Euler Problem 684

Solution

Answer: 922058210

Let $s(n)$ be the smallest positive integer whose digit sum is $n$.

For example:

  • $s(1)=1$
  • $s(10)=19$
  • $s(20)=299$

We define

$$S(k)=\sum_{n=1}^k s(n).$$

We must compute

$$\sum_{i=2}^{90} S(f_i) \pmod{1,000,000,007},$$

where $f_i$ are Fibonacci numbers.


1. Mathematical analysis

Step 1: Formula for $s(n)$

To make the smallest number with digit sum $n$, we want:

  • as few digits as possible,
  • larger digits pushed to the right.

Write

$$n = 9q + r,\qquad 0 \le r \le 8.$$

Then the optimal number is:

  • one leading digit $r$ (if $r>0$),
  • followed by $q$ digits equal to $9$.

Examples:

  • $10 = 9\cdot1+1 \Rightarrow s(10)=19$
  • $20 = 9\cdot2+2 \Rightarrow s(20)=299$

Hence

$$s(n)=r\cdot 10^q + (10^q-1).$$

So:

$$\boxed{s(n)=(r+1)10^q-1}$$

with $n=9q+r$.


Step 2: Derive a formula for $S(k)$

Let

$$k=9q+r,\qquad 0\le r\le 8.$$

We sum complete blocks of 9 terms.

For a fixed $a\ge0$,

$$s(9a+t)=(t+1)10^a-1,\qquad 1\le t\le8,$$

and

$$s(9a+9)=10^{a+1}-1.$$

Therefore:

$$\sum_{t=1}^{9} s(9a+t) = \sum_{t=1}^{8}\big((t+1)10^a-1\big) + (10^{a+1}-1).$$

Compute:

$$\sum_{t=1}^{8}(t+1)=44,$$

so

$$=44\cdot10^a-8+10^{a+1}-1 =54\cdot10^a-9.$$

Thus each full block contributes

$$54\cdot10^a-9.$$

Summing $a=0,\dots,q-1$:

$$\sum_{a=0}^{q-1}(54\cdot10^a-9) = 54\frac{10^q-1}{9}-9q.$$

Hence the full-block contribution is

$$6(10^q-1)-9q.$$

Now add the remaining $r$ terms:

$$\sum_{t=1}^{r}\big((t+1)10^q-1\big).$$

Since

$$\sum_{t=1}^{r}(t+1)=\frac{r(r+3)}2,$$

this becomes

$$\frac{r(r+3)}2,10^q-r.$$

Combining:

$$S(k) = 6(10^q-1)-9q + \frac{r(r+3)}2,10^q-r.$$

Therefore

$$\boxed{ S(k)=\left(\frac{r(r+3)}2+6\right)10^q-9q-r-6 }$$

where $k=9q+r$.


2. Python implementation

MOD = 1_000_000_007

def S(k):
    """
    Compute S(k) modulo MOD using the closed formula.
    """
    q, r = divmod(k, 9)

    # Closed form:
    # S(k) = ((r(r+3))/2 + 6) * 10^q - 9q - r - 6
    value = (
        ((r * (r + 3)) // 2 + 6) * pow(10, q, MOD)
        - 9 * q
        - r
        - 6
    )

    return value % MOD

# Fibonacci iteration
f0, f1 = 0, 1

answer = 0

for _ in range(2, 91):
    f0, f1 = f1, f0 + f1
    answer = (answer + S(f1)) % MOD

print(answer)

3. Code walkthrough

Function S(k)

q, r = divmod(k, 9)

Writes

$$k=9q+r.$$


pow(10, q, MOD)

Efficiently computes

$$10^q \bmod MOD$$

using fast modular exponentiation.


((r * (r + 3)) // 2 + 6)

This is exactly the coefficient derived in the formula.


Fibonacci loop

f0, f1 = 0, 1

Starts Fibonacci generation.

Each iteration:

f0, f1 = f1, f0 + f1

updates to the next Fibonacci number.


Validation on the sample

The problem states:

$$S(20)=1074.$$

Check manually:

$$20 = 9\cdot2 + 2,$$

so $q=2,r=2$.

Formula gives:

$$S(20) = \left(\frac{2(5)}2+6\right)10^2 - 18 -2 -6$$

$$=(5+6)\cdot100 -26$$

$$=1100-26 =1074.$$

Correct.


4. Final verification

  • Fibonacci numbers up to $f_{90}$ are huge, so direct construction of $s(n)$ is impossible.
  • The closed form reduces everything to modular powers of 10.
  • Complexity is tiny: only 89 evaluations.

The computation yields:

$$922058210.$$


Answer: 922058210