Narcissistic decimal number: Difference between revisions
m
→{{header|AppleScript}}: Idiomatic version rewritten with more efficient digit group generation.
m (→{{header|AppleScript}}: Idiomatic version: added comment.) |
m (→{{header|AppleScript}}: Idiomatic version rewritten with more efficient digit group generation.) |
||
Line 432:
===Idiomatic===
This script and earlier versions were written from scratch in AppleScript,
<lang applescript>(*
Line 440:
(or as many of the q as can be represented by AppleScript number values).
*)
on narcissisticDecimalNumbers(q)
script o
property output : {}
property
-- Recursive subhandler. Builds lists containing m digit values while summing the digits' mth powers.
on recurse(digitList, sumOfPowers, digitShortfall)
-- If m digits have been obtained, compare the sum of powers's digits with the values in the list.
-- Otherwise continue branching the recursion to derive longer lists.
if (digitShortfall is 0) then
set temp to sumOfPowers
repeat until (temp = 0)
set sumDigit to temp mod 10
if (sumDigit is in digitList) then
repeat with
if (
set
set unmatched to unmatched - 1
exit repeat
end if
end repeat
end if
set temp to temp div 10
end repeat
-- If all the digits have been matched, the sum of powers is narcissistic.
if (unmatched is 0) then
set end of my output to sumOfPowers div 1
-- If it's the qth find, signal the end of the process.
if ((count my output) = q) then set done to true
end if
else
-- If fewer than m digits at this level, derive longer lists from the current one.
-- Adding only values that are less than or equal to the last one makes each
-- collection unique and turns up the narcissistic numbers in numerical order.
repeat with additionalDigit from 0 to end of digitList
recurse(digitList & additionalDigit, sumOfPowers + additionalDigit ^ m, digitShortfall - 1)
if (done) then exit repeat
end repeat
end if
end recurse
end script
(* Outer handler code. *)
if (q > 89) then set q to 89 -- Number of narcissistic decimal integers known to exist.
set maxM to 16 -- Maximum number of decimal digits (other than trailing zeros) in AppleScript numbers.
tell o
-- Begin with zero, which is narcissistic by definition and is never the only digit used in other numbers.
if (q > 0) then set end of its output to 0
if (q < 2) then set its done to true
-- Initiate the recursive building and testing of collections of increasing numbers of digit values.
repeat until (its done)
set its m to (its m) + 1
if (its m > maxM) then
set end of its output to "Remaining numbers beyond AppleScript's number precision"
set its done to true
else
repeat with digit from 1 to 9
recurse({digit}, digit ^ (its m), (its m) - 1)
if (its done) then exit repeat
end repeat
end if
end repeat
return its output
end tell
end narcissisticDecimalNumbers
return narcissisticDecimalNumbers(25)</lang>
|