33
\$\begingroup\$

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.

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

33 Answers 33

1
2
1
\$\begingroup\$

Tcl, 91 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Japt, 9 bytes

1-indexed.

_+ìw)ìñ}g

Try it

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

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
    
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.