Jump to content

Munchausen numbers: Difference between revisions

m
syntax highlighting fixup automation
(Frink)
m (syntax highlighting fixup automation)
Line 20:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">L(i) 5000
I i == sum(String(i).map(x -> Int(x) ^ Int(x)))
print(i)</langsyntaxhighlight>
 
{{out}}
Line 31:
 
=={{header|360 Assembly}}==
<langsyntaxhighlight lang="360asm">* Munchausen numbers 16/03/2019
MUNCHAU CSECT
USING MUNCHAU,R12 base register
Line 62:
PG DC CL12' ' buffer
REGEQU
END MUNCHAU </langsyntaxhighlight>
{{out}}
<pre>
Line 70:
 
=={{header|8080 Assembly}}==
<langsyntaxhighlight lang="8080asm">putch: equ 2 ; CP/M syscall to print character
puts: equ 9 ; CP/M syscall to print string
org 100h
Line 176:
pop d ; Restore DE
ret
dpow: dw 1,1,4,27,256,3125 ; 0^0 to 5^5 lookup table</langsyntaxhighlight>
 
{{out}}
Line 184:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">;there are considered digits 0-5 because 6^6>5000
DEFINE MAXDIGIT="5"
INT ARRAY powers(MAXDIGIT+1)
Line 232:
FI
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Munchausen_numbers.png Screenshot from Atari 8-bit computer]
Line 241:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Munchausen is
Line 265:
end loop;
Ada.Text_IO.New_Line;
end Munchausen;</langsyntaxhighlight>
 
{{out}}
Line 271:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight 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 #
Line 309:
FI
OD
</syntaxhighlight>
</lang>
{{out}}
<pre>
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.
<langsyntaxhighlight lang="algol68"># Find all Munchausen numbers - note 11*(9^9) has only 10 digits so there are no #
# Munchausen numbers with 11+ digits #
# table of Nth powers - note 0^0 is 0 for Munchausen numbers, not 1 #
Line 375:
OD
OD
OD</langsyntaxhighlight>
{{out}}
<pre>
Line 386:
=={{header|ALGOL W}}==
{{Trans|ALGOL 68}}
<langsyntaxhighlight 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 %
begin
Line 428:
end
 
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 438:
{{works with|Dyalog APL}}
 
<langsyntaxhighlight APLlang="apl">(⊢(/⍨)⊢=+/∘(*⍨∘⍎¨⍕)¨)⍳ 5000</langsyntaxhighlight>
 
{{out}}
Line 446:
=={{header|AppleScript}}==
===Functional===
<langsyntaxhighlight AppleScriptlang="applescript">------------------- MUNCHAUSEN NUMBER ? --------------------
 
-- isMunchausen :: Int -> Bool
Line 525:
end script
end if
end mReturn</langsyntaxhighlight>
{{Out}}
<syntaxhighlight lang AppleScript="applescript">{1, 3435}</langsyntaxhighlight>
 
===Iterative===
Line 533:
More straightforwardly:
 
<langsyntaxhighlight lang="applescript">set MunchhausenNumbers to {}
repeat with i from 1 to 5000
if (i > 0) then
Line 546:
end repeat
 
return MunchhausenNumbers</langsyntaxhighlight>
{{Out}}
<syntaxhighlight lang ="applescript">{1, 3435}</langsyntaxhighlight>
 
=={{header|Arturo}}==
<langsyntaxhighlight lang="rebol">munchausen?: function [n][
n = sum map split to :string n 'digit [
d: to :integer digit
Line 561:
if munchausen? x ->
print x
]</langsyntaxhighlight>
 
{{out}}
Line 568:
3435</pre>
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">Loop, 5000
{
Loop, Parse, A_Index
Line 576:
var := 0
}
Msgbox, %num%</langsyntaxhighlight>
{{out}}
<pre>
Line 584:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f MUNCHAUSEN_NUMBERS.AWK
BEGIN {
Line 599:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 608:
=={{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.
<langsyntaxhighlight lang="basic">10 DEF FN P(X)=INT(X^X*SGN(X))
20 FOR I=0 TO 5
30 FOR J=0 TO 5
Line 619:
100 NEXT K
110 NEXT J
120 NEXT I</langsyntaxhighlight>
{{out}}
<pre> 1
Line 626:
==={{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.
<langsyntaxhighlight lang="basic"> 10 FAST
20 FOR I=0 TO 5
30 FOR J=0 TO 5
Line 638:
110 NEXT J
120 NEXT I
130 SLOW</langsyntaxhighlight>
{{out}}
<pre>1
Line 644:
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic">REM >munchausen
FOR i% = 0 TO 5
FOR j% = 0 TO 5
Line 662:
= 0
ELSE
= x% ^ x%</langsyntaxhighlight>
{{out}}
<pre> 1
Line 668:
 
=={{header|BQN}}==
<langsyntaxhighlight lang="bqn">Dgts ← •Fmt-'0'˙
IsMnch ← ⊢=+´∘(⋆˜ Dgts)
IsMnch¨⊸/ 1+↕5000</langsyntaxhighlight>
{{out}}
<pre>⟨ 1 3435 ⟩</pre>
Line 676:
=={{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].
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <math.h>
 
Line 698:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>1
Line 704:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">Func<char, int> toInt = c => c-'0';
 
foreach (var i in Enumerable.Range(1,5000)
.Where(n => n == n.ToString()
.Sum(x => Math.Pow(toInt(x), toInt(x)))))
Console.WriteLine(i);</langsyntaxhighlight>
{{out}}
<pre>1
Line 716:
=== Faster version ===
{{Trans|Kotlin}}
<langsyntaxhighlight lang="csharp">using System;
 
namespace Munchhausen
Line 758:
}
}
}</langsyntaxhighlight>
<pre>0
1
Line 766:
{{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.
<langsyntaxhighlight lang="csharp">using System;
 
static class Program
Line 791:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>0
Line 799:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <math.h>
#include <iostream>
Line 822:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 832:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="lisp">(ns async-example.core
(:require [clojure.math.numeric-tower :as math])
(:use [criterium.core])
Line 855:
 
(println (find-numbers 5000))
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 862:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">digits = iter (n: int) yields (int)
while n>0 do
yield(n//10)
Line 883:
if munchausen(i) then stream$putl(po, int$unparse(i)) end
end
end start_up</langsyntaxhighlight>
{{out}}
<pre>1
Line 889:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">
;;; check4munch maximum &optional b
;;; Return a list with all Munchausen numbers less then or equal to maximum.
Line 924:
(let ((dm (divmod n base)))
(n2base (car dm) base (cons (cadr dm) digits)))))
</syntaxhighlight>
</lang>
 
{{Out}}
Line 935:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. MUNCHAUSEN.
Line 967:
ADD-DIGIT-POWER.
COMPUTE POWER-SUM =
POWER-SUM + DIGITS(DIGIT) ** DIGITS(DIGIT)</langsyntaxhighlight>
{{out}}
<pre> 1
Line 973:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub digitPowerSum(n: uint16): (sum: uint32) is
Line 994:
end if;
n := n + 1;
end loop;</langsyntaxhighlight>
 
{{out}}
Line 1,003:
=={{header|D}}==
{{trans|C}}
<langsyntaxhighlight Dlang="d">import std.stdio;
 
void main() {
Line 1,023:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>1
Line 1,031:
Needs a modern Dc due to <code>~</code>.
Use <code>S1S2l2l1/L2L1%</code> instead of <code>~</code> to run it in older Dcs.
<langsyntaxhighlight lang="dc">[ O ~ S! d 0!=M L! d ^ + ] sM
[p] sp
[z d d lM x =p z 5001>L ] sL
lL x</langsyntaxhighlight>
Cosmetic: The stack is dirty after execution. The loop <code>L</code> needs a fix if that is a problem.
 
Line 1,041:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Munchausen do
@pow for i <- 0..9, into: %{}, do: {i, :math.pow(i,i) |> round}
Line 1,051:
Enum.each(1..5000, fn i ->
if Munchausen.number?(i), do: IO.puts i
end)</langsyntaxhighlight>
 
{{out}}
Line 1,060:
 
=={{header|F sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let toFloat x = x |> int |> fun n -> n - 48 |> float
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)</langsyntaxhighlight>
{{out}}
<pre>[1; 3435]</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: kernel math.functions math.ranges math.text.utils
prettyprint sequences ;
 
Line 1,075:
dup 1 digit-groups dup [ ^ ] 2map sum = ;
 
5000 [1,b] [ munchausen? ] filter .</langsyntaxhighlight>
{{out}}
<pre>
Line 1,082:
 
=={{header|FALSE}}==
<langsyntaxhighlight lang="false">0[1+$5000>~][
$$0\[$][
$10/$@\10*-
Line 1,092:
]#
%=[$.10,]?
]#%</langsyntaxhighlight>
 
{{out}}
Line 1,100:
 
=={{header|FOCAL}}==
<langsyntaxhighlight FOCALlang="focal">01.10 F N=1,5000;D 2
 
02.10 S M=N;S S=0
Line 1,109:
02.50 I (N-S)2.7,2.6,2.7
02.60 T %4,N,!
02.70 R</langsyntaxhighlight>
 
{{out}}
Line 1,118:
=={{header|Forth}}==
{{works with|GNU Forth|0.7.0}}
<langsyntaxhighlight lang="forth">
: dig.num \ returns input number and the number of its digits ( n -- n n1 )
dup
Line 1,180:
i check.num = if i . cr
then loop ;
</langsyntaxhighlight>
{{out}}
<pre>
Line 1,191:
{{trans|360 Assembly}}
===Fortran IV===
<langsyntaxhighlight lang="fortran">C MUNCHAUSEN NUMBERS - FORTRAN IV
DO 2 I=1,5000
IS=0
Line 1,202:
1 II=IR
2 IF(IS.EQ.I) WRITE(*,*) I
END </langsyntaxhighlight>
{{out}}
<pre>
Line 1,209:
</pre>
===Fortran 77===
<langsyntaxhighlight lang="fortran">! MUNCHAUSEN NUMBERS - FORTRAN 77
DO I=1,5000
IS=0
Line 1,222:
IF(IS.EQ.I) WRITE(*,*) I
END DO
END </langsyntaxhighlight>
{{out}}
<pre>
Line 1,231:
=={{header|FreeBASIC}}==
===Version 1===
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
' Cache n ^ n for the digits 1 to 9
' Note than 0 ^ 0 specially treated as 0 (not 1) for this purpose
Line 1,262:
Print "Press any key to quit"
 
Sleep</langsyntaxhighlight>
{{out}}
<pre>The Munchausen numbers between 0 and 500000000 are :
Line 1,270:
438579088</pre>
===Version 2===
<langsyntaxhighlight lang="freebasic">' version 12-10-2017
' compile with: fbc -s console
 
Line 1,334:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>0
Line 1,342:
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">isMunchausen = { |x|
sum = 0
for d = integerDigits[x]
Line 1,349:
}
 
println[select[1 to 5000, isMunchausen]]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,365:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import(
Line 1,400:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 1,408:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad (join)
import Data.List (unfoldr)
 
Line 1,420:
 
main :: IO ()
main = print $ filter isMunchausen [1 .. 5000]</langsyntaxhighlight>
{{out}}
<pre>[1,3435]</pre>
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:
 
<langsyntaxhighlight lang="haskell">import Data.Char (digitToInt)
 
isMunchausen :: Int -> Bool
Line 1,434:
 
main :: IO ()
main = print $ filter isMunchausen [1 .. 5000]</langsyntaxhighlight>
 
Or, without digitToInt, but importing join, swap and bool.
<langsyntaxhighlight lang="haskell">import Control.Monad (join)
import Data.Bool (bool)
import Data.List (unfoldr)
Line 1,457:
 
main :: IO ()
main = print $ filter isMunchausen [1 .. 5000]</langsyntaxhighlight>
 
{{Out}}
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:
 
<langsyntaxhighlight Jlang="j"> munch=: +/@(^~@(10&#.inv))
(#~ ] = munch"0) 1+i.5000
1 3435</langsyntaxhighlight>
 
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:
 
<langsyntaxhighlight Jlang="j"> munch=: +/@((**^~)@(10&#.inv))
(#~ ] = munch"0) 1+i.5000
1 3435</langsyntaxhighlight>
 
=={{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">
<lang Java>
public class Main {
public static void main(String[] args) {
Line 1,490:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre>1 (munchausen)
Line 1,497:
=== Faster version ===
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">public class Munchhausen {
 
static final long[] cache = new long[10];
Line 1,525:
return sum == n;
}
}</langsyntaxhighlight>
<pre>0
1
Line 1,535:
===ES6===
 
<langsyntaxhighlight lang="javascript">for (let i of [...Array(5000).keys()]
.filter(n => n == n.toString().split('')
.reduce((a, b) => a+Math.pow(parseInt(b),parseInt(b)), 0)))
console.log(i);</langsyntaxhighlight>
{{out}}
<pre>1
Line 1,546:
Or, composing reusable primitives:
 
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 1,577:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<syntaxhighlight lang JavaScript="javascript">[1, 3435]</langsyntaxhighlight>
 
=={{header|jq}}==
{{works with|jq|1.5}}
<langsyntaxhighlight lang="jq">def sigma( stream ): reduce stream as $x (0; . + $x ) ;
 
def ismunchausen:
Line 1,590:
 
# Munchausen numbers from 1 to 5000 inclusive:
range(1;5001) | select(ismunchausen)</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="jq">1
3435</langsyntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|1.0}}
<langsyntaxhighlight lang="julia">println([n for n = 1:5000 if sum(d^d for d in digits(n)) == n])</langsyntaxhighlight>
 
{{out}}
Line 1,604:
=={{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:
<langsyntaxhighlight lang="scala">// version 1.0.6
 
val powers = IntArray(10)
Line 1,628:
for (i in 0..500000000) if (isMunchausen(i))print ("$i ")
println()
}</langsyntaxhighlight>
 
{{out}}
Line 1,637:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
{def munch
{lambda {:w}
Line 1,650:
1
3435
</syntaxhighlight>
</lang>
 
=={{header|langur}}==
{{trans|C#}}
{{works with|langur|0.6.6}}
<langsyntaxhighlight lang="langur"># sum power of digits
val .spod = f(.n) fold f{+}, map(f (.x-'0') ^ (.x-'0'), s2cp toString .n)
 
# Munchausen
writeln "Answers: ", where f(.n) .n == .spod(.n), series 0..5000</langsyntaxhighlight>
 
{{works with|langur|0.8.10}}
<langsyntaxhighlight lang="langur"># sum power of digits
val .spod = f(.n) fold f{+}, map(f .x^.x, s2n toString .n)
 
# Munchausen
writeln "Answers: ", where f(.n) .n == .spod(.n), series 0..5000</langsyntaxhighlight>
 
{{out}}
Line 1,672:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">function isMunchausen (n)
local sum, nStr, digit = 0, tostring(n)
for pos = 1, #nStr do
Line 1,695:
for i = 1, 5000 do
if isMunchausen(i) then print(i) end
end</langsyntaxhighlight>
{{out}}
<pre>1
Line 1,701:
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Munchausen {
Inventory p=0:=0,1:=1
Line 1,720:
}
Munchausen
</syntaxhighlight>
</lang>
Using Array instead of Inventory
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Münchhausen {
Dim p(0 to 9)
Line 1,742:
}
Münchhausen
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,749:
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
DIMENSION P(5)
Line 1,767:
 
VECTOR VALUES FMT = $I4*$
END OF PROGRAM </langsyntaxhighlight>
 
{{out}}
Line 1,775:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">isMunchausen := proc(n::posint)
local num_digits;
num_digits := map(x -> StringTools:-Ord(x) - 48, StringTools:-Explode(convert(n, string)));
Line 1,791:
end proc;
 
Munchausen_upto(5000);</langsyntaxhighlight>
{{out}}
<pre>[1, 3435]</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Off[Power::indet];(*Supress 0^0 warnings*)
Select[Range[5000], Total[IntegerDigits[#]^IntegerDigits[#]] == # &]</langsyntaxhighlight>
{{out}}
<pre>{1,3435}</pre>
Line 1,803:
=={{header|min}}==
{{works with|min|0.19.3}}
<langsyntaxhighlight lang="min">(dup string "" split (int dup pow) (+) map-reduce ==) :munchausen?
1 :i
(i 5000 <=) ((i munchausen?) (i puts!) when i succ @i) while</langsyntaxhighlight>
{{out}}
<pre>
Line 1,813:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE MunchausenNumbers;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,ReadChar;
Line 1,858:
 
ReadChar;
END MunchausenNumbers.</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import math
 
for i in 1..<5000:
Line 1,871:
number = number div 10
if sum == i:
echo i</langsyntaxhighlight>
{{out}}
<pre>1
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.
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.
<langsyntaxhighlight lang="pascal">{$IFDEF FPC}{$MODE objFPC}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF}
uses
sysutils;
Line 1,953:
writeln('Check Count ',cnt);
end.
</syntaxhighlight>
</lang>
{{Out}}
<pre> 1 000000001
Line 1,966:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use List::Util "sum";
for my $n (1..5000) {
print "$n\n" if $n == sum( map { $_**$_ } split(//,$n) );
}</langsyntaxhighlight>
{{out}}
<pre>1
Line 1,975:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
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;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,000:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">
<?php
 
Line 2,025:
echo $i . PHP_EOL;
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,032:
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="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]).</langsyntaxhighlight>
 
{{out}}
Line 2,042:
 
Testing for a larger interval, 1..500 000 000, requires another approach:
<langsyntaxhighlight Picatlang="picat">go2 ?=>
H = [0] ++ [I**I : I in 1..9],
N = 1,
Line 2,062:
end,
nl.
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,071:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(for N 5000
(and
(=
Line 2,078:
'((N) (** N N))
(mapcar format (chop N)) ) )
(println N) ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 2,085:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">munchausen: procedure options(main);
/* precalculate powers */
declare (pows(0:5), i) fixed;
Line 2,103:
end;
end;
end munchausen;</langsyntaxhighlight>
{{out}}
<pre> 1
Line 2,109:
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
Show the Munchausen numbers up to 5000.
Line 2,136:
If the number is 0, break.
Repeat.
Put the sum into the number.</langsyntaxhighlight>
{{out}}
<pre>
Line 2,145:
=={{header|PowerBASIC}}==
{{trans|FreeBASIC}}(Translated from the FreeBasic Version 2 example.)
<langsyntaxhighlight lang="powerbasic">#COMPILE EXE
#DIM ALL
#COMPILER PBCC 6
Line 2,217:
CON.PRINT "execution time:" & STR$(t) & " ms; hit any key to end program"
CON.WAITKEY$
END FUNCTION</langsyntaxhighlight>
{{out}}
<pre> 0
Line 2,226:
 
=={{header|Pure}}==
<langsyntaxhighlight Purelang="pure">// split numer into digits
digits n::number = loop n [] with
loop n l = loop (n div 10) ((n mod 10):l) if n > 0;
Line 2,236:
(map (\d -> d^d)
(digits n)); end;
munchausen 5000;</langsyntaxhighlight>
{{out}}
<pre>[1,3435]</pre>
Line 2,242:
=={{header|PureBasic}}==
{{trans|C}}
<langsyntaxhighlight PureBasiclang="purebasic">EnableExplicit
Declare main()
 
Line 2,266:
EndIf
Next
EndProcedure</langsyntaxhighlight>
{{out}}
<pre>1
Line 2,272:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">for i in range(5000):
if i == sum(int(x) ** int(x) for x in str(i)):
print(i)</langsyntaxhighlight>
{{out}}
<pre>1
Line 2,285:
 
{{Works with|Python|3}}
<langsyntaxhighlight lang="python">'''Munchausen numbers'''
 
from functools import (reduce)
Line 2,340:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
<pre>[1, 3435]</pre>
 
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ dup 0 swap
[ dup 0 != while
10 /mod dup **
Line 2,353:
5000 times
[ i^ 1+ munchausen if
[ i^ 1+ echo sp ] ]</langsyntaxhighlight>
 
{{out}}
Line 2,361:
=={{header|Racket}}==
 
<syntaxhighlight lang="text">#lang racket
 
(define (expt:0^0=1 r p)
Line 2,386:
(check-true (munchausen-number? 3435))
(check-false (munchausen-number? 3))
(check-false (munchausen-number? -45) "no recursion on -ve numbers"))</langsyntaxhighlight>
 
{{out}}
Line 2,394:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub is_munchausen ( Int $n ) {
constant @powers = 0, |map { $_ ** $_ }, 1..9;
$n == @powers[$n.comb].sum;
}
.say if .&is_munchausen for 1..5000;</langsyntaxhighlight>
{{out}}
<pre>1
Line 2,405:
=={{header|REXX}}==
===version 1===
<langsyntaxhighlight lang="rexx">Do n=0 To 10000
If n=m(n) Then
Say n
Line 2,416:
res=res+c**c
End
Return res</langsyntaxhighlight>
{{out}}
<pre>D:\mau>rexx munch
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.
<langsyntaxhighlight 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*/
parse arg z . /*obtain optional argument from the CL.*/
Line 2,441:
parse var x _ +1 x; $= $ + @._ /*add the next power*/
end /*while*/ /* [↑] get a digit.*/
return $==ox /*it is or it ain't.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 2,450:
===version 3===
It is about &nbsp; '''3''' &nbsp; times faster than REXX version 1.
<langsyntaxhighlight 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*/
parse arg z . /*obtain optional argument from the CL.*/
Line 2,464:
parse var x _ +1 x; $= $ + @._ /*sum 6th & up digs*/
end /*while*/
return $==ox /*it is or it ain't*/</langsyntaxhighlight>
{{out|output|text=&nbsp; is the same as the 2<sup>nd</sup> REXX version.}} <br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Munchausen numbers
 
Line 2,484:
ok
next
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,492:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">class Integer
 
def munchausen?
Line 2,500:
end
 
puts (1..5000).select(&:munchausen?)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,508:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn main() {
let mut solutions = Vec::new();
 
Line 2,526:
 
println!("Munchausen numbers below 5_000 : {:?}", solutions);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,534:
=={{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">
<lang Scala>
object Munch {
def main(args: Array[String]): Unit = {
Line 2,544:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>1 (munchausen)
Line 2,550:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_munchausen(n) {
n.digits.map{|d| d**d }.sum == n
}
 
say (1..5000 -> grep(is_munchausen))</langsyntaxhighlight>
{{out}}
<pre>
Line 2,561:
 
=={{header|SuperCollider}}==
<langsyntaxhighlight lang="supercollider">(1..5000).select { |n| n == n.asDigits.sum { |x| pow(x, x) } }</langsyntaxhighlight>
 
<pre>
Line 2,568:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
func isMünchhausen(_ n: Int) -> Bool {
Line 2,578:
for i in 1...5000 where isMünchhausen(i) {
print(i)
}</langsyntaxhighlight>
{{out}}
<pre>1
Line 2,584:
 
=={{header|Symsyn}}==
<langsyntaxhighlight lang="symsyn">
x : 10 1
Line 2,615:
endif
 
</syntaxhighlight>
</lang>
{{out}}
<pre>1
Line 2,623:
{{works with|TI-83 BASIC|TI-84Plus 2.55MP}}
{{trans|Fortran}}
<langsyntaxhighlight lang="ti83b"> For(I,1,5000)
0→S:I→K
For(J,1,4)
Line 2,633:
End
If S=I:Disp I
End </langsyntaxhighlight>
{{out}}
<pre>
Line 2,644:
{{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.
<langsyntaxhighlight lang="ti83b">{1,1,4,27,256,3125}→L₁
For(A,0,5,1)
For(B,0,5,1)
Line 2,662:
End
End
End</langsyntaxhighlight>
{{out}}
<pre>
Line 2,672:
=={{header|VBA}}==
 
<syntaxhighlight lang="vb">
<lang vb>
Option Explicit
 
Line 2,692:
IsMunchausen = (Tot = Number)
End Function
</syntaxhighlight>
</lang>
{{out}}
<pre>1 is a munchausen number.
Line 2,698:
 
=={{header|VBScript}}==
<langsyntaxhighlight lang="vbscript">
for i = 1 to 5000
if Munch(i) Then
Line 2,724:
 
End Function
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,733:
=={{header|Visual Basic}}==
{{trans|FreeBASIC}}(Translated from the FreeBasic Version 2 example.)
<langsyntaxhighlight lang="vb">Option Explicit
 
Declare Function GetTickCount Lib "kernel32.dll" () As Long
Line 2,839:
res = res & "execution time:" & Str$(t) & " ms"
MsgBox res
End Sub</langsyntaxhighlight>
{{out}}
<pre> 0
Line 2,850:
{{trans|FreeBASIC}}(Translated from the FreeBasic Version 2 example.)<br/>
Computation time is under 4 seconds on tio.run.
<langsyntaxhighlight lang="vbnet">Imports System
 
Module Program
Line 2,874:
Next
End Sub
End Module</langsyntaxhighlight>
{{out}}
<pre>0
Line 2,882:
 
=={{header|Wren}}==
<langsyntaxhighlight lang="ecmascript">var powers = List.filled(10, 0)
for (i in 1..9) powers[i] = i.pow(i).round // cache powers
 
Line 2,901:
for (i in 1..5000) {
if (munchausen.call(i)) System.print(i)
}</langsyntaxhighlight>
 
{{out}}
Line 2,912:
=={{header|XPL0}}==
The digits 6, 7, 8 and 9 can't occur because 6^6 = 46656, which is beyond 5000.
<langsyntaxhighlight XPL0lang="xpl0">int Pow, A, B, C, D, N;
[Pow:= [0, 1, 4, 27, 256, 3125];
for A:= 0 to 5 do
Line 2,923:
[IntOut(0, N); CrLf(0)];
];
]</langsyntaxhighlight>
 
{{out}}
Line 2,932:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">[1..5000].filter(fcn(n){ n==n.split().reduce(fcn(s,n){ s + n.pow(n) },0) })
.println();</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.