Munchausen numbers: Difference between revisions

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 olyonly 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 powrpower 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 86 ⟶ 87:
d count[ d7 ] +:= 1; d count[ d8 ] +:= 1; d count[ d9 ] +:= 1;
d count[ da ] +:= 1;
LONG# INTsubtract numberthe :=occurrences of each digit in the power sum; #
INT# (also including leading zeros) - if all counts drop to 0 we := 10;#
# have a Munchausen number #
LONG INT number := digit power sum;
INT leading zeros := 10;
WHILE number > 0 DO
d count[ SHORTEN ( number MOD 10 ) ] -:= 1;
3,049

edits