# The RATS sequence

Your task is to generate the nth term of the RATS sequence, where n is the input. The RATS sequence is also known as the Reverse Add Then Sort sequence. This sequence can also be found here: http://oeis.org/A004000.

test cases:

0 > 1
1 > 2
2 > 4
3 > 8
4 > 16
5 > 77
6 > 145
7 > 668


For example, the output for 5 is 77 because 16 + 61 = 77. After this the 77 is sorted.

Shortest submission wins. This is my first challenge so i hope this is not a duplicate or something.

• Does the input have to be an Integer or could it also be a string? – Denker Jan 31 '16 at 15:36
• @DenkerAffe do you mean a number in the form of a string? – justaprogrammer Jan 31 '16 at 15:37
• @justaprogrammer Yea, so I can get "123" instead of 123 as Integer. Would mayve save some bytes. – Denker Jan 31 '16 at 15:40
• isn't 77+77=154? Or have I missed something? EDIT: Oh, yes, I forgot to sort. – Denham Coote Feb 1 '16 at 10:48
• @DenhamCoote I think you meant "oh rats, I forgot to sort!" – Martin Ender Feb 2 '16 at 10:49

# Tcl, 91 bytes

proc R n {join [lsort [split [expr {$n?[set v [R [expr$n-1]]]+[string rev \$v]:1}] ""]] ""}


Try it online!

# Japt, 9 bytes

1-indexed.

_+ìw)ìñ}g


Try it

_+ìw)ìñ}g     :Implicit input of integer U
_             :Function taking an integer Z as input
ì           :    Convert Z to digit array
w          :   Reverse and convert back
ì        :  Convert result to digit array
ñ       :  Sort and convert back
}      :End function
g     :Starting with 1, do that U times


# Arn, 13 8 bytes

Ignore that it was previously 13 bytes, I never realized sorting doesn't cast back to strings when needed, so I fixed that.

'▀ƒʠBÀ½Ó


Try it!

# Explained

Unpacked: &.{:>+.<}1

&.            Mutate X N times via block B
{           B has key of _
:>        Sort ascending
_     Implied
+       Plus
_   Implied
.<    Reversed
}           End block
1             and X is the number one
_             and N is STDIN; implied