Munchausen numbers: Difference between revisions
Content added Content deleted
(→{{header|ALGOL 68}}: Remove unnecessary test) |
m (→{{header|Pascal}}: more explanation, hopefully no obfuscation ;-)) |
||
Line 294: | Line 294: | ||
<pre>1 |
<pre>1 |
||
3435</pre> |
3435</pre> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}=={{works with|Free Pascal}}{{works with|Delphi}} |
||
tried to speed things up.Only checking one arrangement of 123456789 instead of all 9! = 362880 permutations. |
tried to speed things up.Only checking one arrangement of 123456789 instead of all 9! = 362880 permutations. |
||
This ist possible, because summing up is commutative.So I only need to check, that the number and the sum of power of digits have the same amount in every possible digit.This means, that a permutation of the digits of number leads to the sum of power of digits.Therefor I need leading zero's. |
|||
<lang pascal>{$IFDEF FPC} |
|||
{$MODE objFPC} |
<lang pascal>{$IFDEF FPC}{$MODE objFPC}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF} |
||
{$ENDIF} |
|||
uses |
uses |
||
sysutils; |
sysutils; |
||
Line 305: | Line 304: | ||
const |
const |
||
base = 10; |
base = 10; |
||
maxDigits = base-1;// set for 32-compilation. |
maxDigits = base-1;// set for 32-compilation otherwise overflow. |
||
var |
var |
||
Line 341: | Line 340: | ||
else |
else |
||
For i := minDigit to base-1 do |
For i := minDigit to base-1 do |
||
// |
//number is always the arrangement of the digits leading to smallest number |
||
IF (number+i)<= (DgtPowSum+DgtPotDgt[i]) then |
IF (number+i)<= (DgtPowSum+DgtPotDgt[i]) then |
||
IF CheckSameDigits(number+i,DgtPowSum+DgtPotDgt[i]) then |
IF CheckSameDigits(number+i,DgtPowSum+DgtPotDgt[i]) then |
||
iF number+i>0 then |
iF number+i>0 then |
||
writeln( |
writeln(Format('%*d %.*d', |
||
[maxDigits,DgtPowSum+DgtPotDgt[i],maxDigits,number+i])); |
|||
end; |
end; |
||
Line 368: | Line 368: | ||
Munch(0,0,0,maxDigits); |
Munch(0,0,0,maxDigits); |
||
writeln('Check Count ',cnt); |
writeln('Check Count ',cnt); |
||
end. |
end. |
||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> 1 000000001 |
||
3435 000003345 |
|||
438579088 034578889 |
|||
Check Count 43758 |
Check Count 43758 |
||
real 0m0.002s |
real 0m0.002s |
||
</pre> |
|||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |