# Rotate a number

Given a positive number n, rotate its base-10 digits m positions rightward. That is, output the result of m steps of moving the last digit to the start. The rotation count m will be a non-negative integer.

You should remove leading zeroes in the final result, but not in any of the intermediate steps. For example, for the test case 100,2 => 1, we first rotate to 010, then to 001, then finally drop the leading zeroes to get 1.

## Tests

n,m => Output

123,1 => 312
123,2 => 231
123,3 => 123
123,4 => 312
1,637 => 1
10,1 => 1
100,2 => 1
10,2 => 10
110,2 => 101
123,0 => 123
9998,2 => 9899

• I've edited the post (i.e. added some formatting/CGCC terms) to help make it even more understandable. Nice first challenge! Aug 15, 2020 at 4:25
• The test cases suggest this loops around for big n, which isn't clear from the text.
– xnor
Aug 15, 2020 at 4:50
• You should indicate in the text that the rotation is to the right Aug 15, 2020 at 12:49
• @Shaggy Hm I guess "moving the last digit to the start" is clear enough Aug 15, 2020 at 16:25
• From the test cases, it seems the input number can be base 4 or any higher base, to handle digits up to 3? Power-of-2 bases are much more efficient and convenient to work with in binary computers, e.g. hardware rotate instructions, and bit-shifts. e.g. x86 add ecx,ecx / ror eax, cl rotates by n 2-bit digits, in 4 bytes of machine code. Nothing in the question actually says you have to rotate base-10 digits, which would be inconvenient if you get input as an int or something. But I suspect you meant that? Aug 16, 2020 at 8:08

# V (vim), 11 bytes

Àñ$x0Pñó^0«  Try it online! Àñ ñ # (M-@)rg number of times$           # end of line
x          # delete character (cut)
0         # beginning of line
P        # paste character
ó      # (M-s)ubsitute
^0«   # ^0\+
# (implicitly) with nothing


f n m=read(foldr(\_ y->last y:init y)(show n)[1..m])::Int


Try it online!

f n m=            - function expecting two integers

foldr ... [1..m]  - folds m times..
.. (show n)  - starting with n converted to string
. (\_ y->last y:init y)  - moving the last element in head
read( ... )::Int  - convert the result to Int


With n and m in columns a and b, and this formula in column c

=VALUE(RIGHT(A4, MOD(B4,LEN(A4)))&LEFT(A4,LEN(A4)-MOD(B4,LEN(A4


• These should get you to 47: 1. Put LEN(A1) in C1 and MOD(B1,C1) in D1. 2. Instead of VALUE(), use --() if you need a number. Aug 18, 2020 at 13:57

# SimpleTemplate 0.84, 106 bytes

This challenge really got my head spinning...

{@fnR N,T}{@forfrom Tto1}{@ifN matches"@(.+)(.)@"M}{@setN"#{M.2}#{M.1}"}{@/}{@/}{@set+N N,0}{@returnN}{@/}


This creates a function R that takes the Number and the amount of Times you want to swap the numbers around.

The way it works is by evaluating if the Number matches a regular expression ({@ifN matches"@(.+)(.)@"M}) and storing the Matched parts.

Then, it reconstructs the number with the last digit at the beginning ({@setN"#{M.2}#{M.1}"}).

To remove leading zeroes, I've simply stored the sum of whatever is the Number with 0, returning it after.

Ungolfed:

It is a lot easier to follow an ungolfed example:

{@fn rotate_n number, times}
{@for i from times to 1}
{@if number matches "@^(.+)(.)$@" matches} {@set number "#{matches.2}#{matches.1}"} {@/} {@/} {@set+ number number, 0} {@return number} {@/}  The i is totally useless there, but removing it is a golfing step. You can change the values on line 1071. # Golfscript - 28 bytes ~:a;{1/)\+}a*1/{(~:b!}do b\  • input: 10450 4 • output: 4501 Try it online! Explanation ~:a; # Parses the input "123 3" -> "123" on stack and a = 3 {1/)\+}a* # Pushes the last digit away and rejoins, repeat a times "123" -> "12" "3" -> "3" "12" -> "312" repeat 1/{(~:b!}do b\ # Discard the leading elements until a non-zero element is found  There's room for improvement. # K (ngn/k), 29 25 bytes {.(y#a),(y:-(#a)!y)_a:$x}


Try it online!

¯4 thanks to @ovs

• $ for 10\ and . for 10/ saves a couple bytes, and you can go a bit shorter by doing some arithmetic on the indices with {a@ ... !n:#a:$x}
– ovs
Apr 18, 2022 at 9:26

# Vyxal, 3 bytes

ǔṅ⌊


# PHP, 89 bytes

list($n,$m)=explode(",",$argn);while($m--){$n=$n[-1].substr($n,0,-1);}echo ltrim($n,"0");


Try it online!

Explanation: A PHP answer that works by looping m times through the given string n moving the last character of n to the beginning of the string.

# K (ngn/k), 18 bytes

{.y{(*|x):':x}/$x}  Try it online! Right rotate inspired by @chrispsn. # Desmos, 76 70 bytes f(n,m)=mod(n,t)10^d/t+floor(n/t) d=floor(log(n+0^n))+1 t=10^{mod(m,d)}  Try it on Desmos! Port of R answer. -6 bytes thanks to Aiden Chow # GolfScript, 14 bytes ~\\{1/)\+}\*~  Try it online! ~ # Puts n and m on top of the stack as integers \\ # Parses n to string {1/)\+} # This block goes to the top of the stack without being executed \* # Executes previous block m times ~ # Parses the answer from string to integer, this removes the leading zeroes  What the block does:  1/ # Parses the string to an array of strings )\ # Separates the last digit and puts it in the begining + # Concatenates them again  • The last backslash is unnecessary. Apr 18, 2022 at 2:31 # Factor, 19 bytes [ neg rotate dec> ]  Attempt This Online! # Lua 5.1 (58 bytes) n=n..""for _=1,m do n=n:sub(-1)..n:sub(1,-2)end print(n*1)  # Thunno 2, 2 bytes ẒN  Try it online! #### Explanation  # Implicit input Ẓ # Rotate right that many times N # Convert to integer to remove leading zeros # Implicit output  ## awk awk '($++NF=(__=$++_)*(_!_)^((__=length(__))-$(_+_)%__)%((_!_)^__-_--))' FS=,


123 1 => 312 312
123 2 => 231 231
123 3 => 123 123
123 4 => 312 312
1 637 => 1 1
10 1 => 1 1
100 2 => 1 1
10 2 => 10  10
110 2 => 101 101
123 0 => 123 123
9998 2 => 9899 9899


# Uiua, 6 bytes

⍜⊙°⋕↻¯