Munchausen numbers: Difference between revisions
→{{header|langur}}
Not a robot (talk | contribs) (Add BQN) |
Langurmonkey (talk | contribs) |
||
(41 intermediate revisions by 19 users not shown) | |||
Line 19:
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">L(i) 5000
I i == sum(String(i).map(x -> Int(x) ^ Int(x)))
print(i)</
{{out}}
Line 31 ⟶ 30:
=={{header|360 Assembly}}==
<
MUNCHAU CSECT
USING MUNCHAU,R12 base register
Line 62 ⟶ 61:
PG DC CL12' ' buffer
REGEQU
END MUNCHAU </
{{out}}
<pre>
Line 70 ⟶ 69:
=={{header|8080 Assembly}}==
<
puts: equ 9 ; CP/M syscall to print string
org 100h
Line 176 ⟶ 175:
pop d ; Restore DE
ret
dpow: dw 1,1,4,27,256,3125 ; 0^0 to 5^5 lookup table</
{{out}}
Line 182 ⟶ 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!}}==
<syntaxhighlight lang="action!">;there are considered digits 0-5 because 6^6>5000
DEFINE MAXDIGIT="5"
INT ARRAY powers(MAXDIGIT+1)
INT FUNC Power(BYTE x)
INT res
BYTE i
IF x=0 THEN RETURN (0) FI
res=1
FOR i=0 TO x-1
DO
res==*x
OD
RETURN (res)
BYTE FUNC IsMunchausen(INT x)
INT sum,tmp
BYTE d
tmp=x sum=0
WHILE tmp#0
DO
d=tmp MOD 10
IF d>MAXDIGIT THEN
RETURN (0)
FI
sum==+powers(d)
tmp==/10
OD
IF sum=x THEN
RETURN (1)
FI
RETURN (0)
PROC Main()
INT i
FOR i=0 TO MAXDIGIT
DO
powers(i)=Power(i)
OD
FOR i=1 TO 5000
DO
IF IsMunchausen(i) THEN
PrintIE(i)
FI
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Munchausen_numbers.png Screenshot from Atari 8-bit computer]
<pre>
1
3435
</pre>
=={{header|Ada}}==
<
procedure Munchausen is
Line 208 ⟶ 280:
end loop;
Ada.Text_IO.New_Line;
end Munchausen;</
{{out}}
Line 214 ⟶ 286:
=={{header|ALGOL 68}}==
<
# note that 6^6 is 46 656 so we only need to consider numbers consisting of 0 to 5 #
Line 252 ⟶ 324:
FI
OD
</syntaxhighlight>
{{out}}
<pre>
Line 260 ⟶ 332:
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.
<
# Munchausen numbers with 11+ digits #
# table of Nth powers - note 0^0 is 0 for Munchausen numbers, not 1 #
Line 318 ⟶ 390:
OD
OD
OD</
{{out}}
<pre>
Line 329 ⟶ 401:
=={{header|ALGOL W}}==
{{Trans|ALGOL 68}}
<
% note that 6^6 is 46 656 so we only need to consider numbers consisting of 0 to 5 %
begin
Line 371 ⟶ 443:
end
end.</
{{out}}
<pre>
Line 381 ⟶ 453:
{{works with|Dyalog APL}}
<
{{out}}
Line 389 ⟶ 461:
=={{header|AppleScript}}==
===Functional===
<
-- isMunchausen :: Int -> Bool
Line 468 ⟶ 540:
end script
end if
end mReturn</
{{Out}}
<syntaxhighlight lang
===Iterative===
Line 476 ⟶ 548:
More straightforwardly:
<
repeat with i from 1 to 5000
if (i > 0) then
Line 489 ⟶ 561:
end repeat
return MunchhausenNumbers</
{{Out}}
<syntaxhighlight lang
=={{header|Arturo}}==
<
n = sum map split to :string n 'digit [
d: to :integer digit
Line 504 ⟶ 576:
if munchausen? x ->
print x
]</
{{out}}
Line 510 ⟶ 582:
<pre>1
3435</pre>
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">Loop, 5000
{
Loop, Parse, A_Index
var += A_LoopField**A_LoopField
if (var = A_Index)
num .= var "`n"
var := 0
}
Msgbox, %num%</syntaxhighlight>
{{out}}
<pre>
1
3435
</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f MUNCHAUSEN_NUMBERS.AWK
BEGIN {
Line 527 ⟶ 615:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 536 ⟶ 624:
=={{header|BASIC}}==
This should need only minimal modification to work with any old-style BASIC that supports user-defined functions. The call to <code>INT</code> in line 10 is needed because the exponentiation operator may return a (floating-point) value that is slightly too large.
<
20 FOR I=0 TO 5
30 FOR J=0 TO 5
Line 547 ⟶ 635:
100 NEXT K
110 NEXT J
120 NEXT I</
{{out}}
<pre> 1
Line 554 ⟶ 642:
==={{header|Sinclair ZX81 BASIC}}===
Works with 1k of RAM. The word <code>FAST</code> in line 10 shouldn't be taken <i>too</i> literally. We don't have <code>DEF FN</code>, so the expression for exponentiation-where-zero-to-the-power-zero-equals-zero is written out inline.
<
20 FOR I=0 TO 5
30 FOR J=0 TO 5
Line 566 ⟶ 654:
110 NEXT J
120 NEXT I
130 SLOW</
{{out}}
<pre>1
Line 572 ⟶ 660:
=={{header|BBC BASIC}}==
<
FOR i% = 0 TO 5
FOR j% = 0 TO 5
Line 590 ⟶ 678:
= 0
ELSE
= x% ^ x%</
{{out}}
<pre> 1
Line 596 ⟶ 684:
=={{header|BQN}}==
<
IsMnch ← ⊢=+´∘(⋆˜ Dgts)
IsMnch¨⊸/ 1+↕5000</
{{out}}
<pre>⟨ 1 3435 ⟩</pre>
Line 604 ⟶ 692:
=={{header|C}}==
Adapted from Zack Denton's code posted on [https://zach.se/munchausen-numbers-and-how-to-find-them/ Munchausen Numbers and How to Find Them].
<
#include <math.h>
Line 626 ⟶ 714:
}
return 0;
}</
{{out}}
<pre>1
Line 632 ⟶ 720:
=={{header|C sharp|C#}}==
<
foreach (var i in Enumerable.Range(1,5000)
.Where(n => n == n.ToString()
.Sum(x => Math.Pow(toInt(x), toInt(x)))))
Console.WriteLine(i);</
{{out}}
<pre>1
Line 644 ⟶ 732:
=== Faster version ===
{{Trans|Kotlin}}
<
namespace Munchhausen
Line 686 ⟶ 774:
}
}
}</
<pre>0
1
Line 694 ⟶ 782:
{{trans|Visual Basic .NET}}
Search covers all 11 digit numbers (as pointed out elsewhere, 11*(9^9) has only 10 digits, so there are no Munchausen numbers with 11+ digits), not just the first half of the 9 digit numbers. Computation time is under 1.5 seconds.
<
static class Program
Line 719 ⟶ 807:
}
}
}</
{{out}}
<pre>0
Line 727 ⟶ 815:
=={{header|C++}}==
<
#include <math.h>
#include <iostream>
Line 750 ⟶ 838:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
Line 760 ⟶ 848:
=={{header|Clojure}}==
<
(:require [clojure.math.numeric-tower :as math])
(:use [criterium.core])
Line 783 ⟶ 871:
(println (find-numbers 5000))
</syntaxhighlight>
{{Output}}
<pre>
(1 3435)
</pre>
=={{header|CLU}}==
<syntaxhighlight lang="clu">digits = iter (n: int) yields (int)
while n>0 do
yield(n//10)
n := n/10
end
end digits
munchausen = proc (n: int) returns (bool)
k: int := 0
for d: int in digits(n) do
% Note: 0^0 is to be regarded as 0
if d~=0 then k := k + d ** d end
end
return(n = k)
end munchausen
start_up = proc ()
po: stream := stream$primary_output()
for i: int in int$from_to(1,5000) do
if munchausen(i) then stream$putl(po, int$unparse(i)) end
end
end start_up</syntaxhighlight>
{{out}}
<pre>1
3435</pre>
=={{header|Common Lisp}}==
<
;;; check4munch maximum &optional b
;;; Return a list with all Munchausen numbers less then or equal to maximum.
Line 825 ⟶ 940:
(let ((dm (divmod n base)))
(n2base (car dm) base (cons (cadr dm) digits)))))
</syntaxhighlight>
{{Out}}
Line 836 ⟶ 951:
=={{header|COBOL}}==
<
PROGRAM-ID. MUNCHAUSEN.
Line 868 ⟶ 983:
ADD-DIGIT-POWER.
COMPUTE POWER-SUM =
POWER-SUM + DIGITS(DIGIT) ** DIGITS(DIGIT)</
{{out}}
<pre> 1
Line 874 ⟶ 989:
=={{header|Cowgol}}==
<
sub digitPowerSum(n: uint16): (sum: uint32) is
Line 895 ⟶ 1,010:
end if;
n := n + 1;
end loop;</
{{out}}
<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>
=={{header|D}}==
{{trans|C}}
<
void main() {
Line 924 ⟶ 1,073:
}
}
}</
{{out}}
<pre>1
Line 932 ⟶ 1,081:
Needs a modern Dc due to <code>~</code>.
Use <code>S1S2l2l1/L2L1%</code> instead of <code>~</code> to run it in older Dcs.
<
[p] sp
[z d d lM x =p z 5001>L ] sL
lL x</
Cosmetic: The stack is dirty after execution. The loop <code>L</code> needs a fix if that is a problem.
=={{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}}==
<
@pow for i <- 0..9, into: %{}, do: {i, :math.pow(i,i) |> round}
Line 952 ⟶ 1,149:
Enum.each(1..5000, fn i ->
if Munchausen.number?(i), do: IO.puts i
end)</
{{out}}
Line 961 ⟶ 1,158:
=={{header|F sharp|F#}}==
<
let power x = toFloat x ** toFloat x |> int
let isMunchausen n = n = (string n |> Seq.map char |> Seq.map power |> Seq.sum)
printfn "%A" ([1..5000] |> List.filter isMunchausen)</
{{out}}
<pre>[1; 3435]</pre>
=={{header|Factor}}==
<
prettyprint sequences ;
Line 976 ⟶ 1,173:
dup 1 digit-groups dup [ ^ ] 2map sum = ;
5000 [1,b] [ munchausen? ] filter .</
{{out}}
<pre>
Line 983 ⟶ 1,180:
=={{header|FALSE}}==
<
$$0\[$][
$10/$@\10*-
Line 993 ⟶ 1,190:
]#
%=[$.10,]?
]#%</
{{out}}
Line 1,001 ⟶ 1,198:
=={{header|FOCAL}}==
<
02.10 S M=N;S S=0
Line 1,010 ⟶ 1,207:
02.50 I (N-S)2.7,2.6,2.7
02.60 T %4,N,!
02.70 R</
{{out}}
Line 1,019 ⟶ 1,216:
=={{header|Forth}}==
{{works with|GNU Forth|0.7.0}}
<
: dig.num \ returns input number and the number of its digits ( n -- n n1 )
dup
Line 1,081 ⟶ 1,278:
i check.num = if i . cr
then loop ;
</
{{out}}
<pre>
Line 1,092 ⟶ 1,289:
{{trans|360 Assembly}}
===Fortran IV===
<
DO 2 I=1,5000
IS=0
Line 1,103 ⟶ 1,300:
1 II=IR
2 IF(IS.EQ.I) WRITE(*,*) I
END </
{{out}}
<pre>
Line 1,110 ⟶ 1,307:
</pre>
===Fortran 77===
<
DO I=1,5000
IS=0
Line 1,123 ⟶ 1,320:
IF(IS.EQ.I) WRITE(*,*) I
END DO
END </
{{out}}
<pre>
Line 1,132 ⟶ 1,329:
=={{header|FreeBASIC}}==
===Version 1===
<
' Cache n ^ n for the digits 1 to 9
' Note than 0 ^ 0 specially treated as 0 (not 1) for this purpose
Line 1,163 ⟶ 1,360:
Print "Press any key to quit"
Sleep</
{{out}}
<pre>The Munchausen numbers between 0 and 500000000 are :
Line 1,171 ⟶ 1,368:
438579088</pre>
===Version 2===
<
' compile with: fbc -s console
Line 1,235 ⟶ 1,432:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>0
Line 1,241 ⟶ 1,438:
3435
438579088</pre>
=={{header|Frink}}==
<syntaxhighlight lang="frink">isMunchausen = { |x|
sum = 0
for d = integerDigits[x]
sum = sum + d^d
return sum == x
}
println[select[1 to 5000, isMunchausen]]</syntaxhighlight>
{{out}}
<pre>
[1, 3435]
</pre>
=={{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}}==
{{trans|Kotlin}}
<
import(
Line 1,287 ⟶ 1,510:
}
fmt.Println()
}</
{{out}}
Line 1,295 ⟶ 1,518:
=={{header|Haskell}}==
<
import Data.List (unfoldr)
isMunchausen :: Integer -> Bool
isMunchausen =
(==)
digit 0 = Nothing
digit n = Just (r, q) where (q, r) = n `divMod` 10
main :: IO ()
main = print $ filter isMunchausen [1 .. 5000]</
{{out}}
<pre>[1,3435]</pre>
Line 1,309 ⟶ 1,536:
The Haskell libraries provide a lot of flexibility – we could also reduce the sum and map (above) down to a single foldr:
<
isMunchausen :: Int -> Bool
isMunchausen =
(==)
<*> foldr ((+) . (id >>=) (^) . digitToInt) 0 . show
main :: IO ()
main = print $ filter isMunchausen [1 .. 5000]</
Or, without digitToInt, but importing join, swap and bool.
<
import Data.Bool (bool)
import Data.List (unfoldr)
import Data.Tuple (swap)
isMunchausen :: Integer -> Bool
isMunchausen =
(==)
<*> ( foldr ((+) . join (^)) 0
. unfoldr
( ( flip bool Nothing
. Just
. swap
. flip quotRem 10
)
<*> (0 ==)
)
)
main :: IO ()
main = print $ filter isMunchausen [1 .. 5000]</
{{Out}}
Line 1,336 ⟶ 1,573:
=={{header|J}}==
Here, it would be useful to have a function which sums the powers of the digits of a number. Once we have that we can use it with an equality test to filter those integers:
<
(#~ ] = munch"0) 1+i.5000
1 3435</
Note that [[wp:Munchausen_number|wikipedia]] claims that 0=0^0 in the context of Munchausen numbers. It's not clear why this should be (1 is the multiplicative identity and if you do not multiply it by zero it should still be 1), but it's easy enough to implement. Note also that this does not change the result for this task:
<
(#~ ] = munch"0) 1+i.5000
1 3435</
=={{header|Java}}==
Adapted from Zack Denton's code posted on [https://zach.se/munchausen-numbers-and-how-to-find-them/ Munchausen Numbers and How to Find Them].
<syntaxhighlight lang="java">
public class Main {
public static void main(String[] args) {
Line 1,363 ⟶ 1,599:
}
</syntaxhighlight>
{{out}}
<pre>1 (munchausen)
Line 1,370 ⟶ 1,606:
=== Faster version ===
{{trans|Kotlin}}
<
static final long[] cache = new long[10];
Line 1,398 ⟶ 1,634:
return sum == n;
}
}</
<pre>0
1
Line 1,405 ⟶ 1,641:
=={{header|JavaScript}}==
===ES6===
<
.filter(n => n == n.toString().split('')
.reduce((a, b) => a+Math.pow(parseInt(b),parseInt(b)), 0)))
console.log(i);</
{{out}}
<pre>1
Line 1,419 ⟶ 1,654:
Or, composing reusable primitives:
<
'use strict';
Line 1,450 ⟶ 1,685:
// MAIN ---
return main();
})();</
{{Out}}
<syntaxhighlight lang
=={{header|jq}}==
{{works with|jq|1.5}}
<
def ismunchausen:
Line 1,463 ⟶ 1,698:
# Munchausen numbers from 1 to 5000 inclusive:
range(1;5001) | select(ismunchausen)</
{{out}}
<
3435</
=={{header|Julia}}==
{{works with|Julia|1.0}}
<
{{out}}
Line 1,477 ⟶ 1,712:
=={{header|Kotlin}}==
As it doesn't take long to find all 4 known Munchausen numbers, we will test numbers up to 500 million here rather than just 5000:
<
val powers = IntArray(10)
Line 1,501 ⟶ 1,736:
for (i in 0..500000000) if (isMunchausen(i))print ("$i ")
println()
}</
{{out}}
Line 1,510 ⟶ 1,745:
=={{header|Lambdatalk}}==
<
{def munch
{lambda {:w}
Line 1,523 ⟶ 1,758:
1
3435
</syntaxhighlight>
=={{header|langur}}==
{{trans|C#}}
<syntaxhighlight lang="langur"># sum power of digits
val .spod = fn .n: fold fn{+}, map(fn .x: .x^.x, s2n string .n)
# Munchausen
writeln "Answers: ",
</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}}==
<
local sum, nStr, digit = 0, tostring(n)
for pos = 1, #nStr do
Line 1,568 ⟶ 1,825:
for i = 1, 5000 do
if isMunchausen(i) then print(i) end
end</
{{out}}
<pre>1
Line 1,574 ⟶ 1,831:
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module Munchausen {
Inventory p=0:=0,1:=1
Line 1,593 ⟶ 1,850:
}
Munchausen
</syntaxhighlight>
Using Array instead of Inventory
<syntaxhighlight lang="m2000 interpreter">
Module Münchhausen {
Dim p(0 to 9)
Line 1,615 ⟶ 1,872:
}
Münchhausen
</syntaxhighlight>
{{out}}
<pre>
Line 1,622 ⟶ 1,879:
=={{header|MAD}}==
<
DIMENSION P(5)
Line 1,640 ⟶ 1,897:
VECTOR VALUES FMT = $I4*$
END OF PROGRAM </
{{out}}
Line 1,648 ⟶ 1,905:
=={{header|Maple}}==
<
local num_digits;
num_digits := map(x -> StringTools:-Ord(x) - 48, StringTools:-Explode(convert(n, string)));
Line 1,664 ⟶ 1,921:
end proc;
Munchausen_upto(5000);</
{{out}}
<pre>[1, 3435]</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Select[Range[5000], Total[IntegerDigits[#]^IntegerDigits[#]] == # &]</
{{out}}
<pre>{1,3435}</pre>
Line 1,676 ⟶ 1,933:
=={{header|min}}==
{{works with|min|0.19.3}}
<
1 :i
(i 5000 <=) ((i munchausen?) (i puts!) when i succ @i) while</
{{out}}
<pre>
Line 1,686 ⟶ 1,943:
=={{header|Modula-2}}==
<
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,ReadChar;
Line 1,731 ⟶ 1,988:
ReadChar;
END MunchausenNumbers.</
=={{header|Nim}}==
<
for i in 1..<5000:
Line 1,744 ⟶ 2,001:
number = number div 10
if sum == i:
echo i</
{{out}}
<pre>1
3435</pre>
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let is_munchausen n =
let pwr = [|1; 1; 4; 27; 256; 3125; 46656; 823543; 16777216; 387420489|] in
let rec aux x = if x < 10 then pwr.(x) else aux (x / 10) + pwr.(x mod 10) in
n = aux n
let () =
Seq.(ints 1 |> take 5000 |> filter is_munchausen |> iter (Printf.printf " %u"))
|> print_newline</syntaxhighlight>
{{out}}
<pre> 1 3435</pre>
=={{header|Pascal}}==
Line 1,754 ⟶ 2,023:
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 have to create [http://rosettacode.org/wiki/Combinations_with_repetitions Combinations_with_repetitions] and 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 combination of the digits of number leads to the sum of power of digits. Therefore I need leading zero's.
<
uses
sysutils;
Line 1,826 ⟶ 2,095:
writeln('Check Count ',cnt);
end.
</syntaxhighlight>
{{Out}}
<pre> 1 000000001
Line 1,839 ⟶ 2,108:
=={{header|Perl}}==
<
for my $n (1..5000) {
print "$n\n" if $n == sum( map { $_**$_ } split(//,$n) );
}</
{{out}}
<pre>1
Line 1,848 ⟶ 2,117:
=={{header|Phix}}==
<!--(phixonline)-->
<syntaxhighlight lang="phix">
with javascript_semantics
constant powers = sq_power(tagset(9),tagset(9))
function munchausen(integer n)
integer n0 = n
atom
while n!=0 do
if r then total += powers[r] end if
n = floor(n/10)
end while
return
end function
for
if munchausen(
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>
=={{header|PHP}}==
<
<?php
Line 1,895 ⟶ 2,213:
echo $i . PHP_EOL;
}
}</
{{out}}
<pre>
1
3435</pre>
=={{header|Picat}}==
<syntaxhighlight lang="picat">go =>
println([N : N in 1..5000, munchhausen_number(N)]).
munchhausen_number(N) =>
N == sum([T : I in N.to_string(),II = I.to_int(), T = II**II]).</syntaxhighlight>
{{out}}
<pre>[1,3435]</pre>
Testing for a larger interval, 1..500 000 000, requires another approach:
<syntaxhighlight lang="picat">go2 ?=>
H = [0] ++ [I**I : I in 1..9],
N = 1,
while (N < 500_000_000)
Sum = 0,
NN = N,
Found = true,
while (NN > 0, Found == true)
Sum := Sum + H[1+(NN mod 10)],
if Sum > N then
Found := false
end,
NN := NN div 10
end,
if Sum == N then
println(N)
end,
N := N+1
end,
nl.
</syntaxhighlight>
{{out}}
<pre>1
3435
438579088</pre>
=={{header|PicoLisp}}==
<
(and
(=
Line 1,909 ⟶ 2,265:
'((N) (** N N))
(mapcar format (chop N)) ) )
(println N) ) )</
{{out}}
<pre>
1
3435</pre>
=={{header|PL/I}}==
<syntaxhighlight lang="pli">munchausen: procedure options(main);
/* precalculate powers */
declare (pows(0:5), i) fixed;
pows(0) = 0; /* 0^0=0 for Munchausen numbers */
do i=1 to 5; pows(i) = i**i; end;
declare (d1, d2, d3, d4, num, dpow) fixed;
do d1=0 to 5;
do d2=0 to 5;
do d3=0 to 5;
do d4=1 to 5;
num = d1*1000 + d2*100 + d3*10 + d4;
dpow = pows(d1) + pows(d2) + pows(d3) + pows(d4);
if num=dpow then put skip list(num);
end;
end;
end;
end;
end munchausen;</syntaxhighlight>
{{out}}
<pre> 1
3435</pre>
=={{header|Plain English}}==
<
Start up.
Show the Munchausen numbers up to 5000.
Line 1,943 ⟶ 2,323:
If the number is 0, break.
Repeat.
Put the sum into the number.</
{{out}}
<pre>
Line 1,952 ⟶ 2,332:
=={{header|PowerBASIC}}==
{{trans|FreeBASIC}}(Translated from the FreeBasic Version 2 example.)
<
#DIM ALL
#COMPILER PBCC 6
Line 2,024 ⟶ 2,404:
CON.PRINT "execution time:" & STR$(t) & " ms; hit any key to end program"
CON.WAITKEY$
END FUNCTION</
{{out}}
<pre> 0
Line 2,033 ⟶ 2,413:
=={{header|Pure}}==
<
digits n::number = loop n [] with
loop n l = loop (n div 10) ((n mod 10):l) if n > 0;
Line 2,043 ⟶ 2,423:
(map (\d -> d^d)
(digits n)); end;
munchausen 5000;</
{{out}}
<pre>[1,3435]</pre>
Line 2,049 ⟶ 2,429:
=={{header|PureBasic}}==
{{trans|C}}
<
Declare main()
Line 2,073 ⟶ 2,453:
EndIf
Next
EndProcedure</
{{out}}
<pre>1
Line 2,079 ⟶ 2,459:
=={{header|Python}}==
<
if i == sum(int(x) ** int(x) for x in str(i)):
print(i)</
{{out}}
<pre>1
Line 2,092 ⟶ 2,472:
{{Works with|Python|3}}
<
from functools import (reduce)
Line 2,147 ⟶ 2,527:
if __name__ == '__main__':
main()</
<pre>[1, 3435]</pre>
=={{header|Quackery}}==
<syntaxhighlight lang="quackery"> [ dup 0 swap
[ dup 0 != while
10 /mod dup **
Line 2,160 ⟶ 2,539:
5000 times
[ i^ 1+ munchausen if
[ i^ 1+ echo sp ] ]</
{{out}}
Line 2,167 ⟶ 2,546:
=={{header|Racket}}==
<syntaxhighlight lang="text">#lang racket
(define (expt:0^0=1 r p)
Line 2,193 ⟶ 2,571:
(check-true (munchausen-number? 3435))
(check-false (munchausen-number? 3))
(check-false (munchausen-number? -45) "no recursion on -ve numbers"))</
{{out}}
Line 2,201 ⟶ 2,579:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
constant @powers = 0, |map { $_ ** $_ }, 1..9;
$n == @powers[$n.comb].sum;
}
.say if .&is_munchausen for 1..5000;</
{{out}}
<pre>1
Line 2,212 ⟶ 2,590:
=={{header|REXX}}==
===version 1===
<
If n=m(n) Then
Say n
Line 2,223 ⟶ 2,601:
res=res+c**c
End
Return res</
{{out}}
<pre>D:\mau>rexx munch
Line 2,236 ⟶ 2,614:
For the high limit of '''5,000''', optimization isn't needed. But for much higher limits, optimization becomes significant.
<
@.= 0; do i=1 for 9; @.i= i**i; end /*precompute powers for non-zero digits*/
parse arg z . /*obtain optional argument from the CL.*/
Line 2,248 ⟶ 2,626:
parse var x _ +1 x; $= $ + @._ /*add the next power*/
end /*while*/ /* [↑] get a digit.*/
return $==ox /*it is or it ain't.*/</
{{out|output|text= when using the default input:}}
<pre>
Line 2,257 ⟶ 2,635:
===version 3===
It is about '''3''' times faster than REXX version 1.
<
@.= 0; do i=1 for 9; @.i= i**i; end /*precompute powers for non-zero digits*/
parse arg z . /*obtain optional argument from the CL.*/
Line 2,271 ⟶ 2,649:
parse var x _ +1 x; $= $ + @._ /*sum 6th & up digs*/
end /*while*/
return $==ox /*it is or it ain't*/</
{{out|output|text= is the same as the 2<sup>nd</sup> REXX version.}} <br><br>
=={{header|Ring}}==
<
# Project : Munchausen numbers
Line 2,291 ⟶ 2,669:
ok
next
</syntaxhighlight>
Output:
<pre>
Line 2,298 ⟶ 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>
{{out}}
<pre>
Line 2,315 ⟶ 2,699:
=={{header|Rust}}==
<
let mut solutions = Vec::new();
Line 2,333 ⟶ 2,717:
println!("Munchausen numbers below 5_000 : {:?}", solutions);
}</
{{out}}
<pre>
Line 2,341 ⟶ 2,725:
=={{header|Scala}}==
Adapted from Zack Denton's code posted on [https://zach.se/munchausen-numbers-and-how-to-find-them/ Munchausen Numbers and How to Find Them].
<syntaxhighlight lang="scala">
object Munch {
def main(args: Array[String]): Unit = {
Line 2,351 ⟶ 2,735:
}
}
</syntaxhighlight>
{{out}}
<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}}==
<
n.digits.map{|d| d**d }.sum == n
}
say (1..5000 -> grep(is_munchausen))</
{{out}}
<pre>
Line 2,368 ⟶ 2,772:
=={{header|SuperCollider}}==
<
<pre>
Line 2,375 ⟶ 2,779:
=={{header|Swift}}==
<
func isMünchhausen(_ n: Int) -> Bool {
Line 2,385 ⟶ 2,789:
for i in 1...5000 where isMünchhausen(i) {
print(i)
}</
{{out}}
<pre>1
Line 2,391 ⟶ 2,795:
=={{header|Symsyn}}==
<
x : 10 1
Line 2,422 ⟶ 2,826:
endif
</syntaxhighlight>
{{out}}
<pre>1
Line 2,430 ⟶ 2,834:
{{works with|TI-83 BASIC|TI-84Plus 2.55MP}}
{{trans|Fortran}}
<
0→S:I→K
For(J,1,4)
Line 2,440 ⟶ 2,844:
End
If S=I:Disp I
End </
{{out}}
<pre>
Line 2,451 ⟶ 2,855:
{{trans|BASIC}}
This takes advantage of the fact that N^N > 9999 for any single digit natural number N where N > 6. It also uses a look up table for powers to allow the assumption that 0^0 = 1.
<
For(A,0,5,1)
For(B,0,5,1)
Line 2,469 ⟶ 2,873:
End
End
End</
{{out}}
<pre>
Line 2,478 ⟶ 2,882:
=={{header|VBA}}==
<syntaxhighlight lang="vb">
Option Explicit
Line 2,499 ⟶ 2,902:
IsMunchausen = (Tot = Number)
End Function
</syntaxhighlight>
{{out}}
<pre>1 is a munchausen number.
Line 2,505 ⟶ 2,908:
=={{header|VBScript}}==
<
for i = 1 to 5000
if Munch(i) Then
Line 2,531 ⟶ 2,934:
End Function
</syntaxhighlight>
{{out}}
<pre>
Line 2,540 ⟶ 2,943:
=={{header|Visual Basic}}==
{{trans|FreeBASIC}}(Translated from the FreeBasic Version 2 example.)
<
Declare Function GetTickCount Lib "kernel32.dll" () As Long
Line 2,646 ⟶ 3,049:
res = res & "execution time:" & Str$(t) & " ms"
MsgBox res
End Sub</
{{out}}
<pre> 0
Line 2,657 ⟶ 3,060:
{{trans|FreeBASIC}}(Translated from the FreeBasic Version 2 example.)<br/>
Computation time is under 4 seconds on tio.run.
<
Module Program
Line 2,681 ⟶ 3,084:
Next
End Sub
End Module</
{{out}}
<pre>0
Line 2,689 ⟶ 3,092:
=={{header|Wren}}==
<
for (i in 1..9) powers[i] = i.pow(i).round // cache powers
Line 2,704 ⟶ 3,107:
}
System.print("The Munchausen numbers <= 5000 are:")
for (i in 1..5000) {
if (munchausen.call(i)) System.print(i)
}</
{{out}}
<pre>
The Munchausen numbers <= 5000 are:
1
3435
</pre>
=={{header|XPL0}}==
The digits 6, 7, 8 and 9 can't occur because 6^6 = 46656, which is beyond 5000.
<syntaxhighlight lang="xpl0">int Pow, A, B, C, D, N;
[Pow:= [0, 1, 4, 27, 256, 3125];
for A:= 0 to 5 do
for B:= 0 to 5 do
for C:= 0 to 5 do
for D:= 0 to 5 do
[N:= A*1000 + B*100 + C*10 + D;
if Pow(A) + Pow(B) + Pow(C) + Pow(D) = N then
if N>=1 & N<= 5000 then
[IntOut(0, N); CrLf(0)];
];
]</syntaxhighlight>
{{out}}
<pre>
1
3435
Line 2,718 ⟶ 3,141:
=={{header|zkl}}==
<
.println();</
{{out}}
<pre>
|