Narcissistic decimal number: Difference between revisions

→‎{{header|AppleScript}}: Natively written version: two further optimisations, giving a 16%-18% speed improvement. Burb updated with new timings.
(→‎AppleScript :: Functional: Misleading. ( 'native' means without import))
(→‎{{header|AppleScript}}: Natively written version: two further optimisations, giving a 16%-18% speed improvement. Burb updated with new timings.)
Line 426:
<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>
 
===NativeNatively written===
AppleScript can certainly struggle when the code's an impenetrable and inefficient pidgin trying to be two other languages. However, when tested in 2020 on an apparently faster machine, the script above only takes 7.9 minutes to return 25 numbers. On the same machine, the natively written offering below returns the same result in just under a0.6 secondseconds. It uses the same excellent optimisation approach hinted at above. The effect of this is to block timings according to the number of digits in the integers. So, since the 34th to 41st narcissistic integers all have the same number of digits, it takes roughly the same time (3829.75 seconds) to return the first 34 or the first 41 or anything in between, but nearly elevennine minutes to return the first 42! The highest narcissistic integer AppleScript can represent as an integer is the 31st in the table on the Discussion page. The highest that can be accurately represented as a real is the 43rd. As per the task description, this script returns a specified number of integers rather than those having up to a specified number of digits.
 
<lang applescript>use sorter : script "Insertion Sort" -- <https://www.rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#AppleScript>
Line 461:
-- Each group in the old list is the basis for new ones containing an additional digit each.
set oldGroup to item i of o's previousDigitGroups
repeat-- withPrecalculate jthe fromsums endof the raised powers of oldGroupthe toexisting 9digits.
set copy oldGroupnBase to newGroup0
repeat with thisDigit in set end of newGroup to joldGroup
set nnBase to nnBase + thisDigit ^ m
end repeat
-- Create and test the new groups.
repeat with addedDigit from end of oldGroup to 9
set nnewGroup to 0oldGroup & addedDigit
-- Store each new group and calculate its potential narcissistic integer value.
set end of o's newDigitGroups to newGroup
--set Aftern storingto eachnBase new+ group,addedDigit calculate^ its potential narcissistic integer value.m
set n to 0
repeat with thisDigit in newGroup
set n to n + thisDigit ^ m
end repeat
-- Extract the integer's digits and sort them numerically.
set checkGroup to {}
557

edits