15
2
The SUDSI sequence (sum, difference, swap, increment) is a curious integer sequence that appears to exhibit rather chaotic behavior. It can be generated as follows:
Let S be an infinite list of the natural numbers: 1 2 3 4 5 6 ...
. Let Si denote the one-indexed ith element of S. So initially, S1 is 1, S2 is 2, etc. (there is no S0).
Starting with S1 and S2 ...
- Compute their sum:
sum = S1 + S2
- Compute their absolute difference (the larger one minus the smaller one):
diff = |S1 - S2|
Swap the two values in S at the indices of the sum and difference:
swap(Ssum, Sdiff)
Increment the indices of S you are working with. So next time you will compute the sum and difference of S2 and S3, and the time after that it will be S3 and S4, etc.
- Repeat this process indefinitely.
Here are the first few stages of S as this process is applied. The brackets []
surround the two values that are about to be summed and differenced.
Original S:
[1 2] 3 4 5 6 7 8 9 10 11 12 ...
After S3 (3 = 1 + 2
) and S1 (1 = |1 - 2|
) are swapped:
3 [2 1] 4 5 6 7 8 9 10 11 12 ...
After S3 and S1 are swapped:
1 2 [3 4] 5 6 7 8 9 10 11 12 ...
After S7 and S1 are swapped:
7 2 3 [4 5] 6 1 8 9 10 11 12 ...
After S9 and S1 are swapped:
9 2 3 4 [5 6] 1 8 7 10 11 12 ...
After S11 and S1 are swapped:
11 2 3 4 5 [6 1] 8 7 10 9 12 ...
After S7 and S5 are swapped:
11 2 3 4 1 6 [5 8] 7 10 9 12 ...
etc.
The SUDSI sequence is defined as the sequence of the first elements in each of these lists. So the first few terms of the SUDSI sequence are 1 3 1 7 9 11 11
.
Here are the first 200 terms of the SUDSI sequence (20 per line):
1 3 1 7 9 11 11 11 15 15 19 19 19 19 19 19 19 19 19 19
19 19 19 19 19 19 19 19 57 59 59 59 59 59 59 59 59 59 77 79
81 83 85 87 89 91 91 91 91 91 91 91 91 91 91 91 91 91 115 115
121 123 125 127 127 127 127 127 137 139 141 143 145 147 147 147 147 147 147 147
147 147 147 147 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167
167 167 167 167 209 211 211 211 211 211 221 223 223 223 223 223 223 223 223 223
223 223 243 243 243 243 243 243 257 259 261 263 263 263 263 263 263 263 263 263
263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263
263 263 325 327 329 331 331 331 331 331 331 331 331 331 349 351 351 351 351 351
361 363 363 363 363 363 363 363 363 363 363 363 363 363 363 363 363 363 363 363
It is unclear (to me at least) how one might predict future terms. It only feels safe to say that that the terms are always odd, non-decreasing (after the second term), and that some numbers are repeated lots of times.
Challenge
Write a program or function that takes in a positive integer n and prints or returns the nth term of the SUDSI sequence. For example, if n is 1, the output is 1
, if n is 2, the output is 3
, if n is 200, the output is 363
.
Take input in any usual way (stdin/command line/function arg).
The shortest answer in bytes wins.
(That site encodes things in UTF-8, but you may use any darn existing encoding you want.)
Mathy bonus: (potentially eligible for bounty)
- Tell me more about the SUDSI sequence. What's the underlying pattern to what numbers are part of it and how many of them there are (and stuff like that)? (I couldn't find SUDSI on OEIS by the way.)
As again . Better not link than creating confusion about encoding. – Optimizer – 2015-03-25T08:54:46.940
@Optimizer I've been linking to that byte counter with the same phrasing for ages. Why would it suddenly cause more confusion than it ever did? – Calvin's Hobbies – 2015-03-25T08:59:16.270
I have no idea that you have been doing this from ages. But think about it, if someone links the count from that url for an APL answer, it will definitely be wrong. – Optimizer – 2015-03-25T09:09:32.207
@Optimizer Well hopefully APL users know about that caveat. (And I've never had one ask.) Providing a general purpose byte counter seems like a reasonable thing to do. – Calvin's Hobbies – 2015-03-25T09:22:04.957
@Calvin'sHobbies I think a better byte counter would be some HTML5 file form thingy where you can drag and drop a (binary, APL, UTF8, whatever-you-want-encoding) file. – orlp – 2015-03-25T11:15:04.400
1@orlp I guess that would be a nice additional feature, but I personally rely on being able to copy-paste since I rarely have source files for my submissions. – Martin Ender – 2015-03-25T11:21:31.613
1@orlp But who will need that anyway? They can see the size directly if they had the file. And it is not that easy to remove the newline in the end in some operating systems. – jimmy23013 – 2015-03-25T12:50:03.290
2
@MartinBüttner I was bored: http://meta.codegolf.stackexchange.com/questions/4944/byte-counter-snippet
– orlp – 2015-03-25T13:51:22.060