Munchausen numbers: Difference between revisions

(→‎{{header|Ruby}}: No monkeypatching)
 
(24 intermediate revisions by 9 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æ}}==
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Munchausen_numbers}}
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
'''Solution'''
In '''[https://formulae.org/?example=Munchausen_numbers this]''' page you can see the program(s) related to this task and their results.
 
[[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#}}
{{works with|langur|0.11}}
<syntaxhighlight lang="langur"># sum power of digits
val .spod = f(fn .n): fold ffn{+}, map(ffn (.x-'0'): .x^ (.x-'0'), s2cps2n toStringstring .n)
 
# Munchausen
writeln "Answers: ", filter f(fn .n): .n == .spod(.n), series 0..5000</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
<syntaxhighlight lang="langur"># sum power of digits
<pre>Answers: [1, 3435]</pre>
val .spod = f(.n) fold f{+}, map(f .x^.x, s2n toString .n)
 
=={{header|LDPL}}==
# Munchausen
<syntaxhighlight lang="ldpl">data:
writeln "Answers: ", filter f(.n) .n == .spod(.n), series 0..5000</syntaxhighlight>
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>
<pre>Answers: [1, 3435]</pre>
1
3435
</pre>
 
=={{header|Lua}}==
Line 1,983 ⟶ 2,117:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<syntaxhighlight lang="phix">
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
with javascript_semantics
<span style="color: #008080;">constant</span> <span style="color: #000000;">powers</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_power</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">))</span>
constant powers = sq_power(tagset(9),tagset(9))
<span style="color: #008080;">function</span> <span style="color: #000000;">munchausen</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
function munchausen(integer n)
<span style="color: #004080;">integer</span> <span style="color: #000000;">n0</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span>
integer n0 = n
<span style="color: #004080;">atom</span> <span style="color: #000000;">total</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
atom total = 0
<span style="color: #008080;">while</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
while n!=0 do
<span style="color: #000000;">total</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">powers</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
integer r = remainder(n,10)
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
if r then total += powers[r] end if
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
n = floor(n/10)
<span style="color: #008080;">return</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">total</span><span style="color: #0000FF;">==</span><span style="color: #000000;">n0</span><span style="color: #0000FF;">)</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
return (total==n0)
end function
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">5000</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">munchausen</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">i</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
for m in tagset(5000) & 438579088 do
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
if munchausen(m) then ?m end if
<!--</syntaxhighlight>-->
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"> puts (1..5000).select{|n| n.digits.sum{|d| d**d} == n}</syntaxhighlight>
Line 2,545 ⟶ 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,878 ⟶ 3,092:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var powers = List.filled(10, 0)
for (i in 1..9) powers[i] = i.pow(i).round // cache powers
 
Line 2,893 ⟶ 3,107:
}
 
System.print(powers)
System.print("The Munchausen numbers <= 5000 are:")
for (i in 1..5000) {
885

edits