RosettaCodeData/Task/Digital-root-Multiplicative-digital-root/Phix/digital-root-multiplicative-digital-root-1.phix
2026-02-01 16:33:20 -08:00

44 lines
1 KiB
Text

with javascript_semantics
function mdr_mp(integer m)
integer mp = 0
while m>9 do
integer newm = 1
while m do
newm *= remainder(m,10)
m = floor(m/10)
end while
m = newm
mp += 1
end while
return {m,mp}
end function
constant tests = {123321, 7739, 893, 899998}
printf(1,"Number MDR MP\n")
printf(1,"====== === ==\n")
for i=1 to length(tests) do
integer ti = tests[i]
printf(1,"%6d %6d %6d\n",ti&mdr_mp(ti))
end for
integer i=0, found = 0
sequence res = columnize({tagset(9,0)})
-- (ie {{0},{1},..,{9}})
while found<50 do -- (ie the full 10*5)
integer mdr1 = mdr_mp(i)[1]+1
sequence m1 = res[mdr1]
if length(m1)<6 then
res[mdr1] = 0 -- (avoid p2js violation)
m1 &= i
res[mdr1] = m1
found += 1
end if
i += 1
end while
printf(1,"\nMDR 1 2 3 4 5")
printf(1,"\n=== ===========================\n")
for i=1 to 10 do
printf(1,"%2d %5d %5d %5d %5d %5d\n",res[i])
end for