Find the smallest number bigger than the input whose digital sum is the input
Python 2, 33 bytes
lambda n:[n+9,`n%9`+n/9*'9'][n>9]
A direct expression. Makes a number string with 9's at the end and the remainder at the beginning. Except, for single-digit n
, gives n+9
.
Some outputs have leading zeroes (099
for 18
).
Pyth, 8 bytes
fqQsjT;h
Test suite.
fqQsjT;h
f h first number T from (input+1) onward where:
qQ the input is equal to
s the sum of
jT; the base-10 representation of T
Retina, 39 31 bytes
r`1{1,9}
$.&
T`d`_d`^.$
^.$
1$&
Takes input in unary.
Try it online! (The first two lines allows running several test cases at once and converts from decimal to unary for convenience.)
This doesn't actually search for the result linearly, but computes it explicitly:
- If the input
n
is greater than 9, we replace it withn % 9
followed byn / 9
(floored) nines. - Otherwise, we replace it with
n + 9
.
Using !
(or anything else that isn't 1
) as the unary digit, I can save one more byte with the following approach:
^!(?!!{9})
1
r`!{0,9}
$.&
0\B
But this input format is a bit of a stretch, I think.