Munchausen numbers: Difference between revisions

Content added Content deleted
(→‎{{header|ALGOL 68}}: Alternate version)
Line 56: 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.
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 only 10 digits so there are no #
<lang algol68># Find all Munchausen numbers - note 11*(9^9) has oly 10 digits so there are no #
# Munchausen numbers with 11+ digits #
# Munchausen numbers with 11+ digits #
# table of Nth powers - note 0^0 is 0 for Munchausen numbers, not 1 #
# table of Nth powers - note 0^0 is 0 for Munchausen numbers, not 1 #
Line 64: Line 64:
[ 0 : 9 ]INT d count := z count;
[ 0 : 9 ]INT d count := z count;


# as the digit power sum is independent of the order of the digits, we need only #
# as the digit powr sum is independent of the order of the digits, we need only #
# consider one arrangement of each possible combination of digits #
# consider one arrangement of each possible combination of digits #
FOR d1 FROM 0 TO 9 DO
FOR d1 FROM 0 TO 9 DO
Line 81: Line 81:
digit power sum +:= nth power[ d7 ] + nth power[ d8 ];
digit power sum +:= nth power[ d7 ] + nth power[ d8 ];
digit power sum +:= nth power[ d9 ] + nth power[ da ];
digit power sum +:= nth power[ d9 ] + nth power[ da ];
# count the occurrences of each digit (including leading zeros #
d count := z count;
d count := z count;
d count[ d1 ] +:= 1; d count[ d2 ] +:= 1; d count[ d3 ] +:= 1;
d count[ d1 ] +:= 1; d count[ d2 ] +:= 1; d count[ d3 ] +:= 1;
Line 87: Line 86:
d count[ d7 ] +:= 1; d count[ d8 ] +:= 1; d count[ d9 ] +:= 1;
d count[ d7 ] +:= 1; d count[ d8 ] +:= 1; d count[ d9 ] +:= 1;
d count[ da ] +:= 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;
LONG INT number := digit power sum;
FOR d TO 10 DO
INT leading zeros := 10;
WHILE number > 0 DO
d count[ SHORTEN ( number MOD 10 ) ] -:= 1;
d count[ SHORTEN ( number MOD 10 ) ] -:= 1;
leading zeros -:= 1;
number OVERAB 10
number OVERAB 10
OD;
OD;
d count[ 0 ] -:= leading zeros;
IF d count[ 0 ] = 0 AND d count[ 1 ] = 0 AND d count[ 2 ] = 0
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
AND d count[ 3 ] = 0 AND d count[ 4 ] = 0 AND d count[ 5 ] = 0