Narcissistic decimal number: Difference between revisions
→{{header|AppleScript}}: Idiomatic version: speed optimisation.
m (→Functional) |
(→{{header|AppleScript}}: Idiomatic version: speed optimisation.) |
||
Line 431:
===Idiomatic===
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.▼
▲This and
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.▼
▲The JavaScript/Haskell translation above
<lang applescript>(*
Line 443 ⟶ 444:
script o
property output : {}
-- "DSN" is a convenience term for a number used to store
property previousDSNs : missing value
property newDSNs : missing value
-- Since previousDSNs and newDSNs will be potentially *very* long lists,
-- their contents are handled here as sublists of 1000 each.
property previousSublist : missing value
property newSublist : missing value
end script
Line 456 ⟶ 461:
set end of o's output to i
end repeat
set m to 1 -- Digits/number.▼
set o's newDSNs to rest of o's output -- Initial DSNs. (1-9.)▼
-- Main loop, per increase in digits/number:
-- Tests digit collections
▲ set m to 1 -- Digits/number.
▲ set o's newDSNs to {rest of o's output} -- Initial DSNs. (1-9.)
repeat until (m = maxM) -- or until returning from the handler with q results.
set m to m + 1
set o's previousDSNs to o's newDSNs
set o's
repeat with g from 1 to (count o's previousDSNs)
set
repeat with i from 1 to (count o's previousSublist)
set
set
set subtotal to
-- Derive and test new digit groups having an additional digit each, only adding values
--
set
set sumResult to subtotal
-- Compare the result's digits with those in
-- eliminating matches from the list
set temp to
repeat with groupDigit in thisGroup
if (groupDigit's contents = thisDigit) then
set groupDigit's contents to missing value
exit repeat
end if▼
set temp to temp div 10
end repeat
-- If
▲ set temp to temp div 10
set end of o's output to sumResult div 1
-- If it's the qth find, return all q results.
if ((count o's output) = q) then return o's output
end if
end repeat
▲ -- If all the list's number values have been matched and replaced, the sum-of-powers result is narcissistic.
▲ if ((count thisCollection's numbers) = 0) then
▲ set end of o's output to sumResult div 1
▲ -- If it's the qth find, return the lot.
▲ if ((count o's output) = q) then return o's output
▲ end if
end repeat
end repeat
Line 514 ⟶ 527:
-- Demo:
return narcissisticDecimalNumbers(25)</lang>
{{output}}
|