Munchausen numbers: Difference between revisions

Content deleted Content added
Petelomax (talk | contribs)
m →‎{{header|Phix}}: use pygments, handle 0 correctly, check full range, clearer output
Langurmonkey (talk | contribs)
 
(13 intermediate revisions by 5 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 1,073 ⟶ 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}}==
Line 1,412 ⟶ 1,460:
 
[[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,706 ⟶ 1,762:
=={{header|langur}}==
{{trans|C#}}
<syntaxhighlight lang="langur">
{{works with|langur|0.11}}
<syntaxhighlight lang="langur"># sum power of digits
val .spod = f(.fn n) :fold f(fn{+}, map(ffn (.x-'0') :x^ (.x-'0'), s2cpn toString-> string .n-> s2n))
 
# Munchausen
writeln "Answers: ", filter(fn f(.n): .n == .spod(.n), series 0(1..5000</syntaxhighlight>))
</syntaxhighlight>
 
<syntaxhighlight lang="langur"># sum power of digits
val .spod = f(.n) fold f{+}, map(f .x^.x, s2n toString .n)
 
# Munchausen
writeln "Answers: ", filter f(.n) .n == .spod(.n), series 0..5000</syntaxhighlight>
 
{{out}}
Line 2,099 ⟶ 2,150:
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
Line 2,107 ⟶ 2,159:
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
Line 2,113 ⟶ 2,165:
if j=0 then
digits = repeat('0',length(digits)+1)
lim *= 10
lom = (lom+1)*10-1
exit
elsif digits[j]<'9' then
Line 2,131 ⟶ 2,185:
Munchausen 1..4 digits (999 combinations checked): {1,3435}
All Munchausen, 0..11 digits (352715 combinations): {0,1,3435,438579088}
"10.1s3s"
</pre>
 
Line 2,668 ⟶ 2,722:
<pre>
Munchausen numbers below 5_000 : [1, 3435]
</pre>
 
 
 
===Optimized Version===
<syntaxhighlight lang="rust">use num_bigint::BigUint;
use num_traits::cast::ToPrimitive;
use rayon::prelude::*;
 
fn main() {
// Helper function to compute the Munchausen sum
fn munchausen_sum(num: &BigUint) -> BigUint {
num.to_string()
.chars()
.map(|c| {
let digit = c.to_digit(10).unwrap();
if digit == 0 {
BigUint::from(0u32)
} else {
BigUint::from(digit).pow(digit)
}
})
.sum::<BigUint>()
}
 
// Loop through the desired range
let start = BigUint::from(0u128);
let end = BigUint::from(1_000_00u128);
let solutions: Vec<BigUint> = (start.to_u128().unwrap()..end.to_u128().unwrap())
.into_par_iter()
.map(|n| BigUint::from(n))
.filter(|num| munchausen_sum(num) == *num)
.collect();
 
for solution in &solutions {
println!("Munchausen number found: {:?}", solution);
}
 
println!("Munchausen numbers below {:?}: {:?}", end, solutions);
}</syntaxhighlight>
{{out}}
<pre>
Munchausen number found: 0
Munchausen number found: 1
Munchausen number found: 3435
Munchausen numbers below 100000: [0, 1, 3435]
</pre>
 
Line 2,686 ⟶ 2,786:
<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}}==