Munchausen numbers: Difference between revisions
Content added Content deleted
Line 252: | Line 252: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
<lang AppleScript>------------------- |
<lang AppleScript>-- MUNCHAUSEN NUMBER ? ------------------------------------------------------- |
||
-- isMunchausen :: Int -> Bool |
-- isMunchausen :: Int -> Bool |
||
Line 267: | Line 267: | ||
(class of n is integer) and ¬ |
(class of n is integer) and ¬ |
||
foldl(digitPowerSum, 0, characters of (n as string)) = n |
foldl(digitPowerSum, 0, characters of (n as string)) = n |
||
end isMunchausen |
end isMunchausen |
||
--------------------------- |
-- TEST ---------------------------------------------------------------------- |
||
on run |
on run |
||
Line 281: | Line 281: | ||
-------------------- |
-- GENERIC FUNCTIONS --------------------------------------------------------- |
||
-- enumFromTo :: Int -> Int -> [Int] |
-- enumFromTo :: Int -> Int -> [Int] |
||
on enumFromTo(m, n) |
on enumFromTo(m, n) |
||
if m |
if m > n then |
||
set |
set d to -1 |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
else |
else |
||
set d to 1 |
|||
end if |
end if |
||
set lst to {} |
|||
⚫ | |||
⚫ | |||
end repeat |
|||
⚫ | |||
end enumFromTo |
end enumFromTo |
||
-- filter :: (a -> Bool) -> [a] -> [a] |
-- filter :: (a -> Bool) -> [a] -> [a] |
||
on filter( |
on filter(f, xs) |
||
tell mReturn( |
tell mReturn(f) |
||
set lst to {} |
set lst to {} |
||
set lng to length of xs |
set lng to length of xs |
||
Line 332: | Line 333: | ||
end if |
end if |
||
end mReturn</lang> |
end mReturn</lang> |
||
{{Out}} |
|||
<lang AppleScript>{1, 3435}</lang> |
|||
---- |
|||
More straightforwardly: |
|||
<lang applescript>set MunchhausenNumbers to {} |
|||
repeat with i from 1 to 5000 |
|||
if (i > 0) then |
|||
set n to i |
|||
set s to 0 |
|||
repeat until (n is 0) |
|||
tell n mod 10 to set s to s + it ^ it |
|||
set n to n div 10 |
|||
⚫ | |||
if (s = i) then set end of MunchhausenNumbers to i |
|||
end if |
|||
end repeat |
|||
return MunchhausenNumbers</lang> |
|||
{{Out}} |
{{Out}} |
||
<lang applescript>{1, 3435}</lang> |
<lang applescript>{1, 3435}</lang> |