Munchausen numbers: Difference between revisions

(→‎{{header|ALGOL 68}}: Alternate version)
Line 56:
 
Alternative that finds all 4 Munchausen numbers. As noted by the Pascal sample, we only need to consider one arrangement of the digits of each number (e.g. we only need to consider 3345, not 3435, 3453, etc.). This also relies on the non-standard 0^0 = 0.
<lang algol68># Find all Munchausen numbers - note 11*(9^9) has onlyoly 10 digits so there are no #
# Munchausen numbers with 11+ digits #
# table of Nth powers - note 0^0 is 0 for Munchausen numbers, not 1 #
Line 64:
[ 0 : 9 ]INT d count := z count;
 
# as the digit powerpowr sum is independent of the order of the digits, we need only #
# consider one arrangement of each possible combination of digits #
FOR d1 FROM 0 TO 9 DO
Line 81:
digit power sum +:= nth power[ d7 ] + nth power[ d8 ];
digit power sum +:= nth power[ d9 ] + nth power[ da ];
# count the occurrences of each digit (including leading zeros #
d count := z count;
d count[ d1 ] +:= 1; d count[ d2 ] +:= 1; d count[ d3 ] +:= 1;
Line 87 ⟶ 86:
d count[ d7 ] +:= 1; d count[ d8 ] +:= 1; d count[ d9 ] +:= 1;
d count[ da ] +:= 1;
# subtract the occurrences of each digit in the power sum #
# (also including leading zeros) - if all counts drop to 0 we #
# have a Munchausen number #
LONG INT number := digit power sum;
FORINT dleading TOzeros 10:= DO10;
#WHILE (alsonumber including leading zeros) - if all counts drop to> 0 we #DO
d count[ SHORTEN ( number MOD 10 ) ] -:= 1;
# subtract the occurrences ofleading eachzeros digit-:= in the power sum #1;
number OVERAB 10
OD;
#d count[ the0 occurrences] of each digit (including-:= leading zeros #;
IF d count[ 0 ] = 0 AND d count[ 1 ] = 0 AND d count[ 2 ] = 0
AND d count[ 3 ] = 0 AND d count[ 4 ] = 0 AND d count[ 5 ] = 0
3,049

edits