Narcissistic decimal number: Difference between revisions
m
→{{header|AppleScript}}: "Natively written" solution modified to store digit collections as numbers instead of lists, incidentally finding results in order without needing a sort. Also reprefaced, and reheadered.
(→{{header|AppleScript}}: Minor modification to natively written version to match change to recommended sort.) |
m (→{{header|AppleScript}}: "Natively written" solution modified to store digit collections as numbers instead of lists, incidentally finding results in order without needing a sort. Also reprefaced, and reheadered.) |
||
Line 427:
<lang AppleScript>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315}</lang>
----
===
This and an earlier version it replaces were written from scratch in AppleScript and return the required 25 numbers in around a quarter of a second. (The extreme slowness of the JavaScript/Haskell translation above is ''not'' due to AppleScript being "a little out of its depth here"!) Beyond the requirements of the task, this current version returns the first 41 numbers in just under 20 seconds, but then takes four-and-a-quarter minutes over the first 42 and thirty-eight-and-a-half minutes over the first 44. (All timings on a 3.4 GHz iMac.) The 43rd and 44th numbers are both displayed as 4.33828176939137E+15 in Script Editor's result pane, but appear to have the correct values when tested. The narcissistic decimal numbers beyond these are admittedly beyond the resolution of AppleScript's number classes.
The JavaScript/Haskell translation above is coded not to return so many narcissistic numbers but to return those with up to so many digits. As such, it doesn't strictly conform to the current task description and only returns the required result because the 25th narcissistic decimal number also happens to be the last of the four which have 7 digits. A user of the code would have to know this in advance to be able to supply the relevant parameter. However, as I write, that code's demo call for 7 digits is commented out.
<lang applescript>(*
(or as many of the q as can be represented by AppleScript number values).
*)
▲-- Return the first q narcissistic decimal numbers (or as many as can be represented by AppleScript reals).
on narcissisticDecimalNumbers(q)
script o
property output : {}
-- "DSN" is a convenience term for a number used to store the digits it contains.
property
property newDSNs : missing value
end script
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
-- Begin with single-digit values, which are
repeat with i from 0 to 9
if (i ≥ q) then return o's output
set end of o's output to i
end repeat
set o's newDSNs to rest of o's output -- Initial DSNs. (1-9.)
-- Main loop, per increase in digits/number:
--
repeat until (m = maxM) -- or until returning from the handler with q results.
set o's newDSNs to {}
repeat with thisDSN in o's previousDSNs
--
▲ -- Get each previous list and calculate the sum of its integers raised to their mth powers.
set
set
set subtotal to subtotal + thisDigit ^ m
end repeat
-- Derive and test new
-- up to and including the old DSN's LSD to ensure every collection at this level is unique.
repeat with additionalDigit from
set
--
set
-- Compare the result's
repeat until (temp = 0)
set thisDigit to temp mod 10
if (thisDigit is not in
repeat with
exit
end
set temp to temp
▲ set temp to temp div 10
▲ else
end repeat
-- If all the list's
if (
-- If
▲ set counter to counter + 1
end if
end repeat
end repeat
▲ set m to m + 1
end repeat
--
▲ set end of o's output to "Remaining numbers beyond AppleScript's real precision"
▲ set q to counter + 1
end narcissisticDecimalNumbers
--
{{output}}
|