37
2
(Challenge taken from a multiplayer game (clash of code) at codingame.com)
The challenge
Find the n-th term of the following sequence: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4...
or, to make it more obvious, {1}, {1,2}, {1,2,3}, {1,2,3,4}...
The sequence is made up from concatenated ranges from 1 to x, starting from 1, all the way up to infinity.
Rules / IO
Input and output can be in any format, as long as it's distinguishable. Input can be taken from any appropriate source: STDIN, file, etc...
The input can be 0- or 1-indexed, and the selected indexing must be mentioned in the post.
You will have to handle at least up to a result of 255 inclusive (meaning the 0-indexed maximum input is 32640). Anything over that has to be handled, if your language supports it.
This is code-golf
so the shortest byte count wins!
Test cases (0-based indexing)
0 -> 1
1 -> 1
5 -> 3
10 -> 1
59 -> 5
100 -> 10
1001 -> 12
11OEIS – Gurupad Mamadapur – 2017-01-22T14:44:23.440
4You should probably add a few more larger test cases (
59
,100
, etc) – FlipTack – 2017-01-22T16:07:56.677Related: http://codegolf.stackexchange.com/questions/103670/output-integers-in-negative-order-increase-the-maximum-integer-everytime/
– JAD – 2017-01-23T09:27:18.333It's the challenge in reverse. The best answers from that challenge work in a way that couldn't be reversed. @JarkoDubbeldam – devRicher – 2017-01-23T12:45:47.430
@devRicher I know, just putting it out there and it wasn't meant negatively. My own answer there actually was reversable. Related != duplicate. – JAD – 2017-01-23T14:27:53.223
How many terms do we have to support? Is it guaranteed that the input will not overflow our languages' integer types? – FlipTack – 2017-02-01T07:40:51.550
@FlipTack Each language has to handle a result at least up to 255 inclusive (meaning the input is higher than 255). Anything over that needs to be handled if your language supports it. I'll edit that in. – Yytsi – 2017-02-01T08:03:24.297
What do you mean "the input is higher"? Don't you mean lower or equal to? – FlipTack – 2017-02-01T09:09:33.310
@FlipTack No. To arrive at a result of 255, the 0-indexed input has to be 32640. – Yytsi – 2017-02-01T09:57:23.283