mod R is pr LIST{Nat}+ CONVERSION . ops k s : Nat Nat ~> Nat . op f : Nat
~> String . var N K : Nat . var X Y :[Nat]. eq k(N,0)= N . eq k(N,s K)=
k(rat(f(s(N,0 0 0 0 0 0 0 0 0 0)),10),K). eq s(0,X)= X . ceq s(N,X K Y)=
s(N quo 10,X(s K)Y)if size(X)= N rem 10[owise]. eq f(nil)= "" . eq f(X 0)=
f(X). eq f(X s N)= f(X)+ string(10 * s N + size(X),10). endm
The result is obtained by reducing the k
function with \$n\$ and \$k\$.
Example Session
Maude> red k(1, 10) . --- Expected: 41122314
result NzNat: 41122314
Maude> red k(1221, 0) . --- Expected: 1221
result NzNat: 1221
Maude> red k(1221, 1) . --- Expected: 2122
result NzNat: 2122
Maude> red k(1212, 1) . --- Expected: 2122
result NzNat: 2122
Maude> red k(912334, 1) . --- Expected: 1112231419
result NzNat: 1112231419
Maude> red k(912334, 3) . --- Expected: 412213141519
result NzNat: 412213141519
Maude> red k(21322314, 1) . --- Expected: 21322314
result NzNat: 21322314
Maude> red k(21322314, 123) . --- Expected: 21322314
result NzNat: 21322314
Maude> red k(21322314, 2222) . --- Expected: 21322314
result NzNat: 21322314
Maude> red k(888888888888, 1) . --- Expected: 128
result NzNat: 128
Maude> red k(888888888888, 2) . --- Expected: 111218
result NzNat: 111218
Maude> red k(1888888888888, 1) . --- Expected: 11128
result NzNat: 11128
Ungolfed
mod R is
pr LIST{Nat} + CONVERSION .
ops k s : Nat Nat ~> Nat .
op f : Nat ~> String .
var N K : Nat .
var X Y : [Nat] .
eq k(N, 0) = N .
eq k(N, s K) = k(rat(f(s(N, 0 0 0 0 0 0 0 0 0 0)), 10), K) .
eq s(0, X) = X .
ceq s(N, X K Y) = s(N quo 10, X (s K) Y) if size(X) = N rem 10 [owise] .
eq f(nil) = "" .
eq f(X 0) = f(X) .
eq f(X s N) = f(X) + string(10 * s N + size(X), 10) .
endm
Maude doesn't have logarithms for integers, only for floating-point numbers, so the only good way I could find to "concatenate" integers is to convert them to strings, concatenate, and convert back.
Core Maude + Cheating, 331 bytes
mod R is pr CONVERSION . ops k s : Nat Nat -> Nat . op f : Nat Nat -> String
. var N K S : Nat . eq k(N,0)= N . eq k(N,s K)= k(rat(f(0,s(N,0)),10),K). eq
s(0,S)= S . eq s(N,S)= s(N quo 10,S + 512 ^(N rem 10))[owise]. eq f(10,S)=
"" . eq f(N,S)= if(S & 511)> 0 then string(10 *(S & 511)+ N,10)else "" fi +
f(s N,S >> 9)[owise]. endm
Ungolfed
mod R is
pr CONVERSION .
ops k s : Nat Nat -> Nat .
op f : Nat Nat -> String .
var N K S : Nat .
eq k(N, 0) = N .
eq k(N, s K) = k(rat(f(0, s(N, 0)), 10), K) .
eq s(0, S) = S .
eq s(N, S) = s(N quo 10, S + 512 ^ (N rem 10)) [owise] .
eq f(10, S) = "" .
eq f(N, S) = if (S & 511) > 0 then string(10 * (S & 511) + N, 10) else "" fi + f(s N, S >> 9) [owise] .
endm
If we make the small assumption that no digit occurs more than 511 times, we can avoid having to import LIST
and keep the intermediate summary packed in an integer with 9 bits per digit tracked. The bit size is sort of arbitrary since Maude has unbounded integers, so I picked 9 bits to avoid paying 4 extra bytes for 10
and 1024
.
1888888888888, 1
as a test case (outputs11128
) as my program failed for it (as will any method that concatenates by converting from base 10) \$\endgroup\$21322314, anything -> 21322314
(a stable input). \$\endgroup\$9009
with a run of 21 distinct values before it starts repeating. \$\endgroup\$