Kuznetsov's Sequence
PowerShell v2+, 89 bytes
param($n)for(){$r=-join"$n"["$n".length..0];if(!($n=(($r%$n),($n%$r))[$n-gt$r])){exit}$n}
Iterative solution. Lengthy because there's no easy way to reverse an array, so we stringify it and index on it backwards to store into $r
. Then a pseudo-ternary to pull out the appropriate modulo and re-store into $n
for the next round. However, if the result is zero, that means the !($n...)
will be $true
, so we exit
instead of $n
. The numbers are left on the pipeline and (implicitly) returned as an array, but without an encapsulating pipeline or saving the results into a variable, the default Write-Output
sticks a newline between.
Try it online! (Yes, dead serious.)
PowerShell is now on TIO! You gotta give it a second or two, because PowerShell is a beast to startup, but now you, yes you, can verify PowerShell code right in your browser!
Perl, 43 38 + 1 = 39 bytes
Run with the -n
flag
say while$_=($;=reverse)>$_?$;%$_:$_%$
Try it online! Includes the two non-empty examples.
Explanation chart
-n
: Wraps the entire program in while(<>){ ... ;}
. This turns the above code into the following line: while(<>){say while$_=($;=reverse)>$_?$;%$_:$_%$;}
. Notice, a semicolon has been added to the trailing $
, so it now becomes an instance of the variable $;
. In the condition of a while
loop, <>
automatically reads one line of input and saves it in the $_
variable. So now let's look at what the interpreter reads inside the outer while
loop:
say while$_=($;=reverse)>$_?$;%$_:$_%$;
[op][mod][ condition ] #While is acting as a statement modifier.
#It evaluates the operation as long as the condition is truthy.
($;=reverse)>$_?$;%$_:$_%$; #The meat of the program: a ternary operation
($;=reverse) #The reverse function takes $_ as a parameter by default, and reverses the value.
#The value returned by reverse is stored in the variable $;
>$_ #A condition asking if $% is greater than $_. Condition of the ternary operation
?$;%$_ #If true, then return $; modulo $_
:$_%$; #If false, return $_ modulo $;
$_= #Assign the result of the ternary operation back into $_
#If $_ is non-zero, then the condition is true, and while will evaluate the operation
say #Implicitly takes the $_ variable as parameter, and outputs its contents
Original code, saved for posterity: 43 + 1 = 44 bytes
say$_=$%>$_?$%%$_:$_%$%while$_-($%=reverse)
Pyth, 13 12 bytes
t.u|%F_S,s_`
Thanks to @TheBikingViking.
Try it online: Demonstration
My old code:
W
W=Q%F_S,s_`
Try it online: Demonstration
Explanation:
t.u|%F_S,s_`NNNQ implicit Ns and Q at the end
Q start with N = Q (Q = input number)
, create a pair with the numbers
s_`N convert N to string -> reverse-> convert to int
N and N
S sort
_ reverse
%F fold by modulo
| N or N (if the result is zero use N instead to stop)
.u apply this ^ procedure until a value repeats
print all intermediate values
t except the first one (the original number)