[Question]
  [
Here is the sequence I'm talking about:
```
{1, 4, 5, 9, 10, 11, 16, 17, 18, 19, 25, 26, 27...}
```
Starting from 1, keep 1, drop the next 2, keep the next 2, drop 3, keep 3 and so on. Yes, it's on [OEIS (A064801)](https://oeis.org/A064801), too!
**The Challenge**
Given an integer `n>0`, find the nth term of the above sequence
**Test Cases** 
```
Input -> Output       
1->1  
22->49  
333->683
4444->8908
12345->24747
```
This is code golf so the shortest answer in bytes wins! Good luck!
  
[Answer]
# [Java (OpenJDK 8)](http://openjdk.java.net/), ~~45~~ 44 bytes
```
n->{int i=0;for(;++i<n;n-=i);return~-n+i*i;}
```
[Try it online!](https://tio.run/##ZU@xTsMwEJ3jr7jRJo1VWN1kAiQGpo6IwaROemlytmynalWFXw@GBpBgunfv3r131@mjLqwz1O0OMw7O@ghd4uQYsZfNSHVES/JxAYr904TojR7kE8XtF1KMufGtxxrqXocAzxqJXVi2kCHqmMrR4g6GNOJpC6l9eQXt2yBYlqTZj5n0mlrDb1dwtxaysf5B13uOUFSwPYdoBmnHKF1yiD3xgzHu3lvHUQihWDaxv7FJB7@q1JyWxO8HNynatMavYAEVNFACy2YqqsvnBpZrlQ7hKs9xQ4qKEoXyJo6e3gvK8QbVNKfw7MpBI7Vz/ZmfrhdN8wc "Java (OpenJDK 8) – Try It Online")
*-1 byte thanks to @Nevay*
After staring at this for a while, I noticed a pattern. Every time we drop `n` numbers, the next number in the sequence is a perfect square. Seeing this, I mentally broke the sequence into convenient chunks: `[[1],[4,5],[9,10,11],...]` Basically, the `i`th chunk starts with `i*i`, and iterates upwards for `i` elements.
To find the `n`th number in this sequence, we want to find first which chunk the number is in, and then which position in the chunk it occupies. We subtract our increment number `i` from `n` until `n` is less than `i` (which gives us our chunk), and then simply add `n-1` to `i*i` to get the correct `position` in the chunk.
Example:
```
n = 8
n > 1? Yes, n = n - 1 = 7
n > 2? Yes, n = n - 2 = 5
n > 3? Yes, n = n - 3 = 2
n > 4? No, result is 4 * 4 + 2 - 1 = 17
```
[Answer]
# [Python 3](https://docs.python.org/3/), ~~47~~ 46 bytes
1 byte thanks to Mr. Xcoder.
```
def f(n):a=round((2*n)**.5);return~-n+a*-~a//2
```
[Try it online!](https://tio.run/##TcoxDoMwDIXhnVN0tIMoih2jQtXDRCJRWRwUhYGFq6ddkPKm/5PefpZvUq51DfERQXHxn5wOXQHIKBrzFHznUI6s16C9N8Plx5HqnjctEMEidncTNWDmRu6/hpbYSXu2zspMk7C8ZsT6Aw "Python 3 – Try It Online")
VERY fast for higher numbers
[Answer]
## Haskell, ~~48~~ ~~43~~ 41 bytes
```
n#l=[l..l+n]++(n+1)#(l+2*n+3)
((0:0#1)!!)
```
4 extra bytes for 1-based indexing instead of 0-based. An unnecessary restriction, IMHO.
[Try it online!](https://tio.run/##y0gszk7Nyfn/P085xzY6R08vRzsvVltbI0/bUFNZI0fbSCtP21iTK81WQ8PAykDZUFNRUfN/bmJmnoKtQko@l4JCQVFmXomCikKagiEKzwQIUKWNjE1M/wMA "Haskell – Try It Online")
```
n#l             -- n is one less than the number of element to keep/drop and
                -- l the next number where the keep starts
   [l..l+n]     -- keep (n+1) numbers starting at l
   ++           -- and append a recursive call
   (n+1)#       -- where n is incremented by 1 and
      (l+2*n+3) -- l skips the elements to keep & drop
0#1             -- start with n=1 and l=0 and
 0:             -- prepend a dummy value to shift from 0 to 1-based index
    !!          -- pick the i-th element from the list 
```
[Answer]
# [Haskell](https://www.haskell.org/), 33 bytes
An anonymous function. Use as `((!!)$0:do n<-[1..];[n^2..n^2+n-1]) 1`
```
(!!)$0:do n<-[1..];[n^2..n^2+n-1]
```
[Try it online!](https://tio.run/##y0gszk7Nyflfraus4OPo5x7q6O6q4BwQoKCsW8vFlZuYmWebmVeSWpSYXKJSmpeTmZdarJebWKBRnJFfrpemV5SamKKpBxbm4kqzjfmvoaioqWJglZKvkGejG22opxdrHZ0XZ6SnByS083QNY///N@QyMuIyNjbmMgECLkMjYxNTLgA "Haskell – Try It Online")
* Constructs the sequence as an infinite list, then indexes into it with `!!`. The `0:` is a dummy element to adjust from 0- to 1-based indexing.
* The range `[n^2..n^2+n-1]` constructs a subsequence without gaps, starting with the square of `n` and containing `n` numbers.
* The `do` notation concatenates the constructed ranges for all `n>=1`.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 8 bytes
```
Ḷ+²µ€Ẏ⁸ị
```
[Try it online!](https://tio.run/##ASYA2f9qZWxsef//4bi2K8KywrXigqzhuo7igbjhu4v/w4fFkuG5mP//Nw "Jelly – Try It Online")
[Answer]
# [Python 3](https://docs.python.org/3/), 46 bytes
```
f=lambda x,y=0,z=0:y<x and f(x,y+z,z+1)or~-y+x
```
[Try it online!](https://tio.run/##K6gsycjPM/7/P802JzE3KSVRoUKn0tZAp8rWwKrSpkIhMS9FIU0DKKZdpVOlbaiZX1SnW6ld8b@gKDOvRCNNw1BTkwvGNjJC4hgbGyPxTIAAiWtoZGxiqqn5HwA "Python 3 – Try It Online")
[Answer]
# TeX, 166 bytes
```
\newcommand{\f}[1]{\count0=0\count1=0\loop\advance\count0 by\the\count1\advance\count1 by1\ifnum\count0<#1\repeat\advance\count0 by#1\advance\count0 by-1
\the\count0}
```
## Usage
```
\documentclass[12pt,a4paper]{article}
\begin{document}
\newcommand{\f}[1]{\count0=0\count1=0\loop\advance\count0 by\the\count1\advance\count1 by1\ifnum\count0<#1\repeat\advance\count0 by#1\advance\count0 by-1
\the\count0}
\f{1}
\f{22}
\f{333}
\f{4444}
\f{12345}
\end{document}
```
[![enter image description here](https://i.stack.imgur.com/vyXG0.png)](https://i.stack.imgur.com/vyXG0.png)
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 12 bytes
```
LεÝÁćn+}˜¹<è
```
[Try it online!](https://tio.run/##AR4A4f8wNWFiMWX//0zOtcOdw4HEh24rfcucwrk8w6j//zc "05AB1E – Try It Online")
[Answer]
# [Perl 6](https://perl6.org), 43 bytes
```
{(1..*).rotor({++$=>++$+1}...*).flat[$_-1]}
```
[Test it](https://tio.run/##PY7BasMwDIbvegpRQnHm2J1jL00JMb3u3ttWRpe4EEjjkLhlI@TJetuLpU5XqoPQ///6kFrT1cl07g1eEl5kcPrFZWFLg/k0EMH5S8g762xHBkqDXPtGxcjv/rE@uI/gi4n9OHls60zvMMe6akxPwr8rL@zpm6w@S7qa1XvjMpgP7fxeBm19aJDeoQyOtnvwTCMJqqY9uygwP60pnClDHACx6nF@7JGGET7zCBf/5kw/3QWMk2BaIEIcM602fpBSMp2kEpQvptPNawoiluqN6Vit1foG "Perl 6 – Try It Online")
## Expanded:
```
{  # bare block lambda with implicit parameter ｢$_｣
  ( 1 .. * )                  # range starting from 1
  .rotor(                     # break it into chunks
    { ++$  =>  ++$ + 1} ... * # infinite Seq of increasing pairs
    #   1  =>    1 + 1    ==>   1 => 2 ( grab 1 skip 2 )
    #   2  =>    2 + 1    ==>   2 => 3
    #   3  =>    3 + 1    ==>   3 => 4
    # ...  =>  ... + 1
  ).flat\                     # reduce the sequence of lists to a flat sequence
  [ $_ - 1 ]                  # index into the sequence
                              # (adjusting to 0-based index)
}
```
`(1..*).rotor({++$=>++$+1}...*)` produces:
```
(
 (1,),
 (4, 5),
 (9, 10, 11),
 (16, 17, 18, 19),
 (25, 26, 27, 28, 29),
 ...
).Seq
```
[Answer]
## Mathematica, 37 bytes
```
Flatten[Range@#+#^2-1&~Array~#][[#]]&
```
## Explanation
```
Range@#+#^2-1&
```
`Function` which takes a positive integer `#` and returns the run of `#` consecutive numbers in the sequence.
```
...~Array~#
```
Produces the list of all such runs up to the input `#`
```
Flatten[...][[#]]
```
`Flattens` the resulting list and returns the `#`th element.
[Answer]
# Javascript, ~~43~~ 38 bytes
```
n=>eval("for(r=1;n>r;)n-=r++;r*r+n-1")
```
[Try it online!](https://tio.run/##ZcpBCsMgFATQfU4RstKKTdV0Zc1dJNVikf/DT8n1rS6Dsxl4M19/@mOjtP8k4DuU6Aq4NZw@sykiMXLKwkqWg3QkhKUbCZBq4mWe255SPYwpvfSjlRB83BAOzOGe8cNiPXBuh@GKqtmVtO7NGNPjUtOr0mZ5Vi5/ "JavaScript (Node.js) – Try It Online")
I use the fact, that for each [triangular number](https://en.wikipedia.org/wiki/Triangular_number) plus one, the result is a square number.
As an example: triangular numbers are 0, 1, 3, 6, 10... so for 1, 2, 4, 7, 11... we observe 1, 4, 9, 16, 25... in our sequence.
If the index is somewhere between these known numbers, the elements of our sequence only advance by one. For instance, to calculate the result for 10, we take 7 (as a triangular number plus one), take the result (16) and add 10-7=3. Thus, 16+3=19.
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 8 bytes
```
v₍ʁ²ṠfJi
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCJ24oKNyoHCsuG5oGZKaSIsIiIsIjIyIl0=)
Would beat Jelly if zero-indexing were allowed (remove `J`).
```
v₍ʁ²ṠfJi
v        # For each in a range [1, n]:
 ₍       #  Apply both of the next two elements and wrap in a list:
  ʁ      #   Range [0, n), and:
   ²     #   Square n
    Ṡ    # Sum each (vectorizing)
         # For an input of 4, this will give [[1], [4, 5], [9, 10, 11], [16, 17, 18, 19]]
         # Basically, for each x in [1, n], add x^2 to each in [0, x)
     f   # Flatten
      J  # Prepend the input to the beginning (doesn't matter what we prepend)
       i # Index the input into this
```
[Answer]
# [cQuents](https://github.com/stestoltz/cQuents), 27 bytes
```
#R(2B)^.5)|1:b~-B+A*-b~A//2
```
[Try it online!](https://tio.run/##Sy4sTc0rKf7/XzlIw8hJM07PVLPG0CqpTtdJ21FLN6nOUV/f6P9/EwA "cQuents – Try It Online")
Currently a port of [Leaky's Python answer](https://codegolf.stackexchange.com/a/139906/65836), I think there's a better way though.
[Answer]
# [Swift 3](https://www.swift.org), 72 bytes
Port of my [Python solution](https://codegolf.stackexchange.com/a/139917/59487).
```
func f(_ x:Int,_ y:Int=0,_ z:Int=0)->Int{return y<x ?f(x,y+z,z+1):y+x-1}
```
**[Test Suite.](http://swift.sandbox.bluemix.net/#/repl/599b038cbb3c087fee1d8915)**
[Answer]
# [C# (Mono)](http://www.mono-project.com/), 164 bytes
```
using System.Linq;n=>{var a=new int[1]{1}.ToList();for(int i=1,m;a.Count<n;a.AddRange(new int[++i*2].Select((_,d)=>m+d+1).Skip(i).Take(i)))m=a.Max();return a[n-1];}
```
[Try it online!](https://tio.run/##TVBLa8MwDL77V@hoL67BSXdKXRiFnVoYa2GHEoZJ1M60kbfY6TZCfnvmDsqmgx6fXp9Uh1nryU99cHSE7XeI2Jbsf6TWjj5KxuqzDQGe2MBCtNHVcPGugY11xEUCH3uqF46ihKSWcAAzkVkOF9uBNYSfV3ivq0GPaufXLkQuyoPveILBGS3b0qqV7ykuKHkPTfNs6Yj81pll7i6v1BbPWEfOX2UjzLLNmkwLtT25d@6E2tkTJitEa6za2K@0ocPYdwR2TzNdleOU7rhOqyBiiCsbMICBQcs8l0VRyHkSqfNifj@WLLFDW7/BL8VbfSLz1yvYylPwZ1QvnYuYHoX8wG9pIdK2kY3TDw "C# (Mono) – Try It Online")
[Answer]
# [Perl 5](https://www.perl.org/), 33 + 1 (-p) = 34 bytes
```
$\+=$.--?1:2+($.=++$s)while$_--}{
```
[Try it online!](https://tio.run/##K0gtyjH9/18lRttWRU9X197QykhbQ0XPVltbpVizPCMzJ1UlXle3tvr/f0MjYxPTf/kFJZn5ecX/dX1N9QwMDf7rFgAA "Perl 5 – Try It Online")
[Answer]
# [MATL](https://github.com/lmendo/MATL), 12 bytes
```
:"@U@:q+]vG)
```
[Try it online!](https://tio.run/##y00syfn/30rJIdTBqlA7tsxd8/9/EyAAAA "MATL – Try It Online")
### Explanation
```
:        % Push range [1 2 ... n], where n is implicit input
"        % For each k in that range
  @U     %   Push k^2
  @:     %   Push range [1 2 ... k]
  q      %   Subtract 1: gives [0 1 ... k-1]
  +      %   Add: gives [k^2 k^2+1 ... k^2+k-1]
]        % End
v        % Concatenate all numbers into a column vector
G)       % Get n-th entry. Implicitly display
```
[Answer]
# [Tampio](https://github.com/fergusq/tampio), ~~310~~ 308 bytes
```
n:n uni on n unena 1:lle
a unena k:lle on a vuona k:lla vähennettynä a:sta ja k
a vuona nollalla ja k on a
a vuona k:lla vähennettynä nollasta ja k on a
a vuona b:n seuraajalla ja k on yhteenlaskun kutsuttuna k:n kerrottuna 2:lla arvolla ja k:n vähennettynä a:sta arvolla unena k:n seuraajalle seuraaja
```
Usage: `4:n uni` evaluates to `9`.
Explanation:
```
n:n uni on n unena 1:lle
uni(n)  =  n `uni` 1
a unena k:lle on  a vuona  k:lla vähennettynä a:sta ja k
a `uni` k     =  (a `vuo` (k     `vähennetty` a)    )  k
 a vuona nollalla ja k on a
(a `vuo` 0        )  k =  a
 a vuona  k:lla vähennettynä nollasta ja k on a
(a `vuo` (k     `vähennetty` 0)       )  k =  a
 a vuona  b:n seuraajalla ja k on
(a `vuo` (b   + 1)        )  k =
 yhteenlaskun kutsuttuna k:n kerrottuna 2:lla arvolla
(yhteenlasku            (k   *          2     )
 ja k:n vähennettynä a:sta arvolla unena  k:n seuraajalle seuraaja
((  k   `vähennetty` a     )       `uni` (k   + 1)   )  ) + 1
```
From standard library:
```
a `vähennetty` b = b - a
yhteenlasku a b  = a + b
```
[Answer]
# JavaScript (ES6), 33 bytes
Recursive solution inspired by [Xanderhall's observations](https://codegolf.stackexchange.com/a/139941/58974).
```
f=(n,x=1)=>n<x?n+x*x-1:f(n-x,++x)
```
---
## Try it
```
o.innerText=(
f=(n,x=1)=>n<x?n+x*x-1:f(n-x,++x)
)(i.value=12345);oninput=_=>o.innerText=f(+i.value)
```
```
<input id=i type=number><pre id=o>
```
[Answer]
# [Python 3](https://docs.python.org/3/), 50 bytes
```
def f(n):
 a=i=0
 while a<n:a+=i;i+=1
 return~-a+n
```
[Try it online!](https://tio.run/##TcrBCkIhEIXhvU8xSwcJGkeje8uHEVLuQEwXMaJNr25tAs/q/@Ds7749lMe4lQrVKq4GcpJ0NPDa5F4gX3XNLslFXCIDrfRn088hOx17E@22WkI0//Z@AjNPCr9NJM8hzmcKFBd/ihzPC@L4Ag "Python 3 – Try It Online")
[Answer]
# Mathematica, 82 bytes
```
Complement[Range[3#],Array[#+⌊((r=Sqrt[1+8#])-1)/2⌋⌊(r+1)/2⌋/2&,3#]][[#]]&
```
[Answer]
# [Python](https://docs.python.org/2/), 40 bytes
```
lambda n:(round((2*n)**.5)+.5)**2//2+n-1
```
[Try it online!](https://tio.run/##TcrBCsIwEATQu1@R426q1t1NxBb8Ey@RGg3YTQn14NfHXoQMDMyDWb7rK6vUeL3Vd5jvUzA6QskfnQDYKlp79NhttZb7njs9UI25mGSSmhL0@QDa0wnHpSRdIUJC3P03cwMRaeS2NCQW59szOfIDn734y4BYfw "Python 3 – Try It Online")
Optimizing [Leaky Nun's expression](https://codegolf.stackexchange.com/a/139906/20260).
---
# [Python](https://docs.python.org/2/), 41 bytes
```
f=lambda n,k=1:n*(n<=k)or-~f(n-k,k+1)+2*k
```
[Try it online!](https://tio.run/##TccxDoMwDEDRvafIaEOQaocuqDlMUJsSuXVQxNKFqwcWpPzpv/W/LVm51ui/4Te/glErnibtQJ9eMJdhj6CDWOkJe@6kxlxMMklNCfp5A1m647SWpBtESIi365kbOOcajWcNid34QKwH "Python 2 – Try It Online")
Recursive expression.
[Answer]
# Javascript (ES6) 100 98 Bytes
```
var k=n=>{var s=[],i=1,c=1,j;while(s.length<n){for(j=i;j<i+c;j++){s.push(j)}i+=c*2+1;c++}return s}
```
Kind of did this one fast, so I bet there is a lot of room for improvement, just basic loops and counters.
[Answer]
# [Retina](https://github.com/m-ender/retina), 27 bytes
```
.+
$*
((^1|1\2)+)1
$1$2$&
1
```
[Try it online!](https://tio.run/##K0otycxL/K@q4Z7wX0@bS0WLS0MjzrDGMMZIU1vTkEvFUMVIRY3L8P9/Qy4jIy5jY2MuEyDgMjQyNjEFAA "Retina – Try It Online") Port of @LeakyNun's Python answer. The first and last stages are just boring decimal ⇔ unary conversion. The second stage works like this: `((^1|1\2)+)` is a triangular number matcher; `$1` is the matched triangular number while `$2` is its index. The trailing `1` means it matches the largest triangular number strictly less than the input, thus resulting in exactly one fewer iteration than the Python loop, meaning that `$1` is equivalent to `a-i` and `$2` to `i-1` and their sum is `a-1` or `~-a` as required. (`$&` just prevents the match from being deleted from the result.) Note that for an input of `1` no matching happens and the output is simply the same as the input. If you were perverse you could use `^((^1|1\2)*)1` to match in that case too.
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 38 bytes
Using @Xanderhall's algorithm here
```
i;f(n){for(i=0;++i<n;n-=i);n=n-1+i*i;}
```
[Try it online!](https://tio.run/##XY5BCoMwEEX3niIUCkntgCZpVVKz76oXcFMsKbPoVCSuxLOnycrqXw3vP4bfw7vvQ0DjOInZfUeObWHyHG9kCFoUhlqCMscTmiV8nkhcZHPGYoYRyTt@uNMweQaWPSafLsY6OgizcUqwZeePr9icHS/FrpYSrG5WQcq9oZQCe63V6kSyl3QM2Lop6lVLLHpsO0cqfQErdaWrv1mJpp9L@AE "C (gcc) – Try It Online")
[Answer]
# PHP, ~~48 42~~ 37+1 bytes
ported from [Leaky Nun´s answer](https://codegolf.stackexchange.com/a/139916/55735)
```
while($argn>$a+=$i++);echo$a+~-$argn;
```
Run as pipe with `-F` or [try it online](http://sandbox.onlinephpfunctions.com/code/d22b7f8877f1fc3a09a84ee95093858b3536ee1a).
**direct approach, 42+1 bytes**
(ported from [Leaky Nun´s other answer](https://codegolf.stackexchange.com/a/139906/55735))
```
<?=($a=(2*$argn)**.5+.5|0)*-~$a/2+~-$argn;
```
Run as pipe with `-nR` or uncomment in above TiO.
**older iterative solution, 48+1 bytes**
```
for(;$argn--;$n++)$c++>$z&&$n+=++$z+$c=1;echo$n;
```
[Answer]
# TI-Basic, ~~41~~ 22 bytes
```
Input N
int(.5+√(2N
N-1+.5Ans(Ans+1
```
Port of [Leaky Nun's Python 3 answer](https://codegolf.stackexchange.com/a/139906/11261).
**I was quite happy with my previous 41-byte solution so here it is:**
```
Input N
For I,1,N
For J,1,I(N>0
DS<(N,0
Ans+1
Ans+1
End
End
Ans-1
```
Needs a fresh calculator, or at least `Ans` initialized to `0`
[![demo](https://i.stack.imgur.com/pfo8om.jpg)](https://i.stack.imgur.com/pfo8om.jpg)
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~37~~ 36 bytes
-1 byte thanks to [Steffan](https://codegolf.stackexchange.com/users/92689/steffan)
Port of [Leaky Nun's Python 3 answer](https://codegolf.stackexchange.com/a/139906/11261).
```
->n{~-n-(a=((2*n)**0.5).round)*~a/2}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72kqDSpcsEiN9ulpSVpuhY3VXTt8qrrdPN0NRJtNTSMtPI0tbQM9Ew19YryS_NSNLXqEvWNaqFq7QsU3KINY7lAlJERhDY2NoYwTIAAwjI0MjYxhcoamhiaWhqZmRqbWljGQoxZsABCAwA)
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 9 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
0-indexed
```
g°Uô²cÈóY
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=Z7BV9LJjyPNZ&input=OA)
```
g°Uô²cÈóY     :Implicit input of integer U
g             :Index into
 °U           :  Prefix increment U
   ô          :  Range [0,U]
    ²         :  Square each
     cÈ       :  Flat map each X at 0-based index Y
       óY     :    Range [X,X+Y)
```
]