RTA (Reverse-Then-Add) root of a number
Clash Royale CLAN TAG#URR8PPP
up vote
21
down vote
favorite
The reverse-then-add (RTA) sequence is a sequence obtained by adding a number to its reverse, and repeating the process on the result. For eg.,
$$ 5 + 5 = 10 Rightarrow 10 + 01 = 11 Rightarrow 11 + 11 = 22 Rightarrow 22 + 22 = 44 Rightarrowtext ... $$
Thus, 5's RTA sequence contains 10, 11, 22, 44, 88, 176, etc.
The RTA root of a number $n$ is the smallest number that is either equal to $n$ or gives raise to $n$ in its RTA sequence.
For eg., 44 is found in the RTA sequence of 5, 10, 11, 13, 22, 31, etc. Of these, 5 is the smallest, and hence RTAroot(44) = 5.
72 is not part of any number's RTA sequence, and so is considered its own RTA root.
Input is a positive integer in a range that your language can naturally handle.
Output is the RTA root of the given number, as defined above.
Test cases
Input
Output
44
5
72
72
132
3
143
49
1111
1
999
999
Related OEIS: A067031. The output will be a number from this sequence.
code-golf math number
add a comment |Â
up vote
21
down vote
favorite
The reverse-then-add (RTA) sequence is a sequence obtained by adding a number to its reverse, and repeating the process on the result. For eg.,
$$ 5 + 5 = 10 Rightarrow 10 + 01 = 11 Rightarrow 11 + 11 = 22 Rightarrow 22 + 22 = 44 Rightarrowtext ... $$
Thus, 5's RTA sequence contains 10, 11, 22, 44, 88, 176, etc.
The RTA root of a number $n$ is the smallest number that is either equal to $n$ or gives raise to $n$ in its RTA sequence.
For eg., 44 is found in the RTA sequence of 5, 10, 11, 13, 22, 31, etc. Of these, 5 is the smallest, and hence RTAroot(44) = 5.
72 is not part of any number's RTA sequence, and so is considered its own RTA root.
Input is a positive integer in a range that your language can naturally handle.
Output is the RTA root of the given number, as defined above.
Test cases
Input
Output
44
5
72
72
132
3
143
49
1111
1
999
999
Related OEIS: A067031. The output will be a number from this sequence.
code-golf math number
add a comment |Â
up vote
21
down vote
favorite
up vote
21
down vote
favorite
The reverse-then-add (RTA) sequence is a sequence obtained by adding a number to its reverse, and repeating the process on the result. For eg.,
$$ 5 + 5 = 10 Rightarrow 10 + 01 = 11 Rightarrow 11 + 11 = 22 Rightarrow 22 + 22 = 44 Rightarrowtext ... $$
Thus, 5's RTA sequence contains 10, 11, 22, 44, 88, 176, etc.
The RTA root of a number $n$ is the smallest number that is either equal to $n$ or gives raise to $n$ in its RTA sequence.
For eg., 44 is found in the RTA sequence of 5, 10, 11, 13, 22, 31, etc. Of these, 5 is the smallest, and hence RTAroot(44) = 5.
72 is not part of any number's RTA sequence, and so is considered its own RTA root.
Input is a positive integer in a range that your language can naturally handle.
Output is the RTA root of the given number, as defined above.
Test cases
Input
Output
44
5
72
72
132
3
143
49
1111
1
999
999
Related OEIS: A067031. The output will be a number from this sequence.
code-golf math number
The reverse-then-add (RTA) sequence is a sequence obtained by adding a number to its reverse, and repeating the process on the result. For eg.,
$$ 5 + 5 = 10 Rightarrow 10 + 01 = 11 Rightarrow 11 + 11 = 22 Rightarrow 22 + 22 = 44 Rightarrowtext ... $$
Thus, 5's RTA sequence contains 10, 11, 22, 44, 88, 176, etc.
The RTA root of a number $n$ is the smallest number that is either equal to $n$ or gives raise to $n$ in its RTA sequence.
For eg., 44 is found in the RTA sequence of 5, 10, 11, 13, 22, 31, etc. Of these, 5 is the smallest, and hence RTAroot(44) = 5.
72 is not part of any number's RTA sequence, and so is considered its own RTA root.
Input is a positive integer in a range that your language can naturally handle.
Output is the RTA root of the given number, as defined above.
Test cases
Input
Output
44
5
72
72
132
3
143
49
1111
1
999
999
Related OEIS: A067031. The output will be a number from this sequence.
code-golf math number
code-golf math number
asked Aug 11 at 12:56
sundar
4,656829
4,656829
add a comment |Â
add a comment |Â
17 Answers
17
active
oldest
votes
up vote
12
down vote
Perl 6, 45 bytes
first 1..$^a: $aâÂÂ($_,$_+.flip...*>$a)
Try it online!
Explanation:
# Anonymous code block
first # Find the first element
1..$^a: # In the range 1 to the given number
# That satisfies
$aâ # The given number is an element of
( # A sequence defined by
$_, # The first element is the number we're checking
$_+.flip # Each element is the previous element plus the reverse
...*>$a # The last element is above the given number
) # This is the RTA sequence of the number
5
The Perl 6 ellipsis syntax gets more magical every time I come across it. That lambda-based sequence specification is such a neat idea!
â sundar
Aug 11 at 17:06
@sundar, that syntax was actually one of the main reasons why I came over to Perl 6. (and why, after some time, it became my most favorite language)
â Ramillies
Aug 12 at 14:11
add a comment |Â
up vote
7
down vote
Brachylog, 24 22 bytes
~âÂÂâ¤.&âÂÂâÂÂ;?+ᶠâÂÂ
- 2 bytes thanks to sundar noticing that I had a
and
Explanation
-- f(n):
-- g(x):
-- h(y):
~ -- get z where k(z) = y
-- k(z):
âÂÂâ¤. -- z>=0 and z<=k(z) (constrain so it doesn't keep looking)
&â -- label input (avoiding infinite stuff)
âÂÂ;?+ -- return z+reverse(z)
--
--
--
--
ᶠ-- get all possible answers for g(n)
â -- return smallest of them
sorry for the wonky explanation, this is the best i could come up with
Try it online!
1
The use ofthere is simple but brilliant. Good work!
â sundar
Aug 11 at 16:40
add a comment |Â
up vote
5
down vote
Haskell, 59 57 bytes
-2 bytes thanks to user1472751 (using a second until
instead of list-comprehension & head
)!
f n=until((n==).until(>=n)((+)<*>read.reverse.show))(+1)1
Try it online!
Explanation
This will evaluate to True
for any RTA-root:
(n==) . until (n<=) ((+)<*>read.reverse.show)
The term (+)<*>read.reverse.show
is a golfed version of
r-> r + read (reverse $ show r)
which adds a number to itself reversed.
The function until
repeatedly applies (+)<*>read.reverse.show
until it exceeds our target.
Wrapping all of this in yet another until
starting off with 1
and adding 1 with (+1)
will find the first RTA-root.
If there is no proper RTA-root of n
, we eventually get to n
where until
doesn't apply the function since n<=n
.
1
You can save 2 bytes by usinguntil
for the outer loop as well: TIO
â user1472751
Aug 14 at 19:06
add a comment |Â
up vote
4
down vote
05AB1E, 7 bytes
Using the new version of 05AB1E (rewritten in Elixir).
Code
L.ÃÂûjÃÂ+
Try it online!
Explanation
L # Create the list [1, ..., input]
.ÃÂ # Iterate over each value and return the first value that returns a truthy value for:
û # Where the base case is the current value, compute the following sequence:
ÃÂ+ # Pop a(n - 1) and bifurcate (duplicate and reverse duplicate) and sum them up.
# This gives us: a(0) = value, a(n) = a(n - 1) + reversed(a(n - 1))
j # A û-generator with the 'j' flag, which pops a value (in this case the input)
# and check whether the value exists in the sequence. Since these sequences will be
# infinitely long, this will only work strictly non-decreasing lists.
add a comment |Â
up vote
3
down vote
Jelly, 12 11 bytes
á¹Âá¸Â+ÃÂÃÂáâ¬Ã
ÂiùḢ
This is a full program. Run time is roughly quadratic; test cases $999$ and $1111$ time out on TIO.
Thanks to @JonathanAllan for golfing off 1 byte!
Try it online!
How it works
á¹Âá¸Â+ÃÂÃÂáâ¬Ã
ÂiùḢ Main link. Argument: n
⬠Map the link to the left over [1, ..., n].
ÃÂá For each k, call the link to the left n times. Return the array of k
and the link's n return values.
ÃÂ Combine the three links to the left into a monadic link. Argument: j
á¹ Promote j to its digit array and reverse it.
ḠUndecimal; convert the resulting digit array to integer.
+ Add the result to j.
Ã
Âiù Find the first multindimensional index of n.
Ḣ Head; extract the first coordinate.
add a comment |Â
up vote
3
down vote
Ruby, 66 57 bytes
f=->n(1..n).mapm+(m.digits*'').to_i==n ?f[m]:n.min
Try it online!
Recursive function that repeatedly "undoes" the RTA operation until arriving at a number that can't be produced by it, then returns the minimum.
Instead of using filter
, which is long, I instead simply map
over the range from 1 to the number. For each m in this range, if m + rev(m) is the number, it calls the function recursively on m; otherwise, it returns n. This both removes the need for a filter
and gives us a base case of f(n) = n for free.
Highlights include saving a byte with Integer#digits
:
m.to_s.reverse.to_i
(m.digits*'').to_i
eval(m.digits*'')
The last one would be a byte shorter, but sadly, Ruby parses numbers starting with 0
as octal.
add a comment |Â
up vote
2
down vote
Python 2, 70 bytes
f=lambda n,i=1,k=1:i*(k==n)or f(n,i+(k>n),[k+int(`k`[::-1]),i+1][k>n])
Try it online!
add a comment |Â
up vote
2
down vote
Pyth, 12 bytes
fqQ.W<HQ+s_`
Check out a test suite!
Surprisingly fast and efficient. All the test cases ran at once take less than 2 seconds.
How it works
fqQ.W<HQ+s_` â Full program. Q is the variable that represents the input.
f â Find the first positive integer T that satisfies a function.
.W â Functional while. This is an operator that takes two functions A(H)
and B(Z) and while A(H) is truthy, H = B(Z). Initial value T.
<HQ â First function, A(H) â Condition: H is strictly less than Q.
+s_` â Second function, B(Z) â Modifier.
s_` â Reverse the string representation of Z and treat it as an integer.
+ â Add it to Z.
â It should be noted that .W, functional while, returns the ending
value only. In other words ".W<HQ+s_`" can be interpreted as
"Starting with T, while the current value is less than Q, add it
to its reverse, and yield the final value after the loop ends".
qQ â Check if the result equals Q.
add a comment |Â
up vote
2
down vote
05AB1E, 13 bytes
LÃÂIFDÃÂ+})IÃÂ¥}ý
Try it online!
Explanation
L # push range [1 ... input]
ÃÂ } # filter, keep elements that are true under:
IF } # input times do:
D # duplicate
ÃÂ+ # add current number and its reverse
) # wrap in a list
IÃÂ¥ # check if input is in the list
ý # get the first (smallest) one
Smart! I know my 21 bytes version was already way too long (which I've golfed to 16 with the same approach), but couldn't really figure out a way to do it shorter. Can't believe I haven't thought about using head after the filter.. I kept trying to use the loop index+1, or theglobal_counter
.. >.>
â Kevin Cruijssen
Aug 12 at 14:40
add a comment |Â
up vote
2
down vote
JavaScript (ES6), 61 bytes
n=>(g=k=>k-n?g(k>n?++x:+[...k+''].reverse().join``+k):x)(x=1)
Try it online!
Commented
n => // n = input
(g = k => // g() = recursive function taking k = current value
k - n ? // if k is not equal to n:
g( // do a recursive call:
k > n ? // if k is greater than n:
++x // increment the RTA root x and restart from there
: // else (k is less than n):
+[...k + ''] // split k into a list of digit characters
.reverse().join`` // reverse, join and coerce it back to an integer
+ k // add k
) // end of recursive call
: // else (k = n):
x // success: return the RTA root
)(x = 1) // initial call to g() with k = x = 1
add a comment |Â
up vote
2
down vote
05AB1E, 21 16 15 bytes
GüNùFÃÂ+ÃÂùQiþq]ù
-1 byte thanks to @Emigna.
Try it online.
Explanation:
G # Loop `N` in the range [1, input):
ü # Increase the global_counter by 1 first every iteration (0 by default)
N # Push `N` to the stack as starting value for the inner-loop
ùF # Inner loop an input amount of times
ÃÂ # Bifurcate (short for Duplicate & Reverse) the current value
# i.e. 10 â 10 and '01'
+ # Add them together
# i.e. 10 and '01' â 11
ÃÂ # Triplicate that value
# (one for the check below; one for the next iteration)
ùQi # If it's equal to the input:
þ # Push the global_counter
q # And terminate the program
# (after which the global_counter is implicitly printed to STDOUT)
] # After all loops, if nothing was output yet:
ù # Output the input
You don't need the print due to implicit printing.
â Emigna
Aug 13 at 7:27
add a comment |Â
up vote
1
down vote
Charcoal, 33 bytes
NøâÂÂâÂÂø÷Wâº÷øëâÂÂLâÂÂOÃÂ
ÃÂ÷Wâ¹÷øâ§âºIâ®ÂI÷÷ûILÃÂ
Try it online! Link is to verbose version of code. Explanation:
Nø
Input $q$.
âÂÂâÂÂø÷
Assign $2q$ to $h$ so that the loop starts.
ï¼·âº÷øë
Repeat while $h>q$:
âÂÂLâÂÂOÃÂ
ÃÂ÷
push a dummy null string to $u$ thus increasing its length, and assign the resulting length to $h$;
ï¼·â¹÷ø
repeat while $h<q$:
â§âºIâ®ÂI÷÷
add the reverse of $h$ to $h$.
ûILÃÂ
Print the final length of $u$ which is the desired root.
add a comment |Â
up vote
1
down vote
MATL, 17 bytes
`@G:"ttVPU+]vG-}@
Try it online!
Explanation
` % Do...while loop
@ % Push iteration index, k (starting at 1)
G:" % Do as many times as the input
tt % Duplicate twice
VPU % To string, reverse, to number
+ % Add
] % End
v % Concatenate all stack into a column vector. This vector contains
% a sufficient number of terms of k's RTA sequence
G- % Subtract input. This is used as loop condition, which is falsy
% if some entry is zero, indicating that we have found the input
% in k's RTA sequence
} % Finally (execute on loop exit)
@ % Push current k
% End (implicit). Display (implicit)
1
Just as a side note, I used MATL to generate the test case outputs, using this 31 byte version::!`tG=~yV2&PU*+tG>~*tXzG=A~]f1)
Try it online!
â sundar
Aug 12 at 12:12
add a comment |Â
up vote
1
down vote
Java 8, 103 bytes
n->for(int i=0,j;;)for(j=++i;j<=n;j+=n.valueOf(new StringBuffer(j+"").reverse()+""))if(n==j)return i;
Try it online.
Explanation:
n-> // Method with Integer as both parameter and return-type
for(int i=0,j;;) // Infinite loop `i`, starting at 0
for(j=++i; // Increase `i` by 1 first, and then set `j` to this new `i`
j<=n // Inner loop as long as `j` is smaller than or equal to the input
; // After every iteration:
j+= // Increase `j` by:
n.valueOf(new StringBuffer(j+"").reverse()+""))
// `j` reversed
if(n==j) // If the input and `j` are equal:
return i; // Return `i` as result
Arithmetically reversing the integer is 1 byte longer (104 bytes):
n->for(int i=0,j,t,r;;)for(j=++i;j<=n;)for(t=j,r=0;t>0;t/=10)r=r*10+t%10;if((j+=r)==n
Try it online.
add a comment |Â
up vote
1
down vote
C (gcc), 120 100 99 bytes
f(i,o,a,b,c,d)for(a=o=i;b=a;o=i/b?a:o,a--)for(;b<i;b+=c)for(c=0,d=b;d;d/=10)c=c*10+d%10;return o;
Try it online!
Given input i
, checks every integer from i
to 0 for a sequence containing i
.
i
is the input valueo
is the output value (the minimum root found so far)a
is the current integer being checkedb
is the current element ofa
's sequencec
andd
are used to addb
to its reverse
Compiling with-DL=for
would save you 2 bytes.
â Rogem
Aug 13 at 9:44
Scratch that; doing math wrong.
â Rogem
Aug 13 at 10:00
However, you can return the output value withi=o;
if you use-O0
, saving you 5 bytes.
â Rogem
Aug 13 at 10:05
add a comment |Â
up vote
0
down vote
Physica, 57 bytes
Credit for the method goes to Doorknob.
F=>N:Min@Map[->m:N==m+Int[Str[m]%%-1]&&F@m||N;â¦[1;N]]
Try it online!
add a comment |Â
up vote
0
down vote
C (gcc), 89 bytes
I run each sequence in [1,n) until I get a match; zero is special-cased because it doesn't terminate.
j,k,l,m;r(i)for(j=k=0;k-i&&++j<i;)for(k=j;k<i;k+=m)for(l=k,m=0;l;l/=10)m=m*10+l%10;j=j;
Try it online!
add a comment |Â
17 Answers
17
active
oldest
votes
17 Answers
17
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
12
down vote
Perl 6, 45 bytes
first 1..$^a: $aâÂÂ($_,$_+.flip...*>$a)
Try it online!
Explanation:
# Anonymous code block
first # Find the first element
1..$^a: # In the range 1 to the given number
# That satisfies
$aâ # The given number is an element of
( # A sequence defined by
$_, # The first element is the number we're checking
$_+.flip # Each element is the previous element plus the reverse
...*>$a # The last element is above the given number
) # This is the RTA sequence of the number
5
The Perl 6 ellipsis syntax gets more magical every time I come across it. That lambda-based sequence specification is such a neat idea!
â sundar
Aug 11 at 17:06
@sundar, that syntax was actually one of the main reasons why I came over to Perl 6. (and why, after some time, it became my most favorite language)
â Ramillies
Aug 12 at 14:11
add a comment |Â
up vote
12
down vote
Perl 6, 45 bytes
first 1..$^a: $aâÂÂ($_,$_+.flip...*>$a)
Try it online!
Explanation:
# Anonymous code block
first # Find the first element
1..$^a: # In the range 1 to the given number
# That satisfies
$aâ # The given number is an element of
( # A sequence defined by
$_, # The first element is the number we're checking
$_+.flip # Each element is the previous element plus the reverse
...*>$a # The last element is above the given number
) # This is the RTA sequence of the number
5
The Perl 6 ellipsis syntax gets more magical every time I come across it. That lambda-based sequence specification is such a neat idea!
â sundar
Aug 11 at 17:06
@sundar, that syntax was actually one of the main reasons why I came over to Perl 6. (and why, after some time, it became my most favorite language)
â Ramillies
Aug 12 at 14:11
add a comment |Â
up vote
12
down vote
up vote
12
down vote
Perl 6, 45 bytes
first 1..$^a: $aâÂÂ($_,$_+.flip...*>$a)
Try it online!
Explanation:
# Anonymous code block
first # Find the first element
1..$^a: # In the range 1 to the given number
# That satisfies
$aâ # The given number is an element of
( # A sequence defined by
$_, # The first element is the number we're checking
$_+.flip # Each element is the previous element plus the reverse
...*>$a # The last element is above the given number
) # This is the RTA sequence of the number
Perl 6, 45 bytes
first 1..$^a: $aâÂÂ($_,$_+.flip...*>$a)
Try it online!
Explanation:
# Anonymous code block
first # Find the first element
1..$^a: # In the range 1 to the given number
# That satisfies
$aâ # The given number is an element of
( # A sequence defined by
$_, # The first element is the number we're checking
$_+.flip # Each element is the previous element plus the reverse
...*>$a # The last element is above the given number
) # This is the RTA sequence of the number
edited Aug 11 at 13:28
answered Aug 11 at 13:10
Jo King
15.4k24087
15.4k24087
5
The Perl 6 ellipsis syntax gets more magical every time I come across it. That lambda-based sequence specification is such a neat idea!
â sundar
Aug 11 at 17:06
@sundar, that syntax was actually one of the main reasons why I came over to Perl 6. (and why, after some time, it became my most favorite language)
â Ramillies
Aug 12 at 14:11
add a comment |Â
5
The Perl 6 ellipsis syntax gets more magical every time I come across it. That lambda-based sequence specification is such a neat idea!
â sundar
Aug 11 at 17:06
@sundar, that syntax was actually one of the main reasons why I came over to Perl 6. (and why, after some time, it became my most favorite language)
â Ramillies
Aug 12 at 14:11
5
5
The Perl 6 ellipsis syntax gets more magical every time I come across it. That lambda-based sequence specification is such a neat idea!
â sundar
Aug 11 at 17:06
The Perl 6 ellipsis syntax gets more magical every time I come across it. That lambda-based sequence specification is such a neat idea!
â sundar
Aug 11 at 17:06
@sundar, that syntax was actually one of the main reasons why I came over to Perl 6. (and why, after some time, it became my most favorite language)
â Ramillies
Aug 12 at 14:11
@sundar, that syntax was actually one of the main reasons why I came over to Perl 6. (and why, after some time, it became my most favorite language)
â Ramillies
Aug 12 at 14:11
add a comment |Â
up vote
7
down vote
Brachylog, 24 22 bytes
~âÂÂâ¤.&âÂÂâÂÂ;?+ᶠâÂÂ
- 2 bytes thanks to sundar noticing that I had a
and
Explanation
-- f(n):
-- g(x):
-- h(y):
~ -- get z where k(z) = y
-- k(z):
âÂÂâ¤. -- z>=0 and z<=k(z) (constrain so it doesn't keep looking)
&â -- label input (avoiding infinite stuff)
âÂÂ;?+ -- return z+reverse(z)
--
--
--
--
ᶠ-- get all possible answers for g(n)
â -- return smallest of them
sorry for the wonky explanation, this is the best i could come up with
Try it online!
1
The use ofthere is simple but brilliant. Good work!
â sundar
Aug 11 at 16:40
add a comment |Â
up vote
7
down vote
Brachylog, 24 22 bytes
~âÂÂâ¤.&âÂÂâÂÂ;?+ᶠâÂÂ
- 2 bytes thanks to sundar noticing that I had a
and
Explanation
-- f(n):
-- g(x):
-- h(y):
~ -- get z where k(z) = y
-- k(z):
âÂÂâ¤. -- z>=0 and z<=k(z) (constrain so it doesn't keep looking)
&â -- label input (avoiding infinite stuff)
âÂÂ;?+ -- return z+reverse(z)
--
--
--
--
ᶠ-- get all possible answers for g(n)
â -- return smallest of them
sorry for the wonky explanation, this is the best i could come up with
Try it online!
1
The use ofthere is simple but brilliant. Good work!
â sundar
Aug 11 at 16:40
add a comment |Â
up vote
7
down vote
up vote
7
down vote
Brachylog, 24 22 bytes
~âÂÂâ¤.&âÂÂâÂÂ;?+ᶠâÂÂ
- 2 bytes thanks to sundar noticing that I had a
and
Explanation
-- f(n):
-- g(x):
-- h(y):
~ -- get z where k(z) = y
-- k(z):
âÂÂâ¤. -- z>=0 and z<=k(z) (constrain so it doesn't keep looking)
&â -- label input (avoiding infinite stuff)
âÂÂ;?+ -- return z+reverse(z)
--
--
--
--
ᶠ-- get all possible answers for g(n)
â -- return smallest of them
sorry for the wonky explanation, this is the best i could come up with
Try it online!
Brachylog, 24 22 bytes
~âÂÂâ¤.&âÂÂâÂÂ;?+ᶠâÂÂ
- 2 bytes thanks to sundar noticing that I had a
and
Explanation
-- f(n):
-- g(x):
-- h(y):
~ -- get z where k(z) = y
-- k(z):
âÂÂâ¤. -- z>=0 and z<=k(z) (constrain so it doesn't keep looking)
&â -- label input (avoiding infinite stuff)
âÂÂ;?+ -- return z+reverse(z)
--
--
--
--
ᶠ-- get all possible answers for g(n)
â -- return smallest of them
sorry for the wonky explanation, this is the best i could come up with
Try it online!
edited Aug 11 at 17:17
answered Aug 11 at 14:09
Kroppeb
93628
93628
1
The use ofthere is simple but brilliant. Good work!
â sundar
Aug 11 at 16:40
add a comment |Â
1
The use ofthere is simple but brilliant. Good work!
â sundar
Aug 11 at 16:40
1
1
The use of
there is simple but brilliant. Good work!â sundar
Aug 11 at 16:40
The use of
there is simple but brilliant. Good work!â sundar
Aug 11 at 16:40
add a comment |Â
up vote
5
down vote
Haskell, 59 57 bytes
-2 bytes thanks to user1472751 (using a second until
instead of list-comprehension & head
)!
f n=until((n==).until(>=n)((+)<*>read.reverse.show))(+1)1
Try it online!
Explanation
This will evaluate to True
for any RTA-root:
(n==) . until (n<=) ((+)<*>read.reverse.show)
The term (+)<*>read.reverse.show
is a golfed version of
r-> r + read (reverse $ show r)
which adds a number to itself reversed.
The function until
repeatedly applies (+)<*>read.reverse.show
until it exceeds our target.
Wrapping all of this in yet another until
starting off with 1
and adding 1 with (+1)
will find the first RTA-root.
If there is no proper RTA-root of n
, we eventually get to n
where until
doesn't apply the function since n<=n
.
1
You can save 2 bytes by usinguntil
for the outer loop as well: TIO
â user1472751
Aug 14 at 19:06
add a comment |Â
up vote
5
down vote
Haskell, 59 57 bytes
-2 bytes thanks to user1472751 (using a second until
instead of list-comprehension & head
)!
f n=until((n==).until(>=n)((+)<*>read.reverse.show))(+1)1
Try it online!
Explanation
This will evaluate to True
for any RTA-root:
(n==) . until (n<=) ((+)<*>read.reverse.show)
The term (+)<*>read.reverse.show
is a golfed version of
r-> r + read (reverse $ show r)
which adds a number to itself reversed.
The function until
repeatedly applies (+)<*>read.reverse.show
until it exceeds our target.
Wrapping all of this in yet another until
starting off with 1
and adding 1 with (+1)
will find the first RTA-root.
If there is no proper RTA-root of n
, we eventually get to n
where until
doesn't apply the function since n<=n
.
1
You can save 2 bytes by usinguntil
for the outer loop as well: TIO
â user1472751
Aug 14 at 19:06
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Haskell, 59 57 bytes
-2 bytes thanks to user1472751 (using a second until
instead of list-comprehension & head
)!
f n=until((n==).until(>=n)((+)<*>read.reverse.show))(+1)1
Try it online!
Explanation
This will evaluate to True
for any RTA-root:
(n==) . until (n<=) ((+)<*>read.reverse.show)
The term (+)<*>read.reverse.show
is a golfed version of
r-> r + read (reverse $ show r)
which adds a number to itself reversed.
The function until
repeatedly applies (+)<*>read.reverse.show
until it exceeds our target.
Wrapping all of this in yet another until
starting off with 1
and adding 1 with (+1)
will find the first RTA-root.
If there is no proper RTA-root of n
, we eventually get to n
where until
doesn't apply the function since n<=n
.
Haskell, 59 57 bytes
-2 bytes thanks to user1472751 (using a second until
instead of list-comprehension & head
)!
f n=until((n==).until(>=n)((+)<*>read.reverse.show))(+1)1
Try it online!
Explanation
This will evaluate to True
for any RTA-root:
(n==) . until (n<=) ((+)<*>read.reverse.show)
The term (+)<*>read.reverse.show
is a golfed version of
r-> r + read (reverse $ show r)
which adds a number to itself reversed.
The function until
repeatedly applies (+)<*>read.reverse.show
until it exceeds our target.
Wrapping all of this in yet another until
starting off with 1
and adding 1 with (+1)
will find the first RTA-root.
If there is no proper RTA-root of n
, we eventually get to n
where until
doesn't apply the function since n<=n
.
edited Aug 14 at 20:06
answered Aug 11 at 13:35
BWO
9,41511570
9,41511570
1
You can save 2 bytes by usinguntil
for the outer loop as well: TIO
â user1472751
Aug 14 at 19:06
add a comment |Â
1
You can save 2 bytes by usinguntil
for the outer loop as well: TIO
â user1472751
Aug 14 at 19:06
1
1
You can save 2 bytes by using
until
for the outer loop as well: TIOâ user1472751
Aug 14 at 19:06
You can save 2 bytes by using
until
for the outer loop as well: TIOâ user1472751
Aug 14 at 19:06
add a comment |Â
up vote
4
down vote
05AB1E, 7 bytes
Using the new version of 05AB1E (rewritten in Elixir).
Code
L.ÃÂûjÃÂ+
Try it online!
Explanation
L # Create the list [1, ..., input]
.ÃÂ # Iterate over each value and return the first value that returns a truthy value for:
û # Where the base case is the current value, compute the following sequence:
ÃÂ+ # Pop a(n - 1) and bifurcate (duplicate and reverse duplicate) and sum them up.
# This gives us: a(0) = value, a(n) = a(n - 1) + reversed(a(n - 1))
j # A û-generator with the 'j' flag, which pops a value (in this case the input)
# and check whether the value exists in the sequence. Since these sequences will be
# infinitely long, this will only work strictly non-decreasing lists.
add a comment |Â
up vote
4
down vote
05AB1E, 7 bytes
Using the new version of 05AB1E (rewritten in Elixir).
Code
L.ÃÂûjÃÂ+
Try it online!
Explanation
L # Create the list [1, ..., input]
.ÃÂ # Iterate over each value and return the first value that returns a truthy value for:
û # Where the base case is the current value, compute the following sequence:
ÃÂ+ # Pop a(n - 1) and bifurcate (duplicate and reverse duplicate) and sum them up.
# This gives us: a(0) = value, a(n) = a(n - 1) + reversed(a(n - 1))
j # A û-generator with the 'j' flag, which pops a value (in this case the input)
# and check whether the value exists in the sequence. Since these sequences will be
# infinitely long, this will only work strictly non-decreasing lists.
add a comment |Â
up vote
4
down vote
up vote
4
down vote
05AB1E, 7 bytes
Using the new version of 05AB1E (rewritten in Elixir).
Code
L.ÃÂûjÃÂ+
Try it online!
Explanation
L # Create the list [1, ..., input]
.ÃÂ # Iterate over each value and return the first value that returns a truthy value for:
û # Where the base case is the current value, compute the following sequence:
ÃÂ+ # Pop a(n - 1) and bifurcate (duplicate and reverse duplicate) and sum them up.
# This gives us: a(0) = value, a(n) = a(n - 1) + reversed(a(n - 1))
j # A û-generator with the 'j' flag, which pops a value (in this case the input)
# and check whether the value exists in the sequence. Since these sequences will be
# infinitely long, this will only work strictly non-decreasing lists.
05AB1E, 7 bytes
Using the new version of 05AB1E (rewritten in Elixir).
Code
L.ÃÂûjÃÂ+
Try it online!
Explanation
L # Create the list [1, ..., input]
.ÃÂ # Iterate over each value and return the first value that returns a truthy value for:
û # Where the base case is the current value, compute the following sequence:
ÃÂ+ # Pop a(n - 1) and bifurcate (duplicate and reverse duplicate) and sum them up.
# This gives us: a(0) = value, a(n) = a(n - 1) + reversed(a(n - 1))
j # A û-generator with the 'j' flag, which pops a value (in this case the input)
# and check whether the value exists in the sequence. Since these sequences will be
# infinitely long, this will only work strictly non-decreasing lists.
answered Aug 16 at 21:56
Adnan
35.2k461223
35.2k461223
add a comment |Â
add a comment |Â
up vote
3
down vote
Jelly, 12 11 bytes
á¹Âá¸Â+ÃÂÃÂáâ¬Ã
ÂiùḢ
This is a full program. Run time is roughly quadratic; test cases $999$ and $1111$ time out on TIO.
Thanks to @JonathanAllan for golfing off 1 byte!
Try it online!
How it works
á¹Âá¸Â+ÃÂÃÂáâ¬Ã
ÂiùḢ Main link. Argument: n
⬠Map the link to the left over [1, ..., n].
ÃÂá For each k, call the link to the left n times. Return the array of k
and the link's n return values.
ÃÂ Combine the three links to the left into a monadic link. Argument: j
á¹ Promote j to its digit array and reverse it.
ḠUndecimal; convert the resulting digit array to integer.
+ Add the result to j.
Ã
Âiù Find the first multindimensional index of n.
Ḣ Head; extract the first coordinate.
add a comment |Â
up vote
3
down vote
Jelly, 12 11 bytes
á¹Âá¸Â+ÃÂÃÂáâ¬Ã
ÂiùḢ
This is a full program. Run time is roughly quadratic; test cases $999$ and $1111$ time out on TIO.
Thanks to @JonathanAllan for golfing off 1 byte!
Try it online!
How it works
á¹Âá¸Â+ÃÂÃÂáâ¬Ã
ÂiùḢ Main link. Argument: n
⬠Map the link to the left over [1, ..., n].
ÃÂá For each k, call the link to the left n times. Return the array of k
and the link's n return values.
ÃÂ Combine the three links to the left into a monadic link. Argument: j
á¹ Promote j to its digit array and reverse it.
ḠUndecimal; convert the resulting digit array to integer.
+ Add the result to j.
Ã
Âiù Find the first multindimensional index of n.
Ḣ Head; extract the first coordinate.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Jelly, 12 11 bytes
á¹Âá¸Â+ÃÂÃÂáâ¬Ã
ÂiùḢ
This is a full program. Run time is roughly quadratic; test cases $999$ and $1111$ time out on TIO.
Thanks to @JonathanAllan for golfing off 1 byte!
Try it online!
How it works
á¹Âá¸Â+ÃÂÃÂáâ¬Ã
ÂiùḢ Main link. Argument: n
⬠Map the link to the left over [1, ..., n].
ÃÂá For each k, call the link to the left n times. Return the array of k
and the link's n return values.
ÃÂ Combine the three links to the left into a monadic link. Argument: j
á¹ Promote j to its digit array and reverse it.
ḠUndecimal; convert the resulting digit array to integer.
+ Add the result to j.
Ã
Âiù Find the first multindimensional index of n.
Ḣ Head; extract the first coordinate.
Jelly, 12 11 bytes
á¹Âá¸Â+ÃÂÃÂáâ¬Ã
ÂiùḢ
This is a full program. Run time is roughly quadratic; test cases $999$ and $1111$ time out on TIO.
Thanks to @JonathanAllan for golfing off 1 byte!
Try it online!
How it works
á¹Âá¸Â+ÃÂÃÂáâ¬Ã
ÂiùḢ Main link. Argument: n
⬠Map the link to the left over [1, ..., n].
ÃÂá For each k, call the link to the left n times. Return the array of k
and the link's n return values.
ÃÂ Combine the three links to the left into a monadic link. Argument: j
á¹ Promote j to its digit array and reverse it.
ḠUndecimal; convert the resulting digit array to integer.
+ Add the result to j.
Ã
Âiù Find the first multindimensional index of n.
Ḣ Head; extract the first coordinate.
edited Aug 11 at 15:20
answered Aug 11 at 14:13
Dennisâ¦
182k32292724
182k32292724
add a comment |Â
add a comment |Â
up vote
3
down vote
Ruby, 66 57 bytes
f=->n(1..n).mapm+(m.digits*'').to_i==n ?f[m]:n.min
Try it online!
Recursive function that repeatedly "undoes" the RTA operation until arriving at a number that can't be produced by it, then returns the minimum.
Instead of using filter
, which is long, I instead simply map
over the range from 1 to the number. For each m in this range, if m + rev(m) is the number, it calls the function recursively on m; otherwise, it returns n. This both removes the need for a filter
and gives us a base case of f(n) = n for free.
Highlights include saving a byte with Integer#digits
:
m.to_s.reverse.to_i
(m.digits*'').to_i
eval(m.digits*'')
The last one would be a byte shorter, but sadly, Ruby parses numbers starting with 0
as octal.
add a comment |Â
up vote
3
down vote
Ruby, 66 57 bytes
f=->n(1..n).mapm+(m.digits*'').to_i==n ?f[m]:n.min
Try it online!
Recursive function that repeatedly "undoes" the RTA operation until arriving at a number that can't be produced by it, then returns the minimum.
Instead of using filter
, which is long, I instead simply map
over the range from 1 to the number. For each m in this range, if m + rev(m) is the number, it calls the function recursively on m; otherwise, it returns n. This both removes the need for a filter
and gives us a base case of f(n) = n for free.
Highlights include saving a byte with Integer#digits
:
m.to_s.reverse.to_i
(m.digits*'').to_i
eval(m.digits*'')
The last one would be a byte shorter, but sadly, Ruby parses numbers starting with 0
as octal.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Ruby, 66 57 bytes
f=->n(1..n).mapm+(m.digits*'').to_i==n ?f[m]:n.min
Try it online!
Recursive function that repeatedly "undoes" the RTA operation until arriving at a number that can't be produced by it, then returns the minimum.
Instead of using filter
, which is long, I instead simply map
over the range from 1 to the number. For each m in this range, if m + rev(m) is the number, it calls the function recursively on m; otherwise, it returns n. This both removes the need for a filter
and gives us a base case of f(n) = n for free.
Highlights include saving a byte with Integer#digits
:
m.to_s.reverse.to_i
(m.digits*'').to_i
eval(m.digits*'')
The last one would be a byte shorter, but sadly, Ruby parses numbers starting with 0
as octal.
Ruby, 66 57 bytes
f=->n(1..n).mapm+(m.digits*'').to_i==n ?f[m]:n.min
Try it online!
Recursive function that repeatedly "undoes" the RTA operation until arriving at a number that can't be produced by it, then returns the minimum.
Instead of using filter
, which is long, I instead simply map
over the range from 1 to the number. For each m in this range, if m + rev(m) is the number, it calls the function recursively on m; otherwise, it returns n. This both removes the need for a filter
and gives us a base case of f(n) = n for free.
Highlights include saving a byte with Integer#digits
:
m.to_s.reverse.to_i
(m.digits*'').to_i
eval(m.digits*'')
The last one would be a byte shorter, but sadly, Ruby parses numbers starting with 0
as octal.
edited Aug 11 at 15:26
answered Aug 11 at 15:20
Doorknobâ¦
53.5k16111339
53.5k16111339
add a comment |Â
add a comment |Â
up vote
2
down vote
Python 2, 70 bytes
f=lambda n,i=1,k=1:i*(k==n)or f(n,i+(k>n),[k+int(`k`[::-1]),i+1][k>n])
Try it online!
add a comment |Â
up vote
2
down vote
Python 2, 70 bytes
f=lambda n,i=1,k=1:i*(k==n)or f(n,i+(k>n),[k+int(`k`[::-1]),i+1][k>n])
Try it online!
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Python 2, 70 bytes
f=lambda n,i=1,k=1:i*(k==n)or f(n,i+(k>n),[k+int(`k`[::-1]),i+1][k>n])
Try it online!
Python 2, 70 bytes
f=lambda n,i=1,k=1:i*(k==n)or f(n,i+(k>n),[k+int(`k`[::-1]),i+1][k>n])
Try it online!
answered Aug 11 at 16:14
ovs
17.4k21057
17.4k21057
add a comment |Â
add a comment |Â
up vote
2
down vote
Pyth, 12 bytes
fqQ.W<HQ+s_`
Check out a test suite!
Surprisingly fast and efficient. All the test cases ran at once take less than 2 seconds.
How it works
fqQ.W<HQ+s_` â Full program. Q is the variable that represents the input.
f â Find the first positive integer T that satisfies a function.
.W â Functional while. This is an operator that takes two functions A(H)
and B(Z) and while A(H) is truthy, H = B(Z). Initial value T.
<HQ â First function, A(H) â Condition: H is strictly less than Q.
+s_` â Second function, B(Z) â Modifier.
s_` â Reverse the string representation of Z and treat it as an integer.
+ â Add it to Z.
â It should be noted that .W, functional while, returns the ending
value only. In other words ".W<HQ+s_`" can be interpreted as
"Starting with T, while the current value is less than Q, add it
to its reverse, and yield the final value after the loop ends".
qQ â Check if the result equals Q.
add a comment |Â
up vote
2
down vote
Pyth, 12 bytes
fqQ.W<HQ+s_`
Check out a test suite!
Surprisingly fast and efficient. All the test cases ran at once take less than 2 seconds.
How it works
fqQ.W<HQ+s_` â Full program. Q is the variable that represents the input.
f â Find the first positive integer T that satisfies a function.
.W â Functional while. This is an operator that takes two functions A(H)
and B(Z) and while A(H) is truthy, H = B(Z). Initial value T.
<HQ â First function, A(H) â Condition: H is strictly less than Q.
+s_` â Second function, B(Z) â Modifier.
s_` â Reverse the string representation of Z and treat it as an integer.
+ â Add it to Z.
â It should be noted that .W, functional while, returns the ending
value only. In other words ".W<HQ+s_`" can be interpreted as
"Starting with T, while the current value is less than Q, add it
to its reverse, and yield the final value after the loop ends".
qQ â Check if the result equals Q.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Pyth, 12 bytes
fqQ.W<HQ+s_`
Check out a test suite!
Surprisingly fast and efficient. All the test cases ran at once take less than 2 seconds.
How it works
fqQ.W<HQ+s_` â Full program. Q is the variable that represents the input.
f â Find the first positive integer T that satisfies a function.
.W â Functional while. This is an operator that takes two functions A(H)
and B(Z) and while A(H) is truthy, H = B(Z). Initial value T.
<HQ â First function, A(H) â Condition: H is strictly less than Q.
+s_` â Second function, B(Z) â Modifier.
s_` â Reverse the string representation of Z and treat it as an integer.
+ â Add it to Z.
â It should be noted that .W, functional while, returns the ending
value only. In other words ".W<HQ+s_`" can be interpreted as
"Starting with T, while the current value is less than Q, add it
to its reverse, and yield the final value after the loop ends".
qQ â Check if the result equals Q.
Pyth, 12 bytes
fqQ.W<HQ+s_`
Check out a test suite!
Surprisingly fast and efficient. All the test cases ran at once take less than 2 seconds.
How it works
fqQ.W<HQ+s_` â Full program. Q is the variable that represents the input.
f â Find the first positive integer T that satisfies a function.
.W â Functional while. This is an operator that takes two functions A(H)
and B(Z) and while A(H) is truthy, H = B(Z). Initial value T.
<HQ â First function, A(H) â Condition: H is strictly less than Q.
+s_` â Second function, B(Z) â Modifier.
s_` â Reverse the string representation of Z and treat it as an integer.
+ â Add it to Z.
â It should be noted that .W, functional while, returns the ending
value only. In other words ".W<HQ+s_`" can be interpreted as
"Starting with T, while the current value is less than Q, add it
to its reverse, and yield the final value after the loop ends".
qQ â Check if the result equals Q.
edited Aug 11 at 21:04
answered Aug 11 at 14:18
Mr. Xcoder
30.4k758193
30.4k758193
add a comment |Â
add a comment |Â
up vote
2
down vote
05AB1E, 13 bytes
LÃÂIFDÃÂ+})IÃÂ¥}ý
Try it online!
Explanation
L # push range [1 ... input]
ÃÂ } # filter, keep elements that are true under:
IF } # input times do:
D # duplicate
ÃÂ+ # add current number and its reverse
) # wrap in a list
IÃÂ¥ # check if input is in the list
ý # get the first (smallest) one
Smart! I know my 21 bytes version was already way too long (which I've golfed to 16 with the same approach), but couldn't really figure out a way to do it shorter. Can't believe I haven't thought about using head after the filter.. I kept trying to use the loop index+1, or theglobal_counter
.. >.>
â Kevin Cruijssen
Aug 12 at 14:40
add a comment |Â
up vote
2
down vote
05AB1E, 13 bytes
LÃÂIFDÃÂ+})IÃÂ¥}ý
Try it online!
Explanation
L # push range [1 ... input]
ÃÂ } # filter, keep elements that are true under:
IF } # input times do:
D # duplicate
ÃÂ+ # add current number and its reverse
) # wrap in a list
IÃÂ¥ # check if input is in the list
ý # get the first (smallest) one
Smart! I know my 21 bytes version was already way too long (which I've golfed to 16 with the same approach), but couldn't really figure out a way to do it shorter. Can't believe I haven't thought about using head after the filter.. I kept trying to use the loop index+1, or theglobal_counter
.. >.>
â Kevin Cruijssen
Aug 12 at 14:40
add a comment |Â
up vote
2
down vote
up vote
2
down vote
05AB1E, 13 bytes
LÃÂIFDÃÂ+})IÃÂ¥}ý
Try it online!
Explanation
L # push range [1 ... input]
ÃÂ } # filter, keep elements that are true under:
IF } # input times do:
D # duplicate
ÃÂ+ # add current number and its reverse
) # wrap in a list
IÃÂ¥ # check if input is in the list
ý # get the first (smallest) one
05AB1E, 13 bytes
LÃÂIFDÃÂ+})IÃÂ¥}ý
Try it online!
Explanation
L # push range [1 ... input]
ÃÂ } # filter, keep elements that are true under:
IF } # input times do:
D # duplicate
ÃÂ+ # add current number and its reverse
) # wrap in a list
IÃÂ¥ # check if input is in the list
ý # get the first (smallest) one
answered Aug 12 at 13:42
Emigna
43k431131
43k431131
Smart! I know my 21 bytes version was already way too long (which I've golfed to 16 with the same approach), but couldn't really figure out a way to do it shorter. Can't believe I haven't thought about using head after the filter.. I kept trying to use the loop index+1, or theglobal_counter
.. >.>
â Kevin Cruijssen
Aug 12 at 14:40
add a comment |Â
Smart! I know my 21 bytes version was already way too long (which I've golfed to 16 with the same approach), but couldn't really figure out a way to do it shorter. Can't believe I haven't thought about using head after the filter.. I kept trying to use the loop index+1, or theglobal_counter
.. >.>
â Kevin Cruijssen
Aug 12 at 14:40
Smart! I know my 21 bytes version was already way too long (which I've golfed to 16 with the same approach), but couldn't really figure out a way to do it shorter. Can't believe I haven't thought about using head after the filter.. I kept trying to use the loop index+1, or the
global_counter
.. >.>â Kevin Cruijssen
Aug 12 at 14:40
Smart! I know my 21 bytes version was already way too long (which I've golfed to 16 with the same approach), but couldn't really figure out a way to do it shorter. Can't believe I haven't thought about using head after the filter.. I kept trying to use the loop index+1, or the
global_counter
.. >.>â Kevin Cruijssen
Aug 12 at 14:40
add a comment |Â
up vote
2
down vote
JavaScript (ES6), 61 bytes
n=>(g=k=>k-n?g(k>n?++x:+[...k+''].reverse().join``+k):x)(x=1)
Try it online!
Commented
n => // n = input
(g = k => // g() = recursive function taking k = current value
k - n ? // if k is not equal to n:
g( // do a recursive call:
k > n ? // if k is greater than n:
++x // increment the RTA root x and restart from there
: // else (k is less than n):
+[...k + ''] // split k into a list of digit characters
.reverse().join`` // reverse, join and coerce it back to an integer
+ k // add k
) // end of recursive call
: // else (k = n):
x // success: return the RTA root
)(x = 1) // initial call to g() with k = x = 1
add a comment |Â
up vote
2
down vote
JavaScript (ES6), 61 bytes
n=>(g=k=>k-n?g(k>n?++x:+[...k+''].reverse().join``+k):x)(x=1)
Try it online!
Commented
n => // n = input
(g = k => // g() = recursive function taking k = current value
k - n ? // if k is not equal to n:
g( // do a recursive call:
k > n ? // if k is greater than n:
++x // increment the RTA root x and restart from there
: // else (k is less than n):
+[...k + ''] // split k into a list of digit characters
.reverse().join`` // reverse, join and coerce it back to an integer
+ k // add k
) // end of recursive call
: // else (k = n):
x // success: return the RTA root
)(x = 1) // initial call to g() with k = x = 1
add a comment |Â
up vote
2
down vote
up vote
2
down vote
JavaScript (ES6), 61 bytes
n=>(g=k=>k-n?g(k>n?++x:+[...k+''].reverse().join``+k):x)(x=1)
Try it online!
Commented
n => // n = input
(g = k => // g() = recursive function taking k = current value
k - n ? // if k is not equal to n:
g( // do a recursive call:
k > n ? // if k is greater than n:
++x // increment the RTA root x and restart from there
: // else (k is less than n):
+[...k + ''] // split k into a list of digit characters
.reverse().join`` // reverse, join and coerce it back to an integer
+ k // add k
) // end of recursive call
: // else (k = n):
x // success: return the RTA root
)(x = 1) // initial call to g() with k = x = 1
JavaScript (ES6), 61 bytes
n=>(g=k=>k-n?g(k>n?++x:+[...k+''].reverse().join``+k):x)(x=1)
Try it online!
Commented
n => // n = input
(g = k => // g() = recursive function taking k = current value
k - n ? // if k is not equal to n:
g( // do a recursive call:
k > n ? // if k is greater than n:
++x // increment the RTA root x and restart from there
: // else (k is less than n):
+[...k + ''] // split k into a list of digit characters
.reverse().join`` // reverse, join and coerce it back to an integer
+ k // add k
) // end of recursive call
: // else (k = n):
x // success: return the RTA root
)(x = 1) // initial call to g() with k = x = 1
edited Aug 12 at 15:23
answered Aug 11 at 14:07
Arnauld
64.2k580270
64.2k580270
add a comment |Â
add a comment |Â
up vote
2
down vote
05AB1E, 21 16 15 bytes
GüNùFÃÂ+ÃÂùQiþq]ù
-1 byte thanks to @Emigna.
Try it online.
Explanation:
G # Loop `N` in the range [1, input):
ü # Increase the global_counter by 1 first every iteration (0 by default)
N # Push `N` to the stack as starting value for the inner-loop
ùF # Inner loop an input amount of times
ÃÂ # Bifurcate (short for Duplicate & Reverse) the current value
# i.e. 10 â 10 and '01'
+ # Add them together
# i.e. 10 and '01' â 11
ÃÂ # Triplicate that value
# (one for the check below; one for the next iteration)
ùQi # If it's equal to the input:
þ # Push the global_counter
q # And terminate the program
# (after which the global_counter is implicitly printed to STDOUT)
] # After all loops, if nothing was output yet:
ù # Output the input
You don't need the print due to implicit printing.
â Emigna
Aug 13 at 7:27
add a comment |Â
up vote
2
down vote
05AB1E, 21 16 15 bytes
GüNùFÃÂ+ÃÂùQiþq]ù
-1 byte thanks to @Emigna.
Try it online.
Explanation:
G # Loop `N` in the range [1, input):
ü # Increase the global_counter by 1 first every iteration (0 by default)
N # Push `N` to the stack as starting value for the inner-loop
ùF # Inner loop an input amount of times
ÃÂ # Bifurcate (short for Duplicate & Reverse) the current value
# i.e. 10 â 10 and '01'
+ # Add them together
# i.e. 10 and '01' â 11
ÃÂ # Triplicate that value
# (one for the check below; one for the next iteration)
ùQi # If it's equal to the input:
þ # Push the global_counter
q # And terminate the program
# (after which the global_counter is implicitly printed to STDOUT)
] # After all loops, if nothing was output yet:
ù # Output the input
You don't need the print due to implicit printing.
â Emigna
Aug 13 at 7:27
add a comment |Â
up vote
2
down vote
up vote
2
down vote
05AB1E, 21 16 15 bytes
GüNùFÃÂ+ÃÂùQiþq]ù
-1 byte thanks to @Emigna.
Try it online.
Explanation:
G # Loop `N` in the range [1, input):
ü # Increase the global_counter by 1 first every iteration (0 by default)
N # Push `N` to the stack as starting value for the inner-loop
ùF # Inner loop an input amount of times
ÃÂ # Bifurcate (short for Duplicate & Reverse) the current value
# i.e. 10 â 10 and '01'
+ # Add them together
# i.e. 10 and '01' â 11
ÃÂ # Triplicate that value
# (one for the check below; one for the next iteration)
ùQi # If it's equal to the input:
þ # Push the global_counter
q # And terminate the program
# (after which the global_counter is implicitly printed to STDOUT)
] # After all loops, if nothing was output yet:
ù # Output the input
05AB1E, 21 16 15 bytes
GüNùFÃÂ+ÃÂùQiþq]ù
-1 byte thanks to @Emigna.
Try it online.
Explanation:
G # Loop `N` in the range [1, input):
ü # Increase the global_counter by 1 first every iteration (0 by default)
N # Push `N` to the stack as starting value for the inner-loop
ùF # Inner loop an input amount of times
ÃÂ # Bifurcate (short for Duplicate & Reverse) the current value
# i.e. 10 â 10 and '01'
+ # Add them together
# i.e. 10 and '01' â 11
ÃÂ # Triplicate that value
# (one for the check below; one for the next iteration)
ùQi # If it's equal to the input:
þ # Push the global_counter
q # And terminate the program
# (after which the global_counter is implicitly printed to STDOUT)
] # After all loops, if nothing was output yet:
ù # Output the input
edited Aug 13 at 7:30
answered Aug 11 at 19:23
Kevin Cruijssen
30k552165
30k552165
You don't need the print due to implicit printing.
â Emigna
Aug 13 at 7:27
add a comment |Â
You don't need the print due to implicit printing.
â Emigna
Aug 13 at 7:27
You don't need the print due to implicit printing.
â Emigna
Aug 13 at 7:27
You don't need the print due to implicit printing.
â Emigna
Aug 13 at 7:27
add a comment |Â
up vote
1
down vote
Charcoal, 33 bytes
NøâÂÂâÂÂø÷Wâº÷øëâÂÂLâÂÂOÃÂ
ÃÂ÷Wâ¹÷øâ§âºIâ®ÂI÷÷ûILÃÂ
Try it online! Link is to verbose version of code. Explanation:
Nø
Input $q$.
âÂÂâÂÂø÷
Assign $2q$ to $h$ so that the loop starts.
ï¼·âº÷øë
Repeat while $h>q$:
âÂÂLâÂÂOÃÂ
ÃÂ÷
push a dummy null string to $u$ thus increasing its length, and assign the resulting length to $h$;
ï¼·â¹÷ø
repeat while $h<q$:
â§âºIâ®ÂI÷÷
add the reverse of $h$ to $h$.
ûILÃÂ
Print the final length of $u$ which is the desired root.
add a comment |Â
up vote
1
down vote
Charcoal, 33 bytes
NøâÂÂâÂÂø÷Wâº÷øëâÂÂLâÂÂOÃÂ
ÃÂ÷Wâ¹÷øâ§âºIâ®ÂI÷÷ûILÃÂ
Try it online! Link is to verbose version of code. Explanation:
Nø
Input $q$.
âÂÂâÂÂø÷
Assign $2q$ to $h$ so that the loop starts.
ï¼·âº÷øë
Repeat while $h>q$:
âÂÂLâÂÂOÃÂ
ÃÂ÷
push a dummy null string to $u$ thus increasing its length, and assign the resulting length to $h$;
ï¼·â¹÷ø
repeat while $h<q$:
â§âºIâ®ÂI÷÷
add the reverse of $h$ to $h$.
ûILÃÂ
Print the final length of $u$ which is the desired root.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Charcoal, 33 bytes
NøâÂÂâÂÂø÷Wâº÷øëâÂÂLâÂÂOÃÂ
ÃÂ÷Wâ¹÷øâ§âºIâ®ÂI÷÷ûILÃÂ
Try it online! Link is to verbose version of code. Explanation:
Nø
Input $q$.
âÂÂâÂÂø÷
Assign $2q$ to $h$ so that the loop starts.
ï¼·âº÷øë
Repeat while $h>q$:
âÂÂLâÂÂOÃÂ
ÃÂ÷
push a dummy null string to $u$ thus increasing its length, and assign the resulting length to $h$;
ï¼·â¹÷ø
repeat while $h<q$:
â§âºIâ®ÂI÷÷
add the reverse of $h$ to $h$.
ûILÃÂ
Print the final length of $u$ which is the desired root.
Charcoal, 33 bytes
NøâÂÂâÂÂø÷Wâº÷øëâÂÂLâÂÂOÃÂ
ÃÂ÷Wâ¹÷øâ§âºIâ®ÂI÷÷ûILÃÂ
Try it online! Link is to verbose version of code. Explanation:
Nø
Input $q$.
âÂÂâÂÂø÷
Assign $2q$ to $h$ so that the loop starts.
ï¼·âº÷øë
Repeat while $h>q$:
âÂÂLâÂÂOÃÂ
ÃÂ÷
push a dummy null string to $u$ thus increasing its length, and assign the resulting length to $h$;
ï¼·â¹÷ø
repeat while $h<q$:
â§âºIâ®ÂI÷÷
add the reverse of $h$ to $h$.
ûILÃÂ
Print the final length of $u$ which is the desired root.
answered Aug 11 at 15:38
Neil
75.2k744170
75.2k744170
add a comment |Â
add a comment |Â
up vote
1
down vote
MATL, 17 bytes
`@G:"ttVPU+]vG-}@
Try it online!
Explanation
` % Do...while loop
@ % Push iteration index, k (starting at 1)
G:" % Do as many times as the input
tt % Duplicate twice
VPU % To string, reverse, to number
+ % Add
] % End
v % Concatenate all stack into a column vector. This vector contains
% a sufficient number of terms of k's RTA sequence
G- % Subtract input. This is used as loop condition, which is falsy
% if some entry is zero, indicating that we have found the input
% in k's RTA sequence
} % Finally (execute on loop exit)
@ % Push current k
% End (implicit). Display (implicit)
1
Just as a side note, I used MATL to generate the test case outputs, using this 31 byte version::!`tG=~yV2&PU*+tG>~*tXzG=A~]f1)
Try it online!
â sundar
Aug 12 at 12:12
add a comment |Â
up vote
1
down vote
MATL, 17 bytes
`@G:"ttVPU+]vG-}@
Try it online!
Explanation
` % Do...while loop
@ % Push iteration index, k (starting at 1)
G:" % Do as many times as the input
tt % Duplicate twice
VPU % To string, reverse, to number
+ % Add
] % End
v % Concatenate all stack into a column vector. This vector contains
% a sufficient number of terms of k's RTA sequence
G- % Subtract input. This is used as loop condition, which is falsy
% if some entry is zero, indicating that we have found the input
% in k's RTA sequence
} % Finally (execute on loop exit)
@ % Push current k
% End (implicit). Display (implicit)
1
Just as a side note, I used MATL to generate the test case outputs, using this 31 byte version::!`tG=~yV2&PU*+tG>~*tXzG=A~]f1)
Try it online!
â sundar
Aug 12 at 12:12
add a comment |Â
up vote
1
down vote
up vote
1
down vote
MATL, 17 bytes
`@G:"ttVPU+]vG-}@
Try it online!
Explanation
` % Do...while loop
@ % Push iteration index, k (starting at 1)
G:" % Do as many times as the input
tt % Duplicate twice
VPU % To string, reverse, to number
+ % Add
] % End
v % Concatenate all stack into a column vector. This vector contains
% a sufficient number of terms of k's RTA sequence
G- % Subtract input. This is used as loop condition, which is falsy
% if some entry is zero, indicating that we have found the input
% in k's RTA sequence
} % Finally (execute on loop exit)
@ % Push current k
% End (implicit). Display (implicit)
MATL, 17 bytes
`@G:"ttVPU+]vG-}@
Try it online!
Explanation
` % Do...while loop
@ % Push iteration index, k (starting at 1)
G:" % Do as many times as the input
tt % Duplicate twice
VPU % To string, reverse, to number
+ % Add
] % End
v % Concatenate all stack into a column vector. This vector contains
% a sufficient number of terms of k's RTA sequence
G- % Subtract input. This is used as loop condition, which is falsy
% if some entry is zero, indicating that we have found the input
% in k's RTA sequence
} % Finally (execute on loop exit)
@ % Push current k
% End (implicit). Display (implicit)
edited Aug 12 at 2:33
answered Aug 12 at 2:26
Luis Mendo
72.6k885284
72.6k885284
1
Just as a side note, I used MATL to generate the test case outputs, using this 31 byte version::!`tG=~yV2&PU*+tG>~*tXzG=A~]f1)
Try it online!
â sundar
Aug 12 at 12:12
add a comment |Â
1
Just as a side note, I used MATL to generate the test case outputs, using this 31 byte version::!`tG=~yV2&PU*+tG>~*tXzG=A~]f1)
Try it online!
â sundar
Aug 12 at 12:12
1
1
Just as a side note, I used MATL to generate the test case outputs, using this 31 byte version:
:!`tG=~yV2&PU*+tG>~*tXzG=A~]f1)
Try it online!â sundar
Aug 12 at 12:12
Just as a side note, I used MATL to generate the test case outputs, using this 31 byte version:
:!`tG=~yV2&PU*+tG>~*tXzG=A~]f1)
Try it online!â sundar
Aug 12 at 12:12
add a comment |Â
up vote
1
down vote
Java 8, 103 bytes
n->for(int i=0,j;;)for(j=++i;j<=n;j+=n.valueOf(new StringBuffer(j+"").reverse()+""))if(n==j)return i;
Try it online.
Explanation:
n-> // Method with Integer as both parameter and return-type
for(int i=0,j;;) // Infinite loop `i`, starting at 0
for(j=++i; // Increase `i` by 1 first, and then set `j` to this new `i`
j<=n // Inner loop as long as `j` is smaller than or equal to the input
; // After every iteration:
j+= // Increase `j` by:
n.valueOf(new StringBuffer(j+"").reverse()+""))
// `j` reversed
if(n==j) // If the input and `j` are equal:
return i; // Return `i` as result
Arithmetically reversing the integer is 1 byte longer (104 bytes):
n->for(int i=0,j,t,r;;)for(j=++i;j<=n;)for(t=j,r=0;t>0;t/=10)r=r*10+t%10;if((j+=r)==n
Try it online.
add a comment |Â
up vote
1
down vote
Java 8, 103 bytes
n->for(int i=0,j;;)for(j=++i;j<=n;j+=n.valueOf(new StringBuffer(j+"").reverse()+""))if(n==j)return i;
Try it online.
Explanation:
n-> // Method with Integer as both parameter and return-type
for(int i=0,j;;) // Infinite loop `i`, starting at 0
for(j=++i; // Increase `i` by 1 first, and then set `j` to this new `i`
j<=n // Inner loop as long as `j` is smaller than or equal to the input
; // After every iteration:
j+= // Increase `j` by:
n.valueOf(new StringBuffer(j+"").reverse()+""))
// `j` reversed
if(n==j) // If the input and `j` are equal:
return i; // Return `i` as result
Arithmetically reversing the integer is 1 byte longer (104 bytes):
n->for(int i=0,j,t,r;;)for(j=++i;j<=n;)for(t=j,r=0;t>0;t/=10)r=r*10+t%10;if((j+=r)==n
Try it online.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Java 8, 103 bytes
n->for(int i=0,j;;)for(j=++i;j<=n;j+=n.valueOf(new StringBuffer(j+"").reverse()+""))if(n==j)return i;
Try it online.
Explanation:
n-> // Method with Integer as both parameter and return-type
for(int i=0,j;;) // Infinite loop `i`, starting at 0
for(j=++i; // Increase `i` by 1 first, and then set `j` to this new `i`
j<=n // Inner loop as long as `j` is smaller than or equal to the input
; // After every iteration:
j+= // Increase `j` by:
n.valueOf(new StringBuffer(j+"").reverse()+""))
// `j` reversed
if(n==j) // If the input and `j` are equal:
return i; // Return `i` as result
Arithmetically reversing the integer is 1 byte longer (104 bytes):
n->for(int i=0,j,t,r;;)for(j=++i;j<=n;)for(t=j,r=0;t>0;t/=10)r=r*10+t%10;if((j+=r)==n
Try it online.
Java 8, 103 bytes
n->for(int i=0,j;;)for(j=++i;j<=n;j+=n.valueOf(new StringBuffer(j+"").reverse()+""))if(n==j)return i;
Try it online.
Explanation:
n-> // Method with Integer as both parameter and return-type
for(int i=0,j;;) // Infinite loop `i`, starting at 0
for(j=++i; // Increase `i` by 1 first, and then set `j` to this new `i`
j<=n // Inner loop as long as `j` is smaller than or equal to the input
; // After every iteration:
j+= // Increase `j` by:
n.valueOf(new StringBuffer(j+"").reverse()+""))
// `j` reversed
if(n==j) // If the input and `j` are equal:
return i; // Return `i` as result
Arithmetically reversing the integer is 1 byte longer (104 bytes):
n->for(int i=0,j,t,r;;)for(j=++i;j<=n;)for(t=j,r=0;t>0;t/=10)r=r*10+t%10;if((j+=r)==n
Try it online.
edited Aug 13 at 11:57
answered Aug 13 at 10:01
Kevin Cruijssen
30k552165
30k552165
add a comment |Â
add a comment |Â
up vote
1
down vote
C (gcc), 120 100 99 bytes
f(i,o,a,b,c,d)for(a=o=i;b=a;o=i/b?a:o,a--)for(;b<i;b+=c)for(c=0,d=b;d;d/=10)c=c*10+d%10;return o;
Try it online!
Given input i
, checks every integer from i
to 0 for a sequence containing i
.
i
is the input valueo
is the output value (the minimum root found so far)a
is the current integer being checkedb
is the current element ofa
's sequencec
andd
are used to addb
to its reverse
Compiling with-DL=for
would save you 2 bytes.
â Rogem
Aug 13 at 9:44
Scratch that; doing math wrong.
â Rogem
Aug 13 at 10:00
However, you can return the output value withi=o;
if you use-O0
, saving you 5 bytes.
â Rogem
Aug 13 at 10:05
add a comment |Â
up vote
1
down vote
C (gcc), 120 100 99 bytes
f(i,o,a,b,c,d)for(a=o=i;b=a;o=i/b?a:o,a--)for(;b<i;b+=c)for(c=0,d=b;d;d/=10)c=c*10+d%10;return o;
Try it online!
Given input i
, checks every integer from i
to 0 for a sequence containing i
.
i
is the input valueo
is the output value (the minimum root found so far)a
is the current integer being checkedb
is the current element ofa
's sequencec
andd
are used to addb
to its reverse
Compiling with-DL=for
would save you 2 bytes.
â Rogem
Aug 13 at 9:44
Scratch that; doing math wrong.
â Rogem
Aug 13 at 10:00
However, you can return the output value withi=o;
if you use-O0
, saving you 5 bytes.
â Rogem
Aug 13 at 10:05
add a comment |Â
up vote
1
down vote
up vote
1
down vote
C (gcc), 120 100 99 bytes
f(i,o,a,b,c,d)for(a=o=i;b=a;o=i/b?a:o,a--)for(;b<i;b+=c)for(c=0,d=b;d;d/=10)c=c*10+d%10;return o;
Try it online!
Given input i
, checks every integer from i
to 0 for a sequence containing i
.
i
is the input valueo
is the output value (the minimum root found so far)a
is the current integer being checkedb
is the current element ofa
's sequencec
andd
are used to addb
to its reverse
C (gcc), 120 100 99 bytes
f(i,o,a,b,c,d)for(a=o=i;b=a;o=i/b?a:o,a--)for(;b<i;b+=c)for(c=0,d=b;d;d/=10)c=c*10+d%10;return o;
Try it online!
Given input i
, checks every integer from i
to 0 for a sequence containing i
.
i
is the input valueo
is the output value (the minimum root found so far)a
is the current integer being checkedb
is the current element ofa
's sequencec
andd
are used to addb
to its reverse
edited Aug 13 at 22:14
answered Aug 12 at 4:29
Curtis Bechtel
33618
33618
Compiling with-DL=for
would save you 2 bytes.
â Rogem
Aug 13 at 9:44
Scratch that; doing math wrong.
â Rogem
Aug 13 at 10:00
However, you can return the output value withi=o;
if you use-O0
, saving you 5 bytes.
â Rogem
Aug 13 at 10:05
add a comment |Â
Compiling with-DL=for
would save you 2 bytes.
â Rogem
Aug 13 at 9:44
Scratch that; doing math wrong.
â Rogem
Aug 13 at 10:00
However, you can return the output value withi=o;
if you use-O0
, saving you 5 bytes.
â Rogem
Aug 13 at 10:05
Compiling with
-DL=for
would save you 2 bytes.â Rogem
Aug 13 at 9:44
Compiling with
-DL=for
would save you 2 bytes.â Rogem
Aug 13 at 9:44
Scratch that; doing math wrong.
â Rogem
Aug 13 at 10:00
Scratch that; doing math wrong.
â Rogem
Aug 13 at 10:00
However, you can return the output value with
i=o;
if you use -O0
, saving you 5 bytes.â Rogem
Aug 13 at 10:05
However, you can return the output value with
i=o;
if you use -O0
, saving you 5 bytes.â Rogem
Aug 13 at 10:05
add a comment |Â
up vote
0
down vote
Physica, 57 bytes
Credit for the method goes to Doorknob.
F=>N:Min@Map[->m:N==m+Int[Str[m]%%-1]&&F@m||N;â¦[1;N]]
Try it online!
add a comment |Â
up vote
0
down vote
Physica, 57 bytes
Credit for the method goes to Doorknob.
F=>N:Min@Map[->m:N==m+Int[Str[m]%%-1]&&F@m||N;â¦[1;N]]
Try it online!
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Physica, 57 bytes
Credit for the method goes to Doorknob.
F=>N:Min@Map[->m:N==m+Int[Str[m]%%-1]&&F@m||N;â¦[1;N]]
Try it online!
Physica, 57 bytes
Credit for the method goes to Doorknob.
F=>N:Min@Map[->m:N==m+Int[Str[m]%%-1]&&F@m||N;â¦[1;N]]
Try it online!
edited Aug 12 at 7:59
answered Aug 11 at 22:11
Mr. Xcoder
30.4k758193
30.4k758193
add a comment |Â
add a comment |Â
up vote
0
down vote
C (gcc), 89 bytes
I run each sequence in [1,n) until I get a match; zero is special-cased because it doesn't terminate.
j,k,l,m;r(i)for(j=k=0;k-i&&++j<i;)for(k=j;k<i;k+=m)for(l=k,m=0;l;l/=10)m=m*10+l%10;j=j;
Try it online!
add a comment |Â
up vote
0
down vote
C (gcc), 89 bytes
I run each sequence in [1,n) until I get a match; zero is special-cased because it doesn't terminate.
j,k,l,m;r(i)for(j=k=0;k-i&&++j<i;)for(k=j;k<i;k+=m)for(l=k,m=0;l;l/=10)m=m*10+l%10;j=j;
Try it online!
add a comment |Â
up vote
0
down vote
up vote
0
down vote
C (gcc), 89 bytes
I run each sequence in [1,n) until I get a match; zero is special-cased because it doesn't terminate.
j,k,l,m;r(i)for(j=k=0;k-i&&++j<i;)for(k=j;k<i;k+=m)for(l=k,m=0;l;l/=10)m=m*10+l%10;j=j;
Try it online!
C (gcc), 89 bytes
I run each sequence in [1,n) until I get a match; zero is special-cased because it doesn't terminate.
j,k,l,m;r(i)for(j=k=0;k-i&&++j<i;)for(k=j;k<i;k+=m)for(l=k,m=0;l;l/=10)m=m*10+l%10;j=j;
Try it online!
answered Aug 12 at 13:41
ErikF
1,17917
1,17917
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f170469%2frta-reverse-then-add-root-of-a-number%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password