Munchausen numbers: Difference between revisions

Content added Content deleted
(Frink)
m (syntax highlighting fixup automation)
Line 20: Line 20:
{{trans|Python}}
{{trans|Python}}


<lang 11l>L(i) 5000
<syntaxhighlight lang="11l">L(i) 5000
I i == sum(String(i).map(x -> Int(x) ^ Int(x)))
I i == sum(String(i).map(x -> Int(x) ^ Int(x)))
print(i)</lang>
print(i)</syntaxhighlight>


{{out}}
{{out}}
Line 31: Line 31:


=={{header|360 Assembly}}==
=={{header|360 Assembly}}==
<lang 360asm>* Munchausen numbers 16/03/2019
<syntaxhighlight lang="360asm">* Munchausen numbers 16/03/2019
MUNCHAU CSECT
MUNCHAU CSECT
USING MUNCHAU,R12 base register
USING MUNCHAU,R12 base register
Line 62: Line 62:
PG DC CL12' ' buffer
PG DC CL12' ' buffer
REGEQU
REGEQU
END MUNCHAU </lang>
END MUNCHAU </syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 70: Line 70:


=={{header|8080 Assembly}}==
=={{header|8080 Assembly}}==
<lang 8080asm>putch: equ 2 ; CP/M syscall to print character
<syntaxhighlight lang="8080asm">putch: equ 2 ; CP/M syscall to print character
puts: equ 9 ; CP/M syscall to print string
puts: equ 9 ; CP/M syscall to print string
org 100h
org 100h
Line 176: Line 176:
pop d ; Restore DE
pop d ; Restore DE
ret
ret
dpow: dw 1,1,4,27,256,3125 ; 0^0 to 5^5 lookup table</lang>
dpow: dw 1,1,4,27,256,3125 ; 0^0 to 5^5 lookup table</syntaxhighlight>


{{out}}
{{out}}
Line 184: Line 184:


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>;there are considered digits 0-5 because 6^6>5000
<syntaxhighlight lang="action!">;there are considered digits 0-5 because 6^6>5000
DEFINE MAXDIGIT="5"
DEFINE MAXDIGIT="5"
INT ARRAY powers(MAXDIGIT+1)
INT ARRAY powers(MAXDIGIT+1)
Line 232: Line 232:
FI
FI
OD
OD
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Munchausen_numbers.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Munchausen_numbers.png Screenshot from Atari 8-bit computer]
Line 241: Line 241:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO;


procedure Munchausen is
procedure Munchausen is
Line 265: Line 265:
end loop;
end loop;
Ada.Text_IO.New_Line;
Ada.Text_IO.New_Line;
end Munchausen;</lang>
end Munchausen;</syntaxhighlight>


{{out}}
{{out}}
Line 271: Line 271:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<lang algol68># Find Munchausen Numbers between 1 and 5000 #
<syntaxhighlight lang="algol68"># Find Munchausen Numbers between 1 and 5000 #
# note that 6^6 is 46 656 so we only need to consider numbers consisting of 0 to 5 #
# note that 6^6 is 46 656 so we only need to consider numbers consisting of 0 to 5 #
Line 309: Line 309:
FI
FI
OD
OD
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 317: Line 317:


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.
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.
<lang algol68># Find all Munchausen numbers - note 11*(9^9) has only 10 digits so there are no #
<syntaxhighlight lang="algol68"># Find all Munchausen numbers - note 11*(9^9) has only 10 digits so there are no #
# Munchausen numbers with 11+ digits #
# Munchausen numbers with 11+ digits #
# table of Nth powers - note 0^0 is 0 for Munchausen numbers, not 1 #
# table of Nth powers - note 0^0 is 0 for Munchausen numbers, not 1 #
Line 375: Line 375:
OD
OD
OD
OD
OD</lang>
OD</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 386: Line 386:
=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
{{Trans|ALGOL 68}}
{{Trans|ALGOL 68}}
<lang algolw>% Find Munchausen Numbers between 1 and 5000 %
<syntaxhighlight lang="algolw">% Find Munchausen Numbers between 1 and 5000 %
% note that 6^6 is 46 656 so we only need to consider numbers consisting of 0 to 5 %
% note that 6^6 is 46 656 so we only need to consider numbers consisting of 0 to 5 %
begin
begin
Line 428: Line 428:
end
end


end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 438: Line 438:
{{works with|Dyalog APL}}
{{works with|Dyalog APL}}


<lang APL>(⊢(/⍨)⊢=+/∘(*⍨∘⍎¨⍕)¨)⍳ 5000</lang>
<syntaxhighlight lang="apl">(⊢(/⍨)⊢=+/∘(*⍨∘⍎¨⍕)¨)⍳ 5000</syntaxhighlight>


{{out}}
{{out}}
Line 446: Line 446:
=={{header|AppleScript}}==
=={{header|AppleScript}}==
===Functional===
===Functional===
<lang AppleScript>------------------- MUNCHAUSEN NUMBER ? --------------------
<syntaxhighlight lang="applescript">------------------- MUNCHAUSEN NUMBER ? --------------------


-- isMunchausen :: Int -> Bool
-- isMunchausen :: Int -> Bool
Line 525: Line 525:
end script
end script
end if
end if
end mReturn</lang>
end mReturn</syntaxhighlight>
{{Out}}
{{Out}}
<lang AppleScript>{1, 3435}</lang>
<syntaxhighlight lang="applescript">{1, 3435}</syntaxhighlight>


===Iterative===
===Iterative===
Line 533: Line 533:
More straightforwardly:
More straightforwardly:


<lang applescript>set MunchhausenNumbers to {}
<syntaxhighlight lang="applescript">set MunchhausenNumbers to {}
repeat with i from 1 to 5000
repeat with i from 1 to 5000
if (i > 0) then
if (i > 0) then
Line 546: Line 546:
end repeat
end repeat


return MunchhausenNumbers</lang>
return MunchhausenNumbers</syntaxhighlight>
{{Out}}
{{Out}}
<lang applescript>{1, 3435}</lang>
<syntaxhighlight lang="applescript">{1, 3435}</syntaxhighlight>


=={{header|Arturo}}==
=={{header|Arturo}}==
<lang rebol>munchausen?: function [n][
<syntaxhighlight lang="rebol">munchausen?: function [n][
n = sum map split to :string n 'digit [
n = sum map split to :string n 'digit [
d: to :integer digit
d: to :integer digit
Line 561: Line 561:
if munchausen? x ->
if munchausen? x ->
print x
print x
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 568: Line 568:
3435</pre>
3435</pre>
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang autohotkey>Loop, 5000
<syntaxhighlight lang="autohotkey">Loop, 5000
{
{
Loop, Parse, A_Index
Loop, Parse, A_Index
Line 576: Line 576:
var := 0
var := 0
}
}
Msgbox, %num%</lang>
Msgbox, %num%</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 584: Line 584:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f MUNCHAUSEN_NUMBERS.AWK
# syntax: GAWK -f MUNCHAUSEN_NUMBERS.AWK
BEGIN {
BEGIN {
Line 599: Line 599:
exit(0)
exit(0)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 608: Line 608:
=={{header|BASIC}}==
=={{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.
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.
<lang basic>10 DEF FN P(X)=INT(X^X*SGN(X))
<syntaxhighlight lang="basic">10 DEF FN P(X)=INT(X^X*SGN(X))
20 FOR I=0 TO 5
20 FOR I=0 TO 5
30 FOR J=0 TO 5
30 FOR J=0 TO 5
Line 619: Line 619:
100 NEXT K
100 NEXT K
110 NEXT J
110 NEXT J
120 NEXT I</lang>
120 NEXT I</syntaxhighlight>
{{out}}
{{out}}
<pre> 1
<pre> 1
Line 626: Line 626:
==={{header|Sinclair ZX81 BASIC}}===
==={{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.
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.
<lang basic> 10 FAST
<syntaxhighlight lang="basic"> 10 FAST
20 FOR I=0 TO 5
20 FOR I=0 TO 5
30 FOR J=0 TO 5
30 FOR J=0 TO 5
Line 638: Line 638:
110 NEXT J
110 NEXT J
120 NEXT I
120 NEXT I
130 SLOW</lang>
130 SLOW</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 644: Line 644:


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
<lang bbcbasic>REM >munchausen
<syntaxhighlight lang="bbcbasic">REM >munchausen
FOR i% = 0 TO 5
FOR i% = 0 TO 5
FOR j% = 0 TO 5
FOR j% = 0 TO 5
Line 662: Line 662:
= 0
= 0
ELSE
ELSE
= x% ^ x%</lang>
= x% ^ x%</syntaxhighlight>
{{out}}
{{out}}
<pre> 1
<pre> 1
Line 668: Line 668:


=={{header|BQN}}==
=={{header|BQN}}==
<lang bqn>Dgts ← •Fmt-'0'˙
<syntaxhighlight lang="bqn">Dgts ← •Fmt-'0'˙
IsMnch ← ⊢=+´∘(⋆˜ Dgts)
IsMnch ← ⊢=+´∘(⋆˜ Dgts)
IsMnch¨⊸/ 1+↕5000</lang>
IsMnch¨⊸/ 1+↕5000</syntaxhighlight>
{{out}}
{{out}}
<pre>⟨ 1 3435 ⟩</pre>
<pre>⟨ 1 3435 ⟩</pre>
Line 676: Line 676:
=={{header|C}}==
=={{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].
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].
<lang C>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
#include <math.h>


Line 698: Line 698:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 704: Line 704:


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>Func<char, int> toInt = c => c-'0';
<syntaxhighlight lang="csharp">Func<char, int> toInt = c => c-'0';


foreach (var i in Enumerable.Range(1,5000)
foreach (var i in Enumerable.Range(1,5000)
.Where(n => n == n.ToString()
.Where(n => n == n.ToString()
.Sum(x => Math.Pow(toInt(x), toInt(x)))))
.Sum(x => Math.Pow(toInt(x), toInt(x)))))
Console.WriteLine(i);</lang>
Console.WriteLine(i);</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 716: Line 716:
=== Faster version ===
=== Faster version ===
{{Trans|Kotlin}}
{{Trans|Kotlin}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;


namespace Munchhausen
namespace Munchhausen
Line 758: Line 758:
}
}
}
}
}</lang>
}</syntaxhighlight>
<pre>0
<pre>0
1
1
Line 766: Line 766:
{{trans|Visual Basic .NET}}
{{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.
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.
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;


static class Program
static class Program
Line 791: Line 791:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>0
<pre>0
Line 799: Line 799:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>
<syntaxhighlight lang="cpp">
#include <math.h>
#include <math.h>
#include <iostream>
#include <iostream>
Line 822: Line 822:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 832: Line 832:


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang lisp>(ns async-example.core
<syntaxhighlight lang="lisp">(ns async-example.core
(:require [clojure.math.numeric-tower :as math])
(:require [clojure.math.numeric-tower :as math])
(:use [criterium.core])
(:use [criterium.core])
Line 855: Line 855:


(println (find-numbers 5000))
(println (find-numbers 5000))
</syntaxhighlight>
</lang>
{{Output}}
{{Output}}
<pre>
<pre>
Line 862: Line 862:


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>digits = iter (n: int) yields (int)
<syntaxhighlight lang="clu">digits = iter (n: int) yields (int)
while n>0 do
while n>0 do
yield(n//10)
yield(n//10)
Line 883: Line 883:
if munchausen(i) then stream$putl(po, int$unparse(i)) end
if munchausen(i) then stream$putl(po, int$unparse(i)) end
end
end
end start_up</lang>
end start_up</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 889: Line 889:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>
<syntaxhighlight lang="lisp">
;;; check4munch maximum &optional b
;;; check4munch maximum &optional b
;;; Return a list with all Munchausen numbers less then or equal to maximum.
;;; Return a list with all Munchausen numbers less then or equal to maximum.
Line 924: Line 924:
(let ((dm (divmod n base)))
(let ((dm (divmod n base)))
(n2base (car dm) base (cons (cadr dm) digits)))))
(n2base (car dm) base (cons (cadr dm) digits)))))
</syntaxhighlight>
</lang>


{{Out}}
{{Out}}
Line 935: Line 935:


=={{header|COBOL}}==
=={{header|COBOL}}==
<lang cobol> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. MUNCHAUSEN.
PROGRAM-ID. MUNCHAUSEN.
Line 967: Line 967:
ADD-DIGIT-POWER.
ADD-DIGIT-POWER.
COMPUTE POWER-SUM =
COMPUTE POWER-SUM =
POWER-SUM + DIGITS(DIGIT) ** DIGITS(DIGIT)</lang>
POWER-SUM + DIGITS(DIGIT) ** DIGITS(DIGIT)</syntaxhighlight>
{{out}}
{{out}}
<pre> 1
<pre> 1
Line 973: Line 973:


=={{header|Cowgol}}==
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
<syntaxhighlight lang="cowgol">include "cowgol.coh";


sub digitPowerSum(n: uint16): (sum: uint32) is
sub digitPowerSum(n: uint16): (sum: uint32) is
Line 994: Line 994:
end if;
end if;
n := n + 1;
n := n + 1;
end loop;</lang>
end loop;</syntaxhighlight>


{{out}}
{{out}}
Line 1,003: Line 1,003:
=={{header|D}}==
=={{header|D}}==
{{trans|C}}
{{trans|C}}
<lang D>import std.stdio;
<syntaxhighlight lang="d">import std.stdio;


void main() {
void main() {
Line 1,023: Line 1,023:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 1,031: Line 1,031:
Needs a modern Dc due to <code>~</code>.
Needs a modern Dc due to <code>~</code>.
Use <code>S1S2l2l1/L2L1%</code> instead of <code>~</code> to run it in older Dcs.
Use <code>S1S2l2l1/L2L1%</code> instead of <code>~</code> to run it in older Dcs.
<lang dc>[ O ~ S! d 0!=M L! d ^ + ] sM
<syntaxhighlight lang="dc">[ O ~ S! d 0!=M L! d ^ + ] sM
[p] sp
[p] sp
[z d d lM x =p z 5001>L ] sL
[z d d lM x =p z 5001>L ] sL
lL x</lang>
lL x</syntaxhighlight>
Cosmetic: The stack is dirty after execution. The loop <code>L</code> needs a fix if that is a problem.
Cosmetic: The stack is dirty after execution. The loop <code>L</code> needs a fix if that is a problem.


Line 1,041: Line 1,041:


=={{header|Elixir}}==
=={{header|Elixir}}==
<lang elixir>defmodule Munchausen do
<syntaxhighlight lang="elixir">defmodule Munchausen do
@pow for i <- 0..9, into: %{}, do: {i, :math.pow(i,i) |> round}
@pow for i <- 0..9, into: %{}, do: {i, :math.pow(i,i) |> round}
Line 1,051: Line 1,051:
Enum.each(1..5000, fn i ->
Enum.each(1..5000, fn i ->
if Munchausen.number?(i), do: IO.puts i
if Munchausen.number?(i), do: IO.puts i
end)</lang>
end)</syntaxhighlight>


{{out}}
{{out}}
Line 1,060: Line 1,060:


=={{header|F sharp|F#}}==
=={{header|F sharp|F#}}==
<lang fsharp>let toFloat x = x |> int |> fun n -> n - 48 |> float
<syntaxhighlight lang="fsharp">let toFloat x = x |> int |> fun n -> n - 48 |> float
let power x = toFloat x ** toFloat x |> int
let power x = toFloat x ** toFloat x |> int
let isMunchausen n = n = (string n |> Seq.map char |> Seq.map power |> Seq.sum)
let isMunchausen n = n = (string n |> Seq.map char |> Seq.map power |> Seq.sum)


printfn "%A" ([1..5000] |> List.filter isMunchausen)</lang>
printfn "%A" ([1..5000] |> List.filter isMunchausen)</syntaxhighlight>
{{out}}
{{out}}
<pre>[1; 3435]</pre>
<pre>[1; 3435]</pre>


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: kernel math.functions math.ranges math.text.utils
<syntaxhighlight lang="factor">USING: kernel math.functions math.ranges math.text.utils
prettyprint sequences ;
prettyprint sequences ;


Line 1,075: Line 1,075:
dup 1 digit-groups dup [ ^ ] 2map sum = ;
dup 1 digit-groups dup [ ^ ] 2map sum = ;


5000 [1,b] [ munchausen? ] filter .</lang>
5000 [1,b] [ munchausen? ] filter .</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,082: Line 1,082:


=={{header|FALSE}}==
=={{header|FALSE}}==
<lang false>0[1+$5000>~][
<syntaxhighlight lang="false">0[1+$5000>~][
$$0\[$][
$$0\[$][
$10/$@\10*-
$10/$@\10*-
Line 1,092: Line 1,092:
]#
]#
%=[$.10,]?
%=[$.10,]?
]#%</lang>
]#%</syntaxhighlight>


{{out}}
{{out}}
Line 1,100: Line 1,100:


=={{header|FOCAL}}==
=={{header|FOCAL}}==
<lang FOCAL>01.10 F N=1,5000;D 2
<syntaxhighlight lang="focal">01.10 F N=1,5000;D 2


02.10 S M=N;S S=0
02.10 S M=N;S S=0
Line 1,109: Line 1,109:
02.50 I (N-S)2.7,2.6,2.7
02.50 I (N-S)2.7,2.6,2.7
02.60 T %4,N,!
02.60 T %4,N,!
02.70 R</lang>
02.70 R</syntaxhighlight>


{{out}}
{{out}}
Line 1,118: Line 1,118:
=={{header|Forth}}==
=={{header|Forth}}==
{{works with|GNU Forth|0.7.0}}
{{works with|GNU Forth|0.7.0}}
<lang forth>
<syntaxhighlight lang="forth">
: dig.num \ returns input number and the number of its digits ( n -- n n1 )
: dig.num \ returns input number and the number of its digits ( n -- n n1 )
dup
dup
Line 1,180: Line 1,180:
i check.num = if i . cr
i check.num = if i . cr
then loop ;
then loop ;
</lang>
</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,191: Line 1,191:
{{trans|360 Assembly}}
{{trans|360 Assembly}}
===Fortran IV===
===Fortran IV===
<lang fortran>C MUNCHAUSEN NUMBERS - FORTRAN IV
<syntaxhighlight lang="fortran">C MUNCHAUSEN NUMBERS - FORTRAN IV
DO 2 I=1,5000
DO 2 I=1,5000
IS=0
IS=0
Line 1,202: Line 1,202:
1 II=IR
1 II=IR
2 IF(IS.EQ.I) WRITE(*,*) I
2 IF(IS.EQ.I) WRITE(*,*) I
END </lang>
END </syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,209: Line 1,209:
</pre>
</pre>
===Fortran 77===
===Fortran 77===
<lang fortran>! MUNCHAUSEN NUMBERS - FORTRAN 77
<syntaxhighlight lang="fortran">! MUNCHAUSEN NUMBERS - FORTRAN 77
DO I=1,5000
DO I=1,5000
IS=0
IS=0
Line 1,222: Line 1,222:
IF(IS.EQ.I) WRITE(*,*) I
IF(IS.EQ.I) WRITE(*,*) I
END DO
END DO
END </lang>
END </syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,231: Line 1,231:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
===Version 1===
===Version 1===
<lang freebasic>' FB 1.05.0 Win64
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
' Cache n ^ n for the digits 1 to 9
' Cache n ^ n for the digits 1 to 9
' Note than 0 ^ 0 specially treated as 0 (not 1) for this purpose
' Note than 0 ^ 0 specially treated as 0 (not 1) for this purpose
Line 1,262: Line 1,262:
Print "Press any key to quit"
Print "Press any key to quit"


Sleep</lang>
Sleep</syntaxhighlight>
{{out}}
{{out}}
<pre>The Munchausen numbers between 0 and 500000000 are :
<pre>The Munchausen numbers between 0 and 500000000 are :
Line 1,270: Line 1,270:
438579088</pre>
438579088</pre>
===Version 2===
===Version 2===
<lang freebasic>' version 12-10-2017
<syntaxhighlight lang="freebasic">' version 12-10-2017
' compile with: fbc -s console
' compile with: fbc -s console


Line 1,334: Line 1,334:
Print : Print "hit any key to end program"
Print : Print "hit any key to end program"
Sleep
Sleep
End</lang>
End</syntaxhighlight>
{{out}}
{{out}}
<pre>0
<pre>0
Line 1,342: Line 1,342:


=={{header|Frink}}==
=={{header|Frink}}==
<lang frink>isMunchausen = { |x|
<syntaxhighlight lang="frink">isMunchausen = { |x|
sum = 0
sum = 0
for d = integerDigits[x]
for d = integerDigits[x]
Line 1,349: Line 1,349:
}
}


println[select[1 to 5000, isMunchausen]]</lang>
println[select[1 to 5000, isMunchausen]]</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,365: Line 1,365:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang go>package main
<syntaxhighlight lang="go">package main


import(
import(
Line 1,400: Line 1,400:
}
}
fmt.Println()
fmt.Println()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,408: Line 1,408:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Control.Monad (join)
<syntaxhighlight lang="haskell">import Control.Monad (join)
import Data.List (unfoldr)
import Data.List (unfoldr)


Line 1,420: Line 1,420:


main :: IO ()
main :: IO ()
main = print $ filter isMunchausen [1 .. 5000]</lang>
main = print $ filter isMunchausen [1 .. 5000]</syntaxhighlight>
{{out}}
{{out}}
<pre>[1,3435]</pre>
<pre>[1,3435]</pre>
Line 1,426: Line 1,426:
The Haskell libraries provide a lot of flexibility – we could also reduce the sum and map (above) down to a single foldr:
The Haskell libraries provide a lot of flexibility – we could also reduce the sum and map (above) down to a single foldr:


<lang haskell>import Data.Char (digitToInt)
<syntaxhighlight lang="haskell">import Data.Char (digitToInt)


isMunchausen :: Int -> Bool
isMunchausen :: Int -> Bool
Line 1,434: Line 1,434:


main :: IO ()
main :: IO ()
main = print $ filter isMunchausen [1 .. 5000]</lang>
main = print $ filter isMunchausen [1 .. 5000]</syntaxhighlight>


Or, without digitToInt, but importing join, swap and bool.
Or, without digitToInt, but importing join, swap and bool.
<lang haskell>import Control.Monad (join)
<syntaxhighlight lang="haskell">import Control.Monad (join)
import Data.Bool (bool)
import Data.Bool (bool)
import Data.List (unfoldr)
import Data.List (unfoldr)
Line 1,457: Line 1,457:


main :: IO ()
main :: IO ()
main = print $ filter isMunchausen [1 .. 5000]</lang>
main = print $ filter isMunchausen [1 .. 5000]</syntaxhighlight>


{{Out}}
{{Out}}
Line 1,466: Line 1,466:
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:
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:


<lang J> munch=: +/@(^~@(10&#.inv))
<syntaxhighlight lang="j"> munch=: +/@(^~@(10&#.inv))
(#~ ] = munch"0) 1+i.5000
(#~ ] = munch"0) 1+i.5000
1 3435</lang>
1 3435</syntaxhighlight>


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:
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:


<lang J> munch=: +/@((**^~)@(10&#.inv))
<syntaxhighlight lang="j"> munch=: +/@((**^~)@(10&#.inv))
(#~ ] = munch"0) 1+i.5000
(#~ ] = munch"0) 1+i.5000
1 3435</lang>
1 3435</syntaxhighlight>


=={{header|Java}}==
=={{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].
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">
<lang Java>
public class Main {
public class Main {
public static void main(String[] args) {
public static void main(String[] args) {
Line 1,490: Line 1,490:
}
}


</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>1 (munchausen)
<pre>1 (munchausen)
Line 1,497: Line 1,497:
=== Faster version ===
=== Faster version ===
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang java>public class Munchhausen {
<syntaxhighlight lang="java">public class Munchhausen {


static final long[] cache = new long[10];
static final long[] cache = new long[10];
Line 1,525: Line 1,525:
return sum == n;
return sum == n;
}
}
}</lang>
}</syntaxhighlight>
<pre>0
<pre>0
1
1
Line 1,535: Line 1,535:
===ES6===
===ES6===


<lang javascript>for (let i of [...Array(5000).keys()]
<syntaxhighlight lang="javascript">for (let i of [...Array(5000).keys()]
.filter(n => n == n.toString().split('')
.filter(n => n == n.toString().split('')
.reduce((a, b) => a+Math.pow(parseInt(b),parseInt(b)), 0)))
.reduce((a, b) => a+Math.pow(parseInt(b),parseInt(b)), 0)))
console.log(i);</lang>
console.log(i);</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 1,546: Line 1,546:
Or, composing reusable primitives:
Or, composing reusable primitives:


<lang JavaScript>(() => {
<syntaxhighlight lang="javascript">(() => {
'use strict';
'use strict';


Line 1,577: Line 1,577:
// MAIN ---
// MAIN ---
return main();
return main();
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<lang JavaScript>[1, 3435]</lang>
<syntaxhighlight lang="javascript">[1, 3435]</syntaxhighlight>


=={{header|jq}}==
=={{header|jq}}==
{{works with|jq|1.5}}
{{works with|jq|1.5}}
<lang jq>def sigma( stream ): reduce stream as $x (0; . + $x ) ;
<syntaxhighlight lang="jq">def sigma( stream ): reduce stream as $x (0; . + $x ) ;


def ismunchausen:
def ismunchausen:
Line 1,590: Line 1,590:


# Munchausen numbers from 1 to 5000 inclusive:
# Munchausen numbers from 1 to 5000 inclusive:
range(1;5001) | select(ismunchausen)</lang>
range(1;5001) | select(ismunchausen)</syntaxhighlight>
{{out}}
{{out}}
<lang jq>1
<syntaxhighlight lang="jq">1
3435</lang>
3435</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|1.0}}
{{works with|Julia|1.0}}
<lang julia>println([n for n = 1:5000 if sum(d^d for d in digits(n)) == n])</lang>
<syntaxhighlight lang="julia">println([n for n = 1:5000 if sum(d^d for d in digits(n)) == n])</syntaxhighlight>


{{out}}
{{out}}
Line 1,604: Line 1,604:
=={{header|Kotlin}}==
=={{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:
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:
<lang scala>// version 1.0.6
<syntaxhighlight lang="scala">// version 1.0.6


val powers = IntArray(10)
val powers = IntArray(10)
Line 1,628: Line 1,628:
for (i in 0..500000000) if (isMunchausen(i))print ("$i ")
for (i in 0..500000000) if (isMunchausen(i))print ("$i ")
println()
println()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,637: Line 1,637:


=={{header|Lambdatalk}}==
=={{header|Lambdatalk}}==
<lang scheme>
<syntaxhighlight lang="scheme">
{def munch
{def munch
{lambda {:w}
{lambda {:w}
Line 1,650: Line 1,650:
1
1
3435
3435
</syntaxhighlight>
</lang>


=={{header|langur}}==
=={{header|langur}}==
{{trans|C#}}
{{trans|C#}}
{{works with|langur|0.6.6}}
{{works with|langur|0.6.6}}
<lang langur># sum power of digits
<syntaxhighlight lang="langur"># sum power of digits
val .spod = f(.n) fold f{+}, map(f (.x-'0') ^ (.x-'0'), s2cp toString .n)
val .spod = f(.n) fold f{+}, map(f (.x-'0') ^ (.x-'0'), s2cp toString .n)


# Munchausen
# Munchausen
writeln "Answers: ", where f(.n) .n == .spod(.n), series 0..5000</lang>
writeln "Answers: ", where f(.n) .n == .spod(.n), series 0..5000</syntaxhighlight>


{{works with|langur|0.8.10}}
{{works with|langur|0.8.10}}
<lang langur># sum power of digits
<syntaxhighlight lang="langur"># sum power of digits
val .spod = f(.n) fold f{+}, map(f .x^.x, s2n toString .n)
val .spod = f(.n) fold f{+}, map(f .x^.x, s2n toString .n)


# Munchausen
# Munchausen
writeln "Answers: ", where f(.n) .n == .spod(.n), series 0..5000</lang>
writeln "Answers: ", where f(.n) .n == .spod(.n), series 0..5000</syntaxhighlight>


{{out}}
{{out}}
Line 1,672: Line 1,672:


=={{header|Lua}}==
=={{header|Lua}}==
<lang Lua>function isMunchausen (n)
<syntaxhighlight lang="lua">function isMunchausen (n)
local sum, nStr, digit = 0, tostring(n)
local sum, nStr, digit = 0, tostring(n)
for pos = 1, #nStr do
for pos = 1, #nStr do
Line 1,695: Line 1,695:
for i = 1, 5000 do
for i = 1, 5000 do
if isMunchausen(i) then print(i) end
if isMunchausen(i) then print(i) end
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 1,701: Line 1,701:


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Munchausen {
Module Munchausen {
Inventory p=0:=0,1:=1
Inventory p=0:=0,1:=1
Line 1,720: Line 1,720:
}
}
Munchausen
Munchausen
</syntaxhighlight>
</lang>
Using Array instead of Inventory
Using Array instead of Inventory
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Münchhausen {
Module Münchhausen {
Dim p(0 to 9)
Dim p(0 to 9)
Line 1,742: Line 1,742:
}
}
Münchhausen
Münchhausen
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,749: Line 1,749:


=={{header|MAD}}==
=={{header|MAD}}==
<lang MAD> NORMAL MODE IS INTEGER
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
DIMENSION P(5)
DIMENSION P(5)
Line 1,767: Line 1,767:


VECTOR VALUES FMT = $I4*$
VECTOR VALUES FMT = $I4*$
END OF PROGRAM </lang>
END OF PROGRAM </syntaxhighlight>


{{out}}
{{out}}
Line 1,775: Line 1,775:


=={{header|Maple}}==
=={{header|Maple}}==
<lang Maple>isMunchausen := proc(n::posint)
<syntaxhighlight lang="maple">isMunchausen := proc(n::posint)
local num_digits;
local num_digits;
num_digits := map(x -> StringTools:-Ord(x) - 48, StringTools:-Explode(convert(n, string)));
num_digits := map(x -> StringTools:-Ord(x) - 48, StringTools:-Explode(convert(n, string)));
Line 1,791: Line 1,791:
end proc;
end proc;


Munchausen_upto(5000);</lang>
Munchausen_upto(5000);</syntaxhighlight>
{{out}}
{{out}}
<pre>[1, 3435]</pre>
<pre>[1, 3435]</pre>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>Off[Power::indet];(*Supress 0^0 warnings*)
<syntaxhighlight lang="mathematica">Off[Power::indet];(*Supress 0^0 warnings*)
Select[Range[5000], Total[IntegerDigits[#]^IntegerDigits[#]] == # &]</lang>
Select[Range[5000], Total[IntegerDigits[#]^IntegerDigits[#]] == # &]</syntaxhighlight>
{{out}}
{{out}}
<pre>{1,3435}</pre>
<pre>{1,3435}</pre>
Line 1,803: Line 1,803:
=={{header|min}}==
=={{header|min}}==
{{works with|min|0.19.3}}
{{works with|min|0.19.3}}
<lang min>(dup string "" split (int dup pow) (+) map-reduce ==) :munchausen?
<syntaxhighlight lang="min">(dup string "" split (int dup pow) (+) map-reduce ==) :munchausen?
1 :i
1 :i
(i 5000 <=) ((i munchausen?) (i puts!) when i succ @i) while</lang>
(i 5000 <=) ((i munchausen?) (i puts!) when i succ @i) while</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,813: Line 1,813:


=={{header|Modula-2}}==
=={{header|Modula-2}}==
<lang modula2>MODULE MunchausenNumbers;
<syntaxhighlight lang="modula2">MODULE MunchausenNumbers;
FROM FormatString IMPORT FormatString;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,ReadChar;
FROM Terminal IMPORT WriteString,ReadChar;
Line 1,858: Line 1,858:


ReadChar;
ReadChar;
END MunchausenNumbers.</lang>
END MunchausenNumbers.</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>import math
<syntaxhighlight lang="nim">import math


for i in 1..<5000:
for i in 1..<5000:
Line 1,871: Line 1,871:
number = number div 10
number = number div 10
if sum == i:
if sum == i:
echo i</lang>
echo i</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 1,881: Line 1,881:
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.
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.
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.
<lang pascal>{$IFDEF FPC}{$MODE objFPC}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF}
<syntaxhighlight lang="pascal">{$IFDEF FPC}{$MODE objFPC}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF}
uses
uses
sysutils;
sysutils;
Line 1,953: Line 1,953:
writeln('Check Count ',cnt);
writeln('Check Count ',cnt);
end.
end.
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre> 1 000000001
<pre> 1 000000001
Line 1,966: Line 1,966:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use List::Util "sum";
<syntaxhighlight lang="perl">use List::Util "sum";
for my $n (1..5000) {
for my $n (1..5000) {
print "$n\n" if $n == sum( map { $_**$_ } split(//,$n) );
print "$n\n" if $n == sum( map { $_**$_ } split(//,$n) );
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 1,975: Line 1,975:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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>
Line 1,992: Line 1,992:
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,000: Line 2,000:


=={{header|PHP}}==
=={{header|PHP}}==
<lang php>
<syntaxhighlight lang="php">
<?php
<?php


Line 2,025: Line 2,025:
echo $i . PHP_EOL;
echo $i . PHP_EOL;
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,032: Line 2,032:


=={{header|Picat}}==
=={{header|Picat}}==
<lang Picat>go =>
<syntaxhighlight lang="picat">go =>
println([N : N in 1..5000, munchhausen_number(N)]).
println([N : N in 1..5000, munchhausen_number(N)]).


munchhausen_number(N) =>
munchhausen_number(N) =>
N == sum([T : I in N.to_string(),II = I.to_int(), T = II**II]).</lang>
N == sum([T : I in N.to_string(),II = I.to_int(), T = II**II]).</syntaxhighlight>


{{out}}
{{out}}
Line 2,042: Line 2,042:


Testing for a larger interval, 1..500 000 000, requires another approach:
Testing for a larger interval, 1..500 000 000, requires another approach:
<lang Picat>go2 ?=>
<syntaxhighlight lang="picat">go2 ?=>
H = [0] ++ [I**I : I in 1..9],
H = [0] ++ [I**I : I in 1..9],
N = 1,
N = 1,
Line 2,062: Line 2,062:
end,
end,
nl.
nl.
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,071: Line 2,071:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(for N 5000
<syntaxhighlight lang="picolisp">(for N 5000
(and
(and
(=
(=
Line 2,078: Line 2,078:
'((N) (** N N))
'((N) (** N N))
(mapcar format (chop N)) ) )
(mapcar format (chop N)) ) )
(println N) ) )</lang>
(println N) ) )</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,085: Line 2,085:


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang pli>munchausen: procedure options(main);
<syntaxhighlight lang="pli">munchausen: procedure options(main);
/* precalculate powers */
/* precalculate powers */
declare (pows(0:5), i) fixed;
declare (pows(0:5), i) fixed;
Line 2,103: Line 2,103:
end;
end;
end;
end;
end munchausen;</lang>
end munchausen;</syntaxhighlight>
{{out}}
{{out}}
<pre> 1
<pre> 1
Line 2,109: Line 2,109:


=={{header|Plain English}}==
=={{header|Plain English}}==
<lang plainenglish>To run:
<syntaxhighlight lang="plainenglish">To run:
Start up.
Start up.
Show the Munchausen numbers up to 5000.
Show the Munchausen numbers up to 5000.
Line 2,136: Line 2,136:
If the number is 0, break.
If the number is 0, break.
Repeat.
Repeat.
Put the sum into the number.</lang>
Put the sum into the number.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,145: Line 2,145:
=={{header|PowerBASIC}}==
=={{header|PowerBASIC}}==
{{trans|FreeBASIC}}(Translated from the FreeBasic Version 2 example.)
{{trans|FreeBASIC}}(Translated from the FreeBasic Version 2 example.)
<lang powerbasic>#COMPILE EXE
<syntaxhighlight lang="powerbasic">#COMPILE EXE
#DIM ALL
#DIM ALL
#COMPILER PBCC 6
#COMPILER PBCC 6
Line 2,217: Line 2,217:
CON.PRINT "execution time:" & STR$(t) & " ms; hit any key to end program"
CON.PRINT "execution time:" & STR$(t) & " ms; hit any key to end program"
CON.WAITKEY$
CON.WAITKEY$
END FUNCTION</lang>
END FUNCTION</syntaxhighlight>
{{out}}
{{out}}
<pre> 0
<pre> 0
Line 2,226: Line 2,226:


=={{header|Pure}}==
=={{header|Pure}}==
<lang Pure>// split numer into digits
<syntaxhighlight lang="pure">// split numer into digits
digits n::number = loop n [] with
digits n::number = loop n [] with
loop n l = loop (n div 10) ((n mod 10):l) if n > 0;
loop n l = loop (n div 10) ((n mod 10):l) if n > 0;
Line 2,236: Line 2,236:
(map (\d -> d^d)
(map (\d -> d^d)
(digits n)); end;
(digits n)); end;
munchausen 5000;</lang>
munchausen 5000;</syntaxhighlight>
{{out}}
{{out}}
<pre>[1,3435]</pre>
<pre>[1,3435]</pre>
Line 2,242: Line 2,242:
=={{header|PureBasic}}==
=={{header|PureBasic}}==
{{trans|C}}
{{trans|C}}
<lang PureBasic>EnableExplicit
<syntaxhighlight lang="purebasic">EnableExplicit
Declare main()
Declare main()


Line 2,266: Line 2,266:
EndIf
EndIf
Next
Next
EndProcedure</lang>
EndProcedure</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 2,272: Line 2,272:


=={{header|Python}}==
=={{header|Python}}==
<lang python>for i in range(5000):
<syntaxhighlight lang="python">for i in range(5000):
if i == sum(int(x) ** int(x) for x in str(i)):
if i == sum(int(x) ** int(x) for x in str(i)):
print(i)</lang>
print(i)</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 2,285: Line 2,285:


{{Works with|Python|3}}
{{Works with|Python|3}}
<lang python>'''Munchausen numbers'''
<syntaxhighlight lang="python">'''Munchausen numbers'''


from functools import (reduce)
from functools import (reduce)
Line 2,340: Line 2,340:


if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
<pre>[1, 3435]</pre>
<pre>[1, 3435]</pre>


=={{header|Quackery}}==
=={{header|Quackery}}==


<lang Quackery> [ dup 0 swap
<syntaxhighlight lang="quackery"> [ dup 0 swap
[ dup 0 != while
[ dup 0 != while
10 /mod dup **
10 /mod dup **
Line 2,353: Line 2,353:
5000 times
5000 times
[ i^ 1+ munchausen if
[ i^ 1+ munchausen if
[ i^ 1+ echo sp ] ]</lang>
[ i^ 1+ echo sp ] ]</syntaxhighlight>


{{out}}
{{out}}
Line 2,361: Line 2,361:
=={{header|Racket}}==
=={{header|Racket}}==


<lang>#lang racket
<syntaxhighlight lang="text">#lang racket


(define (expt:0^0=1 r p)
(define (expt:0^0=1 r p)
Line 2,386: Line 2,386:
(check-true (munchausen-number? 3435))
(check-true (munchausen-number? 3435))
(check-false (munchausen-number? 3))
(check-false (munchausen-number? 3))
(check-false (munchausen-number? -45) "no recursion on -ve numbers"))</lang>
(check-false (munchausen-number? -45) "no recursion on -ve numbers"))</syntaxhighlight>


{{out}}
{{out}}
Line 2,394: Line 2,394:
=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<lang perl6>sub is_munchausen ( Int $n ) {
<syntaxhighlight lang="raku" line>sub is_munchausen ( Int $n ) {
constant @powers = 0, |map { $_ ** $_ }, 1..9;
constant @powers = 0, |map { $_ ** $_ }, 1..9;
$n == @powers[$n.comb].sum;
$n == @powers[$n.comb].sum;
}
}
.say if .&is_munchausen for 1..5000;</lang>
.say if .&is_munchausen for 1..5000;</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 2,405: Line 2,405:
=={{header|REXX}}==
=={{header|REXX}}==
===version 1===
===version 1===
<lang rexx>Do n=0 To 10000
<syntaxhighlight lang="rexx">Do n=0 To 10000
If n=m(n) Then
If n=m(n) Then
Say n
Say n
Line 2,416: Line 2,416:
res=res+c**c
res=res+c**c
End
End
Return res</lang>
Return res</syntaxhighlight>
{{out}}
{{out}}
<pre>D:\mau>rexx munch
<pre>D:\mau>rexx munch
Line 2,429: Line 2,429:


For the high limit of &nbsp; '''5,000''', &nbsp; optimization isn't needed. &nbsp; But for much higher limits, optimization becomes significant.
For the high limit of &nbsp; '''5,000''', &nbsp; optimization isn't needed. &nbsp; But for much higher limits, optimization becomes significant.
<lang rexx>/*REXX program finds and displays Münchhausen numbers from one to a specified number (Z)*/
<syntaxhighlight lang="rexx">/*REXX program finds and displays Münchhausen numbers from one to a specified number (Z)*/
@.= 0; do i=1 for 9; @.i= i**i; end /*precompute powers for non-zero digits*/
@.= 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.*/
parse arg z . /*obtain optional argument from the CL.*/
Line 2,441: Line 2,441:
parse var x _ +1 x; $= $ + @._ /*add the next power*/
parse var x _ +1 x; $= $ + @._ /*add the next power*/
end /*while*/ /* [↑] get a digit.*/
end /*while*/ /* [↑] get a digit.*/
return $==ox /*it is or it ain't.*/</lang>
return $==ox /*it is or it ain't.*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 2,450: Line 2,450:
===version 3===
===version 3===
It is about &nbsp; '''3''' &nbsp; times faster than REXX version 1.
It is about &nbsp; '''3''' &nbsp; times faster than REXX version 1.
<lang rexx>/*REXX program finds and displays Münchhausen numbers from one to a specified number (Z)*/
<syntaxhighlight lang="rexx">/*REXX program finds and displays Münchhausen numbers from one to a specified number (Z)*/
@.= 0; do i=1 for 9; @.i= i**i; end /*precompute powers for non-zero digits*/
@.= 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.*/
parse arg z . /*obtain optional argument from the CL.*/
Line 2,464: Line 2,464:
parse var x _ +1 x; $= $ + @._ /*sum 6th & up digs*/
parse var x _ +1 x; $= $ + @._ /*sum 6th & up digs*/
end /*while*/
end /*while*/
return $==ox /*it is or it ain't*/</lang>
return $==ox /*it is or it ain't*/</syntaxhighlight>
{{out|output|text=&nbsp; is the same as the 2<sup>nd</sup> REXX version.}} <br><br>
{{out|output|text=&nbsp; is the same as the 2<sup>nd</sup> REXX version.}} <br><br>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Munchausen numbers
# Project : Munchausen numbers


Line 2,484: Line 2,484:
ok
ok
next
next
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 2,492: Line 2,492:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>class Integer
<syntaxhighlight lang="ruby">class Integer


def munchausen?
def munchausen?
Line 2,500: Line 2,500:
end
end


puts (1..5000).select(&:munchausen?)</lang>
puts (1..5000).select(&:munchausen?)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,508: Line 2,508:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>fn main() {
<syntaxhighlight lang="rust">fn main() {
let mut solutions = Vec::new();
let mut solutions = Vec::new();


Line 2,526: Line 2,526:


println!("Munchausen numbers below 5_000 : {:?}", solutions);
println!("Munchausen numbers below 5_000 : {:?}", solutions);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,534: Line 2,534:
=={{header|Scala}}==
=={{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].
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">
<lang Scala>
object Munch {
object Munch {
def main(args: Array[String]): Unit = {
def main(args: Array[String]): Unit = {
Line 2,544: Line 2,544:
}
}
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>1 (munchausen)
<pre>1 (munchausen)
Line 2,550: Line 2,550:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func is_munchausen(n) {
<syntaxhighlight lang="ruby">func is_munchausen(n) {
n.digits.map{|d| d**d }.sum == n
n.digits.map{|d| d**d }.sum == n
}
}


say (1..5000 -> grep(is_munchausen))</lang>
say (1..5000 -> grep(is_munchausen))</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,561: Line 2,561:


=={{header|SuperCollider}}==
=={{header|SuperCollider}}==
<lang supercollider>(1..5000).select { |n| n == n.asDigits.sum { |x| pow(x, x) } }</lang>
<syntaxhighlight lang="supercollider">(1..5000).select { |n| n == n.asDigits.sum { |x| pow(x, x) } }</syntaxhighlight>


<pre>
<pre>
Line 2,568: Line 2,568:


=={{header|Swift}}==
=={{header|Swift}}==
<lang swift>import Foundation
<syntaxhighlight lang="swift">import Foundation


func isMünchhausen(_ n: Int) -> Bool {
func isMünchhausen(_ n: Int) -> Bool {
Line 2,578: Line 2,578:
for i in 1...5000 where isMünchhausen(i) {
for i in 1...5000 where isMünchhausen(i) {
print(i)
print(i)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 2,584: Line 2,584:


=={{header|Symsyn}}==
=={{header|Symsyn}}==
<lang symsyn>
<syntaxhighlight lang="symsyn">
x : 10 1
x : 10 1
Line 2,615: Line 2,615:
endif
endif


</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>1
<pre>1
Line 2,623: Line 2,623:
{{works with|TI-83 BASIC|TI-84Plus 2.55MP}}
{{works with|TI-83 BASIC|TI-84Plus 2.55MP}}
{{trans|Fortran}}
{{trans|Fortran}}
<lang ti83b> For(I,1,5000)
<syntaxhighlight lang="ti83b"> For(I,1,5000)
0→S:I→K
0→S:I→K
For(J,1,4)
For(J,1,4)
Line 2,633: Line 2,633:
End
End
If S=I:Disp I
If S=I:Disp I
End </lang>
End </syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,644: Line 2,644:
{{trans|BASIC}}
{{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.
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.
<lang ti83b>{1,1,4,27,256,3125}→L₁
<syntaxhighlight lang="ti83b">{1,1,4,27,256,3125}→L₁
For(A,0,5,1)
For(A,0,5,1)
For(B,0,5,1)
For(B,0,5,1)
Line 2,662: Line 2,662:
End
End
End
End
End</lang>
End</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,672: Line 2,672:
=={{header|VBA}}==
=={{header|VBA}}==


<syntaxhighlight lang="vb">
<lang vb>
Option Explicit
Option Explicit


Line 2,692: Line 2,692:
IsMunchausen = (Tot = Number)
IsMunchausen = (Tot = Number)
End Function
End Function
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>1 is a munchausen number.
<pre>1 is a munchausen number.
Line 2,698: Line 2,698:


=={{header|VBScript}}==
=={{header|VBScript}}==
<lang vbscript>
<syntaxhighlight lang="vbscript">
for i = 1 to 5000
for i = 1 to 5000
if Munch(i) Then
if Munch(i) Then
Line 2,724: Line 2,724:


End Function
End Function
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,733: Line 2,733:
=={{header|Visual Basic}}==
=={{header|Visual Basic}}==
{{trans|FreeBASIC}}(Translated from the FreeBasic Version 2 example.)
{{trans|FreeBASIC}}(Translated from the FreeBasic Version 2 example.)
<lang vb>Option Explicit
<syntaxhighlight lang="vb">Option Explicit


Declare Function GetTickCount Lib "kernel32.dll" () As Long
Declare Function GetTickCount Lib "kernel32.dll" () As Long
Line 2,839: Line 2,839:
res = res & "execution time:" & Str$(t) & " ms"
res = res & "execution time:" & Str$(t) & " ms"
MsgBox res
MsgBox res
End Sub</lang>
End Sub</syntaxhighlight>
{{out}}
{{out}}
<pre> 0
<pre> 0
Line 2,850: Line 2,850:
{{trans|FreeBASIC}}(Translated from the FreeBasic Version 2 example.)<br/>
{{trans|FreeBASIC}}(Translated from the FreeBasic Version 2 example.)<br/>
Computation time is under 4 seconds on tio.run.
Computation time is under 4 seconds on tio.run.
<lang vbnet>Imports System
<syntaxhighlight lang="vbnet">Imports System


Module Program
Module Program
Line 2,874: Line 2,874:
Next
Next
End Sub
End Sub
End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre>0
<pre>0
Line 2,882: Line 2,882:


=={{header|Wren}}==
=={{header|Wren}}==
<lang ecmascript>var powers = List.filled(10, 0)
<syntaxhighlight lang="ecmascript">var powers = List.filled(10, 0)
for (i in 1..9) powers[i] = i.pow(i).round // cache powers
for (i in 1..9) powers[i] = i.pow(i).round // cache powers


Line 2,901: Line 2,901:
for (i in 1..5000) {
for (i in 1..5000) {
if (munchausen.call(i)) System.print(i)
if (munchausen.call(i)) System.print(i)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,912: Line 2,912:
=={{header|XPL0}}==
=={{header|XPL0}}==
The digits 6, 7, 8 and 9 can't occur because 6^6 = 46656, which is beyond 5000.
The digits 6, 7, 8 and 9 can't occur because 6^6 = 46656, which is beyond 5000.
<lang XPL0>int Pow, A, B, C, D, N;
<syntaxhighlight lang="xpl0">int Pow, A, B, C, D, N;
[Pow:= [0, 1, 4, 27, 256, 3125];
[Pow:= [0, 1, 4, 27, 256, 3125];
for A:= 0 to 5 do
for A:= 0 to 5 do
Line 2,923: Line 2,923:
[IntOut(0, N); CrLf(0)];
[IntOut(0, N); CrLf(0)];
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 2,932: Line 2,932:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>[1..5000].filter(fcn(n){ n==n.split().reduce(fcn(s,n){ s + n.pow(n) },0) })
<syntaxhighlight lang="zkl">[1..5000].filter(fcn(n){ n==n.split().reduce(fcn(s,n){ s + n.pow(n) },0) })
.println();</lang>
.println();</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>