Munchausen numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: use pygments, handle 0 correctly, check full range, clearer output)
m (→‎Alternative: minor optimisation on range of tot, now 3*faster)
Line 2,099: Line 2,099:
string digits = sprint(lo)
string digits = sprint(lo)
sequence res = {}
sequence res = {}
integer count = 0
integer count = 0, l = length(digits)
atom lim = power(10,l), lom = 0
while length(digits)<=maxlen do
while length(digits)<=maxlen do
count += 1
count += 1
Line 2,107: Line 2,108:
if d then tot += power(d,d) end if
if d then tot += power(d,d) end if
end for
end for
if sort(sprint(tot))=digits then
if tot>=lom and tot<=lim and sort(sprint(tot))=digits then
res &= tot
res &= tot
end if
end if
Line 2,113: Line 2,114:
if j=0 then
if j=0 then
digits = repeat('0',length(digits)+1)
digits = repeat('0',length(digits)+1)
lim *= 10
lom = (lom+1)*10-1
exit
exit
elsif digits[j]<'9' then
elsif digits[j]<'9' then
Line 2,131: Line 2,134:
Munchausen 1..4 digits (999 combinations checked): {1,3435}
Munchausen 1..4 digits (999 combinations checked): {1,3435}
All Munchausen, 0..11 digits (352715 combinations): {0,1,3435,438579088}
All Munchausen, 0..11 digits (352715 combinations): {0,1,3435,438579088}
"1.1s"
"0.3s"
</pre>
</pre>