A triangular number is a number that is the sum of n
natural numbers from 1 to n
. For example 1 + 2 + 3 + 4 = 10
so 10
is a triangular number.
Given a positive integer (0 < n <= 10000
) as input (can be taken as an integer, or as a string), return the smallest possible triangular number that can be added to the input to create another triangular number.
For example given input 26
, adding 10
results in 36
, which is also a triangular number. There are no triangular numbers smaller than 10
that can be added to 26
to create another triangular number, so 10
is the correct result in this case.
0
is a triangular number, therefore if the input is itself a triangular number, the output should be 0
Testcases
Cases are given in the format input -> output (resulting triangular number)
0 -> 0 (0)
4 -> 6 (10)
5 -> 1 (6)
7 -> 3 (10)
8 -> 28 (36)
10 -> 0 (10)
24 -> 21 (45)
25 -> 3 (28)
26 -> 10 (36)
34 -> 21 (55)
10000 -> 153 (10153)
Scoring
This is code-golf so fewest bytes in each language wins!
26 -> 2
? \$\endgroup\$