[Question]
  [
A [triangular number](https://en.wikipedia.org/wiki/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](/questions/tagged/code-golf "show questions tagged 'code-golf'") so fewest bytes **in each language** wins!
  
[Answer]
# Java 8, 58 57 bytes
```
n->{int i=0,m=0;while(n!=0)n+=n<0?++i:--m;return-~i*i/2;}
```
[Online test suite](https://tio.run/##ZZBNU4MwEIbv/RUrJxCofGjtFLE3b556dDyksNS0sHQgRZwO/nVc6Ad0nEkOebJ599lsRSXsfI@0jXdtlIqyhHch6TgBkKSwSESE8NafewJYiVRPc9oAGQHThjevUgklI66EhCCEluzXY1cuQ8fKQif4/pIp6nQXOgaZIb04S9OUC9vOggLVoSD7V97LBy9o2mDCcftCVkLhJbbKZQwKS6V3mbV1Mqn3GCmMDRjs8nWJRYUxKyQ07V3rXhNg9VMqzKb5QU05nVRK@lAdXtNgCVq@02ABunZlGphDgQmaNTTqri4H4/wj3QCHdcriY/@M/1VfKe69@fgEYZys@6m8mQWucxbtietYcAMeLYDZGDwxcMfgmYE/BnMG3nxMPE7xbh75/4jHwf55kKb9Aw)
Thanks to [Dennis](https://codegolf.stackexchange.com/users/12012/dennis) for a 1-byte saving.
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~13~~ 12 bytes
*1 byte removed using an idea (set intersection) from [Emigna's 05AB1E answer](https://codegolf.stackexchange.com/a/129043/36398)*
```
Q:qYstG-X&X<
```
[**Try it online!**](https://tio.run/##y00syfn/P9CqMLK4xF03Qi3C5v9/QwMgAAA "MATL ‚Äì Try It Online")
### Explanation
Let `t(n) = 1 + 2 + ¬∑¬∑¬∑ + n` denote the `n`-th triangular number.
The code exploits the fact that, given `n`, the solution is upper-bounded by `t(n-1)`. To see this, observe that `t(n-1) + n` equals `t(n)` and so it is a triangular number.
Consider input `8` as an example.
```
Q:q   % Input n implicitly. Push [0 1 2 ... n]
      % STACK: [0 1 2 3 4 5 6 7 8]
Ys    % Cumulative sum
      % STACK: [0 1 3 6 10 15 21 28 36]
t     % Duplicate
      % STACK: [0 1 3 6 10 15 21 28 36], [0 1 3 6 10 15 21 28 36]
G-    % Subtract input, element-wise
      % STACK: [0 1 3 6 10 15 21 28 36], [-8 -7 -5 -2  2  7 13 20 28]
X&    % Set intersection
      % STACK: 28
X<    % Minimum of array (in case there are several solutions). Implicit display
      % STACK: 28
```
[Answer]
# [Java (OpenJDK 8)](http://openjdk.java.net/), 83 bytes
```
n->{int m=0,a=n,b;for(;a-->0;)for(b=0;b<=n;)m=2*n+b*~b++==a*~a?a*a+a:m;return m/2;}
```
[Try it online!](https://tio.run/##LY9Na4NAEIbv@RVDQHB1tdZqW9xueiv00FOOIYdZP8JaHUXXlCDmr1tNchjmZT7eZ6bEM3pNm1OZ/c7toCqdQlph38MPaoJxA/Co9gbNks6NzqBeevbedJpOhyNgd@rZbRSgXPz8wejKLwZKjW7I/ybz9dBQyJm83ajJQC0DjpK4EkXT2QI9bxcItmolA6E@JAlWy9AhVzlX5bpSonPFT3TQxaQWXW6GjqB@CsU0r2Rx46/7qzslQPkfLPJwHMNX/hzwiMf8jb/zMOIvEQ/jid1PBthfepPXfjMYv11@MoW9teIsAau3aMuBOBQ@tm11sYmxO2farDHN/w "Java (OpenJDK 8) ‚Äì Try It Online")
## Credits
* -3 bytes thanks to [ceilingcat](https://tio.run/##LY9Na4NAEIbv@RVDQHB1tdZqW9xueiv00FOOIYdZP8JaHUXXlCDmr1tNchjmZT7eZ6bEM3pNm1OZ/c7toCqdQlph38MPaoJxA/Co9gbNks6NzqBeevbedJpOhyNgd@rZbRSgXPz8wejKLwZKjW7I/ybz9dBQyJm83ajJQC0DjpK4EkXT2QI9bxcItmolA6E@JAlWy9AhVzlX5bpSonPFT3TQxaQWXW6GjqB@CsU0r2Rx46/7qzslQPkfLPJwHMNX/hzwiMf8jb/zMOIvEQ/jid1PBthfepPXfjMYv11@MoW9teIsAau3aMuBOBQ@tm11sYmxO2farDHN/w)
[Answer]
# Mathematica, 46 bytes
```
Min[Select[(d=Divisors[2#])-2#/d,OddQ]^2-1]/8&
```
[Answer]
# [Neim](https://github.com/okx-code/Neim), ~~12~~ 9 bytes
```
tSùïäŒõtùïö)0ùïî
```
This takes too long to compute (but works given infinite time and memory), so in the link I only generate the first 143 triangular numbers - using `¬£ùïñ`, which is enough to handle an input of 10,000, but not enough to time out.
Warning: this may not work in future versions. If so, substitute ¬£ for 143
Explanation:
```
t                 Infinite list of triangular numbers
 [ ùïñ]             Select the first  v  numbers
 [¬£ ]                              143
     Sùïä           Subtract the input from each element
       Œõ  )       Only keep elements that are
        tùïö          triangular
           0ùïî     Get the value closest to 0 - prioritising the higher number if tie
```
[Try it!](http://178.62.56.56:80/neim?code=t%C2%A3%F0%9D%95%96S%F0%9D%95%8A%CE%9Bt%F0%9D%95%9A)0%F0%9D%95%94&input=10000)
[Answer]
# [PHP](https://php.net/), 45 bytes
```
for(;!$$t;$t+=++$i)${$argn+$t}=~+$t;echo~$$t;
```
[Try it online!](https://tio.run/##K8go@G9jX5BRoMClkliUnmerZGyiZP0/Lb9Iw1pRRaXEWqVE21ZbWyVTU6UarEBbpaTWtg5IWqcmZ@TXgZT8/w8A "PHP ‚Äì Try It Online")
Is the shorter variant of `for(;!$r[$t];$t+=++$i)$r[$argn+$t]=~+$t;echo~$r[$t];`
Expanded
```
for(;!$$t;  # stop if a triangular number exists where input plus triangular number is a triangular number
$t+=++$i) # make the next triangular number
  ${$argn+$t}=~+$t; # build variable $4,$5,$7,$10,... for input 4 
echo~$$t; # Output result 
```
# [PHP](https://php.net/), 53 bytes
```
for(;$d=$t<=>$n+$argn;)~$d?$n+=++$k:$t+=++$i;echo+$n;
```
[Try it online!](https://tio.run/##K8go@G9jX5BRoMClkliUnmerZGSiZP0/Lb9Iw1olxValxMbWTiVPGyxnrVmnkmIP5Nlqa6tkW6mUgBmZ1qnJGfnaKnnW//8DAA "PHP ‚Äì Try It Online")
Use the new [spaceship operator](http://php.net/manual/en/language.operators.comparison.php) in PHP 7
Expanded
```
for(;$d=$t<=>$n+$argn;) # stop if triangular number is equal to input plus triangular number 
  ~$d
    ?$n+=++$k  # raise additional triangular number
    :$t+=++$i; # raise triangular number sum
echo+$n; # Output and cast variable to integer in case of zero
```
# [PHP](https://php.net/), 55 bytes
```
for(;fmod(sqrt(8*($t+$argn)+1),2)!=1;)$t+=++$i;echo+$t;
```
[Try it online!](https://tio.run/##K8go@G9jX5BRoMClkliUnmerZGyiZP0/Lb9IwzotNz9Fo7iwqETDQktDpUQbrEBT21BTx0hT0dbQWhMoZqutrZJpnZqcka@tUmL9/z8A "PHP ‚Äì Try It Online")
[Answer]
# Java 8, ~~110~~ ~~102~~ ~~100~~ ~~93~~ 92 bytes
```
n->{int r=0;for(;t(r)<-t(n+r);r++);return r;}int t(int n){for(int j=0;n>0;n-=++j);return n;}
```
-2 bytes thanks to *@PeterTaylor*.  
-7 bytes thanks to *@JollyJoker*.  
-1 byte thanks to *@ceilingcat*.
**Explanation:**
[Try it online.](https://tio.run/##hY7NbsMgDIDvfQofQShRl/1KLH2D9tLjtAOjtCJLnIg4raaIZ89MVu2KhA22P6OvMVdT9IPD5vS92NaMI@yNx3kD4JFcOBvr4JDKtQFWpIxScydy8DkAQg0LFrs5zUK91ec@CE0iyPeCBKogdVCKk6MpIAQddSLp/tec8PRseBV3HEWtVPPPo44LwDB9td7CSIb4uvb@BB2biiMFj5ePTzDyTzP5Q8dK6G5rIVZbgOPPSK4r@4nKgXeoRdGVWFpRvcgc8rDNIk9Z4jlLvGaJtyxR5UUe80h1l42buPwC)
```
n->{                  // Method with integer as parameter and return-type
  int r=0;            //  Result-integer (starting at 0)
  for(;t(r)<-t(n+r);  //  Loop as long as neither `r` nor `n+r` is a triangular number
    r++);             //   And increase `r` by 1 after every iteration
  return r;}          //  Return the result of the loop
int t(int n){         // Separate method with integer as parameter and return-type
                      // This method will return 0 if the input is a triangular number
  for(int i=0;n>0;)   //  Loop as long as the input `n` is larger than 0
    n-=++j;           //   Decrease `n` by `j` every iteration, after we've raised `j` by 1
  return n;}          //  Return `n`, which is now either 0 or below 0
```
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), ~~17~~ 15 bytes
```
‚ü¶{a‚ÇÄ+}·∂†‚äáƒä-·πÖ?‚àßƒäh
```
[Try it online!](https://tio.run/##AS8A0P9icmFjaHlsb2cy///in6Z7YeKCgCt94bag4oqHxIot4bmFP@KIp8SKaP//MjX/Wg "Brachylog ‚Äì Try It Online")
### Explanation
```
‚ü¶                  [0, ‚Ä¶, Input]
 {   }·∂†            Find all‚Ä¶
  a‚ÇÄ+                ‚Ä¶Sums of prefixes (i.e. triangular numbers)
       ‚äáƒä          Take an ordered subset of two elements
         -·πÖ?       Subtracting those elements results in -(Input)
            ‚àßƒäh    Output is the first element of that subset
```
[Answer]
# [Python 2](https://docs.python.org/2/), 59 bytes
```
lambda n:min((r-2*n/r)**2/8for r in range(1,2*n,2)if n%r<1)
```
[Try it online!](https://tio.run/##FYpBDsIwDATP8ApfkJJiVGIKVBW8BDgEQUok6laWL7w@OHtZaWaWn35mppKu9/KN0/MVgYcps3Oyo4Zb8U1DbZ9mAYHMIJHHtwtoDsnnBLyRS/ClBlqDW0AghANCh3BEOCP0CGFv1AAZoZPprjLbY1ivFsmsTjE59ajber78AQ "Python 2 ‚Äì Try It Online")
This uses the following characterization of the triangular numbers `t` than can be added to `n` to get a triangular number:
> 
> `8*t+1 = (r-2*s)^2` for divisor pairs `(r,s)` with `r*s==n` and `r` odd.
> 
> 
> 
The code takes the minimum of all such triangular numbers.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 8 bytes
```
0r+\√∞f_·∏¢
```
[Try it online!](https://tio.run/##y0rNyan8/9@gSDvm8Ia0@Ic7Fv0/uudw@6OmNe7//5voKJjqKJjrKFjoKBga6CgYAQWMgCJGZjoKxiYgMSAAAA "Jelly ‚Äì Try It Online")
### How it works
```
0r+\√∞f_·∏¢  Main link. Argument: n
0r        Build [0, ..., n].
  +\      Take the cumulative sum, generating A := [T(0), ..., T(n)].
    √∞     Begin a dyadic chain with left argument A and right argument n.
      _   Compute A - n, i.e., subtract n from each number in A.
     f    Filter; keep only numbers of A that appear in A - n.
       ·∏¢  Head; take the first result.
```
[Answer]
# [Octave](https://www.gnu.org/software/octave/), ~~38~~ 36 bytes
*2 bytes off thanks to [@Giuseppe!](https://codegolf.stackexchange.com/users/67312/giuseppe)*
```
@(n)(x=cumsum(0:n))(any(x+n==x'))(1)
```
Anonymous function that uses almost the same approach as my [MATL answer](https://codegolf.stackexchange.com/a/129022/36398).
[**Try it online!**](https://tio.run/##y08uSSxL/f/fQSNPU6PCNrk0t7g0V8PAKk9TUyMxr1KjQjvP1rZCHcgz1PyfpmCrkJhXbM2VpmFkpqmjkKZhaACmTMCkKZg0B5MWYNIIImEMoYxMNf8DAA)
[Answer]
# [Japt](https://github.com/ETHproductions/japt), ~~24~~ ~~23~~ ~~16~~ ~~15~~ 13 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
√≤ √•+
m-N √¶@√∏X
```
1 byte saved thanks to [ETH](https://codegolf.stackexchange.com/users/42545/ethproductions).
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=8iDlKwptLU4g5kD4WA&input=MTE3Nw)
```
√≤ √•+\nm-N √¶@√∏X     :Implicit input of integer U
√≤                  :Range [0,U]
  √•+               :Cumulatively reduce by addition
    \n             :Reassign to U
      m-           :Map and subtract
        N          :  The array of inputs (i.e., the original value of U)
         √¶         :Get the first element that return true when
          @        :Passed through the following function as X
           √∏X      :  Does U contain X
```
[Answer]
# [C (GCC)](https://gcc.gnu.org), ~~58 55 54~~ 52 bytes
```
i;s;f(n){for(i=s=0;n;)n+=n<0?++i:--s;return-~i*i/2;}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=LY4xDoJAFERr_ilWjWZXRBFBjQtaaesFTIhZQH-CX8NCLIxexIaGa3gPbyOo003mTWaelQoPSpVlVeSJNX-7KLVMOIlbcs44BjqwJUlBZkC-vTJNXFiWllmcFxlZD-zjyJH3fzfsIKm0iGLm6zzC8_C4BEDK2WmPxAW7gdE4kmBcj5jGXKs9JbzdjdoD1iPBWgFbbzeCXbKa-wY7qqPmjpDwnylfNrjgwQzmMLbBccHxwJnCxK1trR_1AQ)
**EDIT**: *-3 bytes* by removing `!=0`
**EDIT**: *-1 byte* by moving variable declarations to a `for`
**EDIT**: *-2 bytes* by moving variable declarations outside the function, thanks to [ceilingcat](https://codegolf.stackexchange.com/users/52904/ceilingcat)
# Explanation
```
i;s;f(n){for(i=s=0;n;)n+=n<0?++i:--s;return-~i*i/2;}¬≠‚Å°‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å§‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å¢‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å¢‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å£‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å§‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å§‚Å°‚Å§‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å£‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å§‚Å¢‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å§‚Å£‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å§‚Å§‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å°‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å°‚Å¢‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å°‚Å£‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å§‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å¢‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å£‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å§‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å§‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å°‚Å§‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å¢‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å¢‚Å¢‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å¢‚Å°‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å¢‚Å£‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å¢‚Å§‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å£‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å£‚Å¢‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å£‚Å£‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å£‚Å§‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å§‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å§‚Å¢‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å§‚Å£‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å§‚Å§‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å°‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å°‚Å¢‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å°‚Å£‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å°‚Å§‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å¢‚Å°‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å¢‚Å¢‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å¢‚Å¢‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å¢‚Å£‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å¢‚Å§‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å£‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å£‚Å¢‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å£‚Å£‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å£‚Å§‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å§‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å§‚Å¢‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å§‚Å£‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å§‚Å§‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å§‚Å°‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å§‚Å°‚Å¢‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å§‚Å°‚Å£‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå¬≠
i;s;                                                  // ‚Äé‚Å°Declare i and s
    f(n){                                          }  // ‚Äé‚Å¢Declare function:
             i=s=0;                                   // ‚Äé‚Å£* Set i and s to 0
         for(      n;)                                // ‚Äé‚Å§* While n not 0:
                      n+=n<0?++i:--s;                 // ‚Äé‚Å¢‚Å°* * Increment i and add to n if n < 0
                                                      // ‚Äé‚Å¢‚Å°    Otherwise, decrement s and add to n
                                     return-~i*i/2;   // ‚Äé‚Å¢‚Å¢* Return i*(i+1)/2
üíé
```
Created with the help of [Luminespire](https://vyxal.github.io/Luminespire).
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 12 bytes
```
√ùŒ∑O√£D‚Ç¨√Ü¬πQ√è¬¨¬§
```
Uses the **05AB1E** encoding. [Try it online!](https://tio.run/##ASIA3f8wNWFiMWX//8OdzrdPw6NE4oKsw4bCuVHDj8KswqT//zI1 "05AB1E ‚Äì Try It Online")
[Answer]
# Mathematica, 62 bytes
```
(s=Min@Abs[m/.Solve[2#==(n-m)(n+m+1),{n,m},Integers]])(s+1)/2&
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 8 [bytes](https://github.com/Adriandmen/05AB1E/blob/master/docs/code-page.md)
```
√ùŒ∑ODI-√É–Ω
```
[Try it online!](https://tio.run/##MzBNTDJM/f//8Nxz2/1dPHUPN1/Y@/@/kQkA "05AB1E ‚Äì Try It Online")
or as a [Test suite](https://tio.run/##ATkAxv8wNWFiMWX/fHZ5/8OdzrdPRHktw4PQvf95IsO/OiDDvyIs/zQKNQo3CjgKMTAKMjQKMjUKMjYKMzQ)
**Explanation**
```
√ù          # range [0 ... input]
 Œ∑         # prefixes
  O        # sum each
   D       # duplicate
    I-     # subtract input from each
      √É    # keep only the elements in the first list that also exist in the second list
       –Ω   # get the first (smallest)
```
[Answer]
# [Python 2](https://docs.python.org/2/), ~~78~~ ~~71~~ 70 bytes
Seven bytes saved, thanx to [ovs](https://codegolf.stackexchange.com/users/64121/ovs) and [theespinosa](https://codegolf.stackexchange.com/users/41804/theespinosa)
One more byte saved due to the remark of [neil](https://codegolf.stackexchange.com/users/17602/neil), `x+9` is suffisant and [checked](https://tio.run/##dY6xCsJAEET7@4qxkZwmeHudgfuA@wwLo1u4CUuKkxB/Pd5iIQhOOW8ezPSc76PEbcuUFjl0LznFYVQIWKAXuV2bc41fnbXl21IIgXwPh/xPLEfzoJQeLE2m/cJdgY3YRplWX2n80F9ojIcq75LG3gGTssworVKr9e8b) for all natural numbers `0 <= n <= 10000`. It was also [verified](https://tio.run/##dY6xCsJAEET7@4qxkZwmeHudgfuA@wwLo1u4CUuKkxB/Pe5hIQhOOW8ezPSc76PEbcuUFjl0LznFYVQIWKAXuV2bs8Wvrrbl21IIgXwPh/xPLEcyD0rpwdJk2i/cFdQR11Gm1RuNH/oLK@PB5F3S2DtgUpYZpVVq1f6@AQ) for `x+1` instead of `x+9`, it works also.
```
x=input()
I={n*-~n/2for n in range(x+1)}
print min(I&{i-x for i in I})
```
[Try it online!](https://tio.run/##K6gsycjPM/r/v8I2M6@gtERDk8vTtjpPS7cuT98oLb9IIU8hM0@hKDEvPVWjQttQs5aroCgzr0QhNzNPw1OtOlO3QgGkKhOkyrNW8/9/IzMA "Python 2 ‚Äì Try It Online")
[Answer]
## JavaScript (ES6), ~~43~~ 42 bytes
```
f=(n,a=s=0)=>n?f(n+=n>0?--s:++a,a):a*++a/2
```
```
<input type=number min=0 value=0 oninput=o.textContent=f(+this.value)><pre id=o>0
```
Edit: Saved 1 byte thanks to @PeterTaylor.
[Answer]
# [Python 3](https://docs.python.org/3/), ~~60~~ 44 bytes
```
f=lambda n,k=1:(8*n+1)**.5%1and f(n+k,k+1)+k
```
*Thanks to @xnor for a suggestion that saved 16 bytes!*
[Try it online!](https://tio.run/##FYxBCoMwFETX9RR/E0z0V0ysrRXsXWytNER/JIkLT5/GgYGZNzDbEX6WmqjXzboA/vBZcuW/wX0/u/Pa0qJXHbisk0Sch2Vc39MIhGaQPe8KKqUoiqplcqQJZk6lQZNYaeJsHRBoghrhhtAiPBA6BJm6SkAlou4ITcqdfKpzSeqzy@Y0BZ6zdofrC1i358CAE57/Qoj4Bw "Python 3 ‚Äì Try It Online")
### Background
Let **n** be a non-negative integer. If **n** is the **k**th triangular number, we have
![condition](https://i.stack.imgur.com/pKX4Q.png)
which means there will be a natural solution if and only if **1 + 8n** is an odd, perfect square. Clearly, checking the parity of **1 + 8n** is not required.
### How it works
The recursive function **n** accepts a single, non-negative integer as argument. When called with a single argument, **k** defaults to **1**.
First, `(8*n+1)**.5%1` tests if **n** is a triangular number: if (and only if) it is, `(8*n+1)**.5` will yield an integer, so the residue from the division by **1** will yield **0**.
If the modulus is **0**, the `and` condition will fail, causing **f** to return **0**. If this happens in the initial call to **f**, note that this is the correct output since **n** is already triangular.
If the modulus is positive, the `and` condition holds and `f(n+k,k+1)+k` gets executed. This calls **f** again, incrementing **n** by **k** and **k** by **1**, then adds **k** to the result.
When **f(n0, k0)** finally returns **0**, we back out of the recursion. The first argument in the first call was **n**, the second one **n + 1**, the third one **n + 1 + 2**, until finally **n0 = n + 1 + ‚Ä¶ k0-1**. Note that **n0 - n** is a triangular number.
Likewise, all these integers will be added to the innermost return value (**0**), so the result of the intial call **f(n)** is **n0 - n**, as desired.
[Answer]
# [C# (.NET Core)](https://www.microsoft.com/net/core/platform), ~~291~~ 281 bytes
```
class p{static int Main(string[]I){string d="0",s=I[0];int c=1,j,k;for(;;){j=k=0;string[]D=d.Split(' '),S=s.Split(' ');for(;j<D.Length;j++)for(;k<S.Length;k++)if(D[j]==S[k])return int.Parse(D[k]);j=int.Parse(D[0])+c++;d=d.Insert(0,$"{j} ");s=s.Insert(0,$"{j+int.Parse(I[0])} ");}}}
```
[Try it online!](https://tio.run/##VY9Bi8MgEIX/ioSFKtpgz3ZuuQR2YSHH4CEY21WLLY49hfx217SU7c5p5pv34D2De3NNthRzmRDJbcE8ZWeIi5l8TS5SzMnF86h7tjxXMkMjG4HQj1KrTWfgILwI6nRNVCm2eAgg1cvYwdwOt4vLdEd2TAyAb@fT449d@2njOf8ozzl7sHAcXixU5k60G70GGMagWbL5nuIWsv2eEtr6q1R5eCdSM244V3MN0Ee0KVMpPprFr6RhCmuOf5T/ebdm7KFa17WUcpB1fgE "C# (.NET Core) ‚Äì Try It Online")
Program that takes a string as input and outputs through Exit Code.
Saved 10 Bytes thanks to Kevin Cruijssen
[Answer]
# JavaScript (ES7), ~~46~~ 44 bytes
```
f=(n,x=r=0)=>(8*(n+x)+1)**.5%1?f(n,x+=++r):x
```
---
## Try it
```
o.innerText=(
f=(n,x=r=0)=>(8*(n+x)+1)**.5%1?f(n,x+=++r):x
)(i.value=8);oninput=_=>o.innerText=f(+i.value)
```
```
<input id=i type=number><pre id=o>
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), 10 bytes
```
`·∏ü‚à´ŒòNo¬£‚à´N+
```
[Try it online!](https://tio.run/##yygtzv7/P@HhjvmPOlafm@GXf2gxkOGn/f//f0MDIAAA "Husk ‚Äì Try It Online")
[Answer]
# Dyalog APL, 19 bytes
*6 bytes saved thanks to @KritixiLithos*
```
{‚äÉo/‚ç®o‚àä‚ç®‚çµ+o‚Üê0,+\‚ç≥‚çµ}
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v8/7VHbhOpHXc35@o96V@Q/6ugCUo96t2rnA8UNdLRjHvVuBnJrgQoVjMy40hQMDYCECRCbArE5EFsAsRFIwBhEGJkCAA)
**How?**
`o‚Üê0,+\‚ç≥‚çµ` - assign `o` the first `‚çµ` triangular numbers
`o/‚ç®` - filter `o` by
`o‚àä‚ç®‚çµ+o` - triangular numbers that summed with `‚çµ` produce triangulars
`‚äÉ` - and take the first
[Answer]
# [Pari/GP](http://pari.math.u-bordeaux.fr/), 54 bytes
```
n->vecmin([y^2-1|y<-[2*n/d-d|d<-divisors(2*n)],y%2])/8
```
[Try it online!](https://tio.run/##FYhRCsIwEAWv8igIiexSG632o/YioUIxVgK6hlQKgd49xo@BmQlT9PwMecYVWXhYH/e3F2XTzXCzpZ6t2Uvt2G2uZ@dXv3ziosrTI6WdGXXd5SmEV1ICHhCil2/R6h8VZiVaE6w5E5oD4URoCRdCRzAljgXTjjr/AA "Pari/GP ‚Äì Try It Online")
[Answer]
# [Haskell](https://www.haskell.org/), 56 bytes
```
f x|e<-(`elem`scanl1(+)[0..x])=[n|n<-[0..],e n,e$x+n]!!0
```
[Try it online!](https://tio.run/##FcdLCoMwGEXhrfxCB4oa4tw46TIkYGivDxpvxWSQgXtP7YFvcFYXPvA@51nShb4tJ3jsU3g5@q6sq1ErlWxlRl7s2//ZBsIGj1TTFoXOu9soRhbE55cRjEGGwchxboyiZL6dcO/c6bsf "Haskell ‚Äì Try It Online")
[Answer]
# [Add++](https://github.com/cairdcoinheringaahing/AddPlusPlus), 68 bytes
```
L,RBFEREsECAAx$pBcB_B]VARBFEREsB]GEi$pGBcB*A8*1+.5^1%!!@A!@*b]EZBF#@
```
[Try it online!](https://tio.run/##S0xJKSj4/99HJ8jJzTXItdjV2dGxQqXAKdkp3ik2zBEq6hTr7pqpUuAOFNZytNAy1NYzjTNUVVR0cFR00EqKdY1yclN2@P//vwUA "Add++ ‚Äì Try It Online"), or see the [test suite](https://tio.run/##S0xJKSj4/99FJ03HwUonyMnNNci12NXZ0bFCpcAp2SneKTbMESrqFOvumqlS4A4U1nK00DLU1jONM1RVVHRwVHTQSop1jXJyU3YIS3ZXUtC1U1BydPD6r5JmZ8AFJExAhCmIMAcRFiDCECxjBJYyAssZmYFIYxOILBD8BwA)!
Even *Java* is beating me. I really need to add some set commands to Add++
## How it works
```
L,    - Create a lambda function
      - Example argument:  8
  R   - Range;     STACK = [[1 2 3 4 5 6 7 8]]
  BF  - Flatten;   STACK = [1 2 3 4 5 6 7 8]
  ER  - Range;     STACK = [[1] [1 2] ... [1 2 3 4 5 6 7 8]
  Es  - Sum;       STACK = [1 3 6 10 15 21 28 36]
  EC  - Collect;   STACK = [[1 3 6 10 15 21 28 36]]
  A   - Argument;  STACK = [[1 3 6 10 15 21 28 36] 8]
  A   - Argument;  STACK = [[1 3 6 10 15 21 28 36] 8 8]
  x   - Repeat;    STACK = [[1 3 6 10 15 21 28 36] 8 [8 8 8 8 8 8 8 8]]
  $p  - Remove;    STACK = [[1 3 6 10 15 21 28 36] [8 8 8 8 8 8 8 8]]
  Bc  - Zip;       STACK = [[1 8] [3 8] [6 8] [10 8] [15 8] [21 8] [28 8] [36 8]]
  B_  - Deltas;    STACK = [-7 -5 -2 2 7 13 20 28]
  B]  - Wrap;      STACK = [[-7 -5 -2 2 7 13 20 28]]
  V   - Save;      STACK = []
  A   - Argument;  STACK = [8]
  R   - Range;     STACK = [[1 2 3 4 5 6 7 8]]
  BF  - Flatten;   STACK = [1 2 3 4 5 6 7 8]
  ER  - Range;     STACK = [[1] [1 2] ... [1 2 3 4 5 6 7 8]]
  Es  - Sum;       STACK = [1 3 6 10 15 21 28 36]
  B]  - Wrap;      STACK = [[1 3 6 10 15 21 28 36]]
  G   - Retrieve;  STACK = [[1 3 6 10 15 21 28 36] [-7 -5 -2 2 7 13 20 28]]
  Ei  - Contains;  STACK = [[1 3 6 10 15 21 28 36] [0 0 0 0 0 0 0 1]]
  $p  - Remove;    STACK = [[0 0 0 0 0 0 0 1]]
  G   - Retrieve;  STACK = [[0 0 0 0 0 0 0 1] [-7 -5 -2 2 7 13 20 28]]
  Bc  - Zip;       STACK = [[0 -7] [0 -5] [0 -2] [0 2] [0 7] [0 13] [0 20] [1 28]]
  B*  - Products;  STACK = [0 0 0 0 0 0 0 28]
  A   - Argument;  STACK = [0 0 0 0 0 0 0 28 8]
  8*  - Times 8;   STACK = [0 0 0 0 0 0 0 28 64]
  1+  - Increment; STACK = [0 0 0 0 0 0 0 28 65]
  .5^ - Root;      STACK = [0 0 0 0 0 0 0 28 8.1]
  1%  - Frac part; STACK = [0 0 0 0 0 0 0 28 0.1]
  !!  - To bool;   STACK = [0 0 0 0 0 0 0 28 1]
  @   - Reverse;   STACK = [1 28 0 0 0 0 0 0 0]
  A   - Argument;  STACK = [1 28 0 0 0 0 0 0 0 8] 
  !   - Not;       STACK = [1 28 0 0 0 0 0 0 0 0]
  @   - Reverse;   STACK = [0 0 0 0 0 0 0 0 28 1]
  *   - Multiply;  STACK = [0 0 0 0 0 0 0 0 28]
  b]  - Wrap;      STACK = [0 0 0 0 0 0 0 0 [28]]
  EZ  - Unzero;    STACK = [[28]]
  BF  - Flatten;   STACK = [28]
  #   - Sort;      STACK = [28]
  @   - Reverse;   STACK = [28]
```
[Answer]
# [R](https://www.r-project.org/), ~~46~~ ~~44~~ ~~43~~ 41 bytes
```
function(x,y=cumsum(0:x))y[(x+y)%in%y][1]
```
[Try it online!](https://tio.run/##K/qfZvs/rTQvuSQzP0@jQqfSNrk0t7g0V8PAqkJTszJao0K7UlM1M0@1MjbaMPZ/moaRieZ/AA "R ‚Äì Try It Online")
An anonymous function with one mandatory argument, `x`; computes first `x+1` triangular numbers as an optional argument to golf out a few curly braces. I used `choose` before I saw Luis Mendo's [Octave answer](https://codegolf.stackexchange.com/a/129035/67312).
I shaved off a few bytes of Luis Mendo's answer but forgot to use the same idea in my answer.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 18 bytes
```
0rRS$‚Ç¨ƒã
0√∞,+√á‚Ç¨·∫†√∞1#
```
[Try it online!](https://tio.run/##y0rNyan8/9@gKChY5VHTmiPdXAaHN@hoH24Hch7uWnB4g6Hy////DQ0A "Jelly ‚Äì Try It Online")
[Answer]
# [Python 2](https://docs.python.org/2/), ~~83~~ 81 bytes
* @Felipe Nardi Batista saved 2 bytes.
```
lambda n:min(x for x in i(n)if n+x in i(n))
i=lambda n:[i*-~i/2for i in range(n)]
```
[Try it online!](https://tio.run/##K6gsycjPM/qfZRvzPycxNyklUSHPKjczT6NCIS2/SKFCITNPIVMjTzMzTSFPG87T5Mq0hauOztTSrcvUNwKpzwSpKErMS08Fqor9X1CUmVeikaVhZKap@R8A "Python 2 ‚Äì Try It Online")
[Answer]
# [APL (Dyalog Classic)](https://www.dyalog.com/), ~~16~~ 14 bytes
```
(‚äÉ0‚àò,‚à©‚ä¢-‚â¢)+\‚àò‚ç≥
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v8/7VHbBI1HXc0Gjzpm6DzqWPmoa5Huo85FmtoxQIFHvZv/P@qbClSSBqS4aMT8b2TGZWjAZcJlymXOZcFlZMJlbMJlZAoA "APL (Dyalog Classic) ‚Äì Try It Online")
]