Narcissistic decimal number: Difference between revisions

Content added Content deleted
m (→‎{{header|AppleScript}}: Idiomatic version rewritten with more efficient digit group generation.)
m (→‎{{header|AppleScript}}: Idiomatic: minor optimisation, rewritten preamble.)
Line 432:
===Idiomatic===
 
ThisWhen scriptcorrected andactually earlierto versionsreturn werethe written25 fromnumbers scratchrequired inby AppleScriptthe task, thisthe oneJavaScript/Haskell returningtranslation theabove requiredtakes 25seven numbersminutes infifty-three justseconds underon amy sixthcurrent ofmachine. aBy second.contrast, (Thethe extremecode slownesshere ofwas thewritten JavaScript/Haskellfrom translationscratch abovein isAppleScript, certainlytakes notthe duenumber toof AppleScriptresults beingrequired "aas littleits outparameter ofrather itsthan depththe here"!)numbers Theof handlerdigits belowin them, and returns the 25 numbers in just under a sixth of a second. The first 41 numbers intake aroundjust under four-and-a -half seconds, the first 42 in twenty-nineseven, and the first 44 in onea minute fortythirty-seven. (All timings on -and-a 3.4 GHz iMac-half.) The 43rd and 44th numbers are both displayed in Script Editor's result pane as 4.33828176939137E+15, but appear to havebe the correct values when tested. The JavaScript/Haskell translation's problems are certainly ''not'' due to AppleScript being "a little out of its depth here", but the narcissistic decimal numbers beyond thesethe 44th are admittedly beyond the resolution of AppleScript's number classes.
 
At the time of writing, the JavaScript/Haskell translation above still only returns 20 numbers. To get it to return the required 25, a user of the code would have to know in advance that the 25th narcissistic decimal number also happens to be the last of four which have 7 digits.
 
<lang applescript>(*
Line 443 ⟶ 441:
script o
property output : {}
property listOfDigits : missing value
property m : 0 -- Digits per collection/number.
property done : false
Line 451 ⟶ 450:
-- Otherwise continue branching the recursion to derive longer lists.
if (digitShortfall is 0) then
-- Assign the list to a script property to allow faster references to its items (ie. incl. reference to script).
set listOfDigits to digitList
set temp to sumOfPowers
set unmatched to m
Line 457 ⟶ 458:
if (sumDigit is in digitList) then
repeat with d from 1 to unmatched
if (sumDigit = number d of digitListmy = sumDigitlistOfDigits) then
set number d of digitListmy listOfDigits to missing value
set unmatched to unmatched - 1
exit repeat
Line 486 ⟶ 487:
end script
(* OuterRest of main 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.