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: Line 432:
===Idiomatic===
===Idiomatic===


This script and earlier versions were written from scratch in AppleScript, this one returning the required 25 numbers in just under a sixth 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"!) The handler below returns the first 41 numbers in around four-and-a half seconds, the first 42 in twenty-nine, and the first 44 in one minute forty-seven. (All timings on a 3.4 GHz iMac.) The 43rd and 44th numbers are both displayed 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.
When corrected actually to return the 25 numbers required by the task, the JavaScript/Haskell translation above takes seven minutes fifty-three seconds on my current machine. By contrast, the code here was written from scratch in AppleScript, takes the number of results required as its parameter rather than the numbers of digits in them, and returns the 25 numbers in just under a sixth of a second. The first 41 numbers take just under four-and-a-half seconds, the first 42 twenty-seven, and the first 44 a minute thirty-seven-and-a-half. The 43rd and 44th numbers are both displayed in Script Editor's result pane as 4.33828176939137E+15, but appear to be 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 the 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>(*
<lang applescript>(*
Line 443: Line 441:
script o
script o
property output : {}
property output : {}
property listOfDigits : missing value
property m : 0 -- Digits per collection/number.
property m : 0 -- Digits per collection/number.
property done : false
property done : false
Line 451: Line 450:
-- Otherwise continue branching the recursion to derive longer lists.
-- Otherwise continue branching the recursion to derive longer lists.
if (digitShortfall is 0) then
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 temp to sumOfPowers
set unmatched to m
set unmatched to m
Line 457: Line 458:
if (sumDigit is in digitList) then
if (sumDigit is in digitList) then
repeat with d from 1 to unmatched
repeat with d from 1 to unmatched
if (number d of digitList = sumDigit) then
if (sumDigit = number d of my listOfDigits) then
set number d of digitList to missing value
set number d of my listOfDigits to missing value
set unmatched to unmatched - 1
set unmatched to unmatched - 1
exit repeat
exit repeat
Line 486: Line 487:
end script
end script
(* Outer handler code. *)
(* Rest of main handler code. *)
if (q > 89) then set q to 89 -- Number of narcissistic decimal integers known to exist.
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.
set maxM to 16 -- Maximum number of decimal digits (other than trailing zeros) in AppleScript numbers.
tell o
tell o
-- Begin with zero, which is narcissistic by definition and is never the only digit used in other numbers.
-- Begin with zero, which is narcissistic by definition and is never the only digit used in other numbers.