Skip to content

Instantly share code, notes, and snippets.

@peteeveleigh
Created November 16, 2018 17:55
Show Gist options
  • Save peteeveleigh/c305063840527968f26c8cf991f83ab1 to your computer and use it in GitHub Desktop.
Save peteeveleigh/c305063840527968f26c8cf991f83ab1 to your computer and use it in GitHub Desktop.
RATS sequence generator in TWIG
{#
http://mathworld.wolfram.com/RATSSequence.html
A sequence produced by the instructions "reverse, add to the original, then sort the digits." For example, after 668, the next iteration is given by
668+866=1534,
so the next term is 1345.
Applied to 1, the sequence gives 1, 2, 4, 8, 16, 77, 145, 668, 1345, 6677, 13444, 55778, 133345, 666677, 1333444, 5567777, 12333445, 66666677, 133333444, 556667777, 1233334444, 5566667777, 12333334444, 55666667777, 123333334444, 556666667777, 1233333334444,
#}
{% macro rats(start) %}
{% import _self as macros %}
{{-start-}}
{# prevent it looping forever #}
{% if start < 99999 %}
{{-macros.rats(((start + (start|reverse))|split('')|sort)|join(''))-}}
{% endif %}
{% endmacro %}
{% import _self as macros %}
{# and away we go #}
{{ macros.rats(1) }}
{#
result is
1 2 4 8 16 77 145 668 1345 6677 13444 55778 133345
#}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment