Narcissistic decimal number: Difference between revisions

→‎{{header|AppleScript}}: Idiomatic version: speed optimisation.
(→‎{{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 an earlier version it replacesversions 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 certainly ''not'' due to AppleScript being "a little out of its depth here"!) BeyondThis version actually ''does'' contain a "hand optimisation" which, for speed, limits the requirementslengths of the task,temporary thislists currentused. versionIt now returns the first 41 numbers in justaround under 20seven seconds, but then takes four-and-a-quarter minutes over the first 42 andin thirty-eight-and-aforty-halfsix minutesseconds, overand the first 44 in two minutes fifty. (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 as 4.33828176939137E+15, 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.
 
The JavaScript/Haskell translation above isdoesn't codedreturn notthe to returnfirst so many narcissistic decimal numbers, but to return those with up to so many digits. As such, it doesn't strictly conform to the current task description. andIt would only returnsreturn the required result25 numbers because the 25th narcissistic decimal number also happens to be the last of the four which havewith 7 digits. A user of the code would haveneed to know this in advance to be able to supply the relevant parameter. However, as Icurrently writepresented, thatthe code's demostill callonly forreturns 720 digits is commented outnumbers.
 
<lang applescript>(*
Line 443 ⟶ 444:
script o
property output : {}
-- "DSN" is a convenience term for a number used to store theits digits it contains.
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 of digits rather than successive integer values, but happens to turn up the numbers in order.
set m to 1 -- Digits/number.
-- but happens to turn up the narcissistic numbers in numerical order.
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 newDSNsnewSublist to {}
repeatset witho's thisDSNnewDSNs into {o's previousDSNsnewSublist}
repeat with g from 1 to (count o's previousDSNs)
-- Base number for further DSNs to be derived from this one.
set baseDSNo's previousSublist to thisDSNitem g of *o's 10previousDSNs
repeat with i from 1 to (count o's previousSublist)
-- Extract this one's digits, subtotalling the sum of their current-mth powers.
set baseDigitsthisDSN to {}item i of o's previousSublist
set subtotal -- Base number for further DSNs to 0be derived from this one.
repeat until ( set baseDSN to thisDSN =* 0)10
set-- thisDigitExtract tothis thisDSNone's moddigits, 10subtotalling the sum of their current-mth powers.
set end of baseDigits to thisDigit{}
set subtotal to subtotal + thisDigit ^ m0
setrepeat thisDSN tountil (thisDSN div= 100)
end repeat set thisDigit to thisDSN mod 10
-- Derive and test new digit collections having anset additionalend digitof each,baseDigits onlyto adding valuesthisDigit
-- up to and including the old DSN's LSDset subtotal to ensure every collection at thissubtotal level+ isthisDigit unique.^ m
repeat with additionalDigit from 0 set thisDSN to (beginningthisDSN ofdiv baseDigits)10
set end of o's newDSNs to baseDSN + additionalDigitrepeat
-- Derive and test new digit groups having an additional digit each, only adding values
set thisCollection to baseDigits & additionalDigit
-- Completeup to and including the sum-of-powersold calculationDSN's forLSD to ensure every group at this collectionlevel is unique.
setrepeat sumResultwith toadditionalDigit subtotalfrom +0 additionalDigitto (beginning ^of mbaseDigits)
-- Compare the result's digitsset withend of o's thosenewSublist into thebaseDSN collection+ list,additionalDigit
-- eliminating matches from theif list((count one-for-one.o's newSublist) is 1000) then
set tempo's newSublist to sumResult{}
repeat until (temp = 0) set end of o's newDSNs to o's newSublist
setend thisDigit to temp mod 10if
ifset (thisDigitthisGroup isto notbaseDigits in& thisCollection) then exit repeatadditionalDigit
repeat-- withComplete collectionDigitthe insum-of-powers thisCollectioncalculation for this group.
set sumResult to subtotal if+ (collectionDigit'sadditionalDigit contents^ = thisDigit) thenm
-- Compare the result's digits with those in set collectionDigit's contents tothe missinggroup valuelist,
-- eliminating matches from the list exit repeatone-for-one.
set temp to end ifsumResult
setrepeat temp tountil (temp div= 100)
set end of o's outputset thisDigit to sumResulttemp divmod 110
if ((countthisDigit o'sis output)not =in qthisGroup) then return o'sexit outputrepeat
repeat with groupDigit in thisGroup
if (groupDigit's contents = thisDigit) then
set groupDigit's contents to missing value
exit repeat
end if
-- If it's the qthend find, return the lot.repeat
set temp to temp div 10
end repeat
-- If allthis thedigit listgroup's number valuesmembers have all been matched and replaced, the sum-of-powers result is narcissistic.
set temp to temp div 10
if ((count thisCollectionthisGroup's numbers) = 0) then
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}}
557

edits