Munchausen numbers: Difference between revisions
m
→{{header|Phix}}: use pygments, handle 0 correctly, check full range, clearer output
(added Easylang) |
m (→{{header|Phix}}: use pygments, handle 0 correctly, check full range, clearer output) |
||
Line 2,067:
=={{header|Phix}}==
<!--
with javascript_semantics
constant powers = sq_power(tagset(9),tagset(9))
function munchausen(integer n)
integer n0 = n
atom total = 0
while n!=0 do
integer r = remainder(n,10)
if r then total += powers[r] end if
n = floor(n/10)
end while
return (total==n0)
end function
for m in tagset(5000) & 438579088 do
if munchausen(m) then ?m end if
▲<!--</syntaxhighlight>-->
end for
</syntaxhighlight>
{{out}}
Checking every number between 5,000 and 438,579,088 would take/waste a couple of minutes, and it wouldn't prove anything unless it went to 99,999,999,999 which would take a ''very'' long time!
<pre>
1
3435
438579088
</pre>
=== Alternative ===
function munchausen(integer lo, maxlen)
string digits = sprint(lo)
sequence res = {}
integer count = 0
while length(digits)<=maxlen do
count += 1
atom tot = 0
for j=1 to length(digits) do
integer d = digits[j]-'0'
if d then tot += power(d,d) end if
end for
if sort(sprint(tot))=digits then
res &= tot
end if
for j=length(digits) to 0 by -1 do
if j=0 then
digits = repeat('0',length(digits)+1)
elsif digits[j]<'9' then
digits[j..$] = digits[j]+1
end if
end for
end while
return {count,res}
end function
atom t0 = time()
printf(1,"Munchausen 1..4 digits (%d combinations checked): %v\n",munchausen(1,4))
printf(1,"All Munchausen, 0..11 digits (%d combinations): %v\n",munchausen(0,11))
?elapsed(time()-t0)
{{out}}
<pre>
Munchausen 1..4 digits (999 combinations checked): {1,3435}
All Munchausen, 0..11 digits (352715 combinations): {0,1,3435,438579088}
"
</pre>
|