# Find the smallest number bigger than the input whose digital sum is the input

"Digital sum" refers to the sum of all the digits in a number.

For example, the digital sum of 1324 is 10, because 1+3+2+4 = 10.

The challenge is to write a program/function to calculate the smallest number bigger than the input whose digital sum is the input.

## Example with walkthrough

As an example, take the number 9 as the input:

9 = 1+8 -> 18
9 = 2+7 -> 27
9 = 3+6 -> 36
...
9 = 8+1 -> 81
9 = 9+0 -> 90

The valid output would be the smallest number above, which is 18.

## Specs

Note that 9 is not the valid output for this example, because the reversed number must be greater than the original number.

Note that the input will be positive.

## Test-Cases:

2 => 11      (2 = 1 + 1)
8 => 17      (8 = 1 + 7)
12 => 39     (12 = 3 + 9)
16 => 79     (16 = 7 + 9)
18 => 99     (18 = 9 + 9)
24 => 699    (24 = 6 + 9 + 9)
32 => 5999   (32 = 5 + 9 + 9 + 9)

## References:

This is OEIS A161561.

Thanks to Martin Ender for the Leaderboard Snippet

• This title made my brain hurt. – Fatalize Jun 1 '16 at 12:26

# Shakespeare Programming Language, 456 bytes

,.Ajax,.Ford,.Page,.Act I:.Scene I:.[Enter Ajax and Ford]Ford:Listen tothy.Ajax:You be I.[Exit Ajax][Enter Page]Scene V:.Page:You be the sum ofyou a cat.Remember you.Ford:You zero.Scene X:.Ford:You be the sum ofyou the remainder of the quotient betweenI twice the sum ofa cat a big big cat.Page:You be the quotient betweenyou twice the sum ofa cat a big big cat.Be you nicer zero?If solet usScene X.Recall.Am I as big as Ajax?If notlet usScene V.Open heart

Try it online!

# GolfScript, 22 bytes

~:x{).10base{+}*x=!}do

Try it online!

Any tips are appreciated--I feel like this could be shorter.

### Explanation

~:x{).10base{+}*x=!}do
~                       Eval the input (casting from string to integer)
:x                     Store a copy in x
{               }do  Run a do...while loop using this block:
)                    Increment
.                   Duplicate
10base             Convert the number to a list of base-10 digits
{+}*         Fold on + (sum the digits)
x        Push x (the original input)
=       Test if the sum equals the original input
!      Negate, so the loop condition is true while the sum does *not* equal the
original input

When the loop exits, we have a number on the stack whose digit sum equals the original input. It is guaranteed to be greater than the original input because the loop, which begins by incrementing the number, executes at least once. Finally, when the program terminates, the stack is autoprinted.