Munchausen numbers: Difference between revisions
→{{header|langur}}
(→{{header|Ruby}}: end-less method; sum + block) |
Langurmonkey (talk | contribs) |
||
(24 intermediate revisions by 10 users not shown) | |||
Line 181:
<pre>3435
0001</pre>
=={{header|ABC}}==
<syntaxhighlight lang="ABC">HOW TO REPORT munchausen n:
PUT 0 IN sum
PUT n IN m
WHILE m > 0:
PUT m mod 10 IN digit
PUT sum + digit**digit IN sum
PUT floor(m/10) IN m
REPORT sum = n
FOR n IN {1..5000}:
IF munchausen n: WRITE n/</syntaxhighlight>
{{out}}
<pre>1
3435</pre>
=={{header|Action!}}==
Line 999 ⟶ 1,015:
<pre>1
3435</pre>
=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">for i = 0 to 5
for j = 0 to 5
for k = 0 to 5
for l = 0 to 5
let m = int(i ^ i * sgn(i))
let m = m + int(j ^ j * sgn(j))
let m = m + int(k ^ k * sgn(k))
let m = m + int(l ^ l * sgn(l))
let n = 1000 * i + 100 * j + 10 * k + l
if m = n and m > 0 then
print m
endif
wait
next l
next k
next j
next i</syntaxhighlight>
{{out| Output}}<pre>1
3435</pre>
Line 1,039 ⟶ 1,089:
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Munchausen_numbers#Pascal Pascal].
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc munchausen(word n) bool:
/* d^d for d>6 does not fit in a 16-bit word,
* it follows that any 16-bit integer containing
* a digit d>6 is not a Munchausen number */
[7]word dpow = (1, 1, 4, 27, 256, 3125, 46656);
word m, d, sum;
m := n;
sum := 0;
while
d := m % 10;
m>0 and d<=6
do
m := m/10;
sum := sum + dpow[d]
od;
d<=6 and sum=n
corp;
proc main() void:
word n;
for n from 1 upto 5000 do
if munchausen(n) then
writeln(n)
fi
od
corp</syntaxhighlight>
{{out}}
<pre>1
3435</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
for i = 1 to 5000
sum = 0
n = i
while n > 0
dig = n mod 10
sum += pow dig dig
n = n div 10
.
if sum = i
print i
.
.
</syntaxhighlight>
=={{header|Elixir}}==
Line 1,356 ⟶ 1,454:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Munchausen_numbers}}
'''Solution'''
[[File:Fōrmulæ - Munchausen numbers 01.png]]
'''Test case 1''' Find all Munchausen numbers between 1 and 5000
[[File:Fōrmulæ - Munchausen numbers 02.png]]
[[File:Fōrmulæ - Munchausen numbers 03.png]]
'''Test case 2''' Show the Munchausen numbers between 1 and 5,000 from bases 2 to 10
[[File:Fōrmulæ - Munchausen numbers 04.png]]
[[File:Fōrmulæ - Munchausen numbers 05.png]]
=={{header|Go}}==
Line 1,651 ⟶ 1,762:
=={{header|langur}}==
{{trans|C#}}
<syntaxhighlight lang="langur"># sum power of digits
val .spod =
# Munchausen
writeln "Answers: ", filter
</syntaxhighlight>
{{out}}
<pre>Answers: [1, 3435]</pre>
=={{header|LDPL}}==
<syntaxhighlight lang="ldpl">data:
d is number
i is number
n is number
sum is number
procedure:
for i from 1 to 5001 step 1 do
store 0 in sum
store i in n
while n is greater than 0 do
modulo n by 10 in d
raise d to d in d
add sum and d in sum
divide n by 10 in n
floor n
repeat
if sum is equal to i then
display i lf
end if
repeat
</syntaxhighlight>
{{out}}
<pre>
1
3435
</pre>
=={{header|Lua}}==
Line 1,983 ⟶ 2,117:
=={{header|Phix}}==
<!--
<syntaxhighlight lang="phix">
with javascript_semantics
constant powers = sq_power(tagset(9),tagset(9))
function munchausen(integer n)
integer n0 = n
atom total = 0
while n!=0 do
integer r = remainder(n,10)
if r then total += powers[r] end if
n = floor(n/10)
end while
return (total==n0)
end function
for m in tagset(5000) & 438579088 do
if munchausen(m) then ?m end if
end for
</syntaxhighlight>
{{out}}
Checking every number between 5,000 and 438,579,088 would take/waste a couple of minutes, and it wouldn't prove anything unless it went to 99,999,999,999 which would take a ''very'' long time!
<pre>
1
3435
438579088
</pre>
=== Alternative ===
<syntaxhighlight lang="phix">
function munchausen(integer lo, maxlen)
string digits = sprint(lo)
sequence res = {}
integer count = 0, l = length(digits)
atom lim = power(10,l), lom = 0
while length(digits)<=maxlen do
count += 1
atom tot = 0
for j=1 to length(digits) do
integer d = digits[j]-'0'
if d then tot += power(d,d) end if
end for
if tot>=lom and tot<=lim and sort(sprint(tot))=digits then
res &= tot
end if
for j=length(digits) to 0 by -1 do
if j=0 then
digits = repeat('0',length(digits)+1)
lim *= 10
lom = (lom+1)*10-1
exit
elsif digits[j]<'9' then
digits[j..$] = digits[j]+1
exit
end if
end for
end while
return {count,res}
end function
atom t0 = time()
printf(1,"Munchausen 1..4 digits (%d combinations checked): %v\n",munchausen(1,4))
printf(1,"All Munchausen, 0..11 digits (%d combinations): %v\n",munchausen(0,11))
?elapsed(time()-t0)
</syntaxhighlight>
{{out}}
<pre>
Munchausen 1..4 digits (999 combinations checked): {1,3435}
All Munchausen, 0..11 digits (352715 combinations): {0,1,3435,438579088}
"0.3s"
</pre>
Line 2,496 ⟶ 2,676:
</pre>
=={{header|RPL}}==
≪ { } 1 5000 '''FOR''' j
j →STR DUP SIZE 0 1 ROT '''FOR''' k
OVER k DUP SUB STR→ DUP ^ +
'''NEXT'''
SWAP DROP
'''IF''' j == '''THEN''' j + '''END'''
'''NEXT'''
≫
EVAL
{{out}}
<pre>
1: { 1 3435 }
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">
{{out}}
<pre>
Line 2,551 ⟶ 2,739:
<pre>1 (munchausen)
3435 (munchausen)</pre>
=={{header|SETL}}==
<syntaxhighlight lang="setl">program munchausen_numbers;
loop for n in [1..5000] | munchausen n do
print(n);
end loop;
op munchausen(n);
m := n;
loop while m>0 do
d := m mod 10;
m div:= 10;
sum +:= d ** d;
end loop;
return sum = n;
end op;
end program;</syntaxhighlight>
{{out}}
<pre>1
3435</pre>
=={{header|Sidef}}==
Line 2,884 ⟶ 3,092:
=={{header|Wren}}==
<syntaxhighlight lang="
for (i in 1..9) powers[i] = i.pow(i).round // cache powers
Line 2,899 ⟶ 3,107:
}
System.print("The Munchausen numbers <= 5000 are:")
for (i in 1..5000) {
|