Lychrel numbers: Difference between revisions

m
→‎{{header|AppleScript}}: Speeded up with additional subgrouping of digit list storage. Number range constrained to start at 1.
(Added AppleScript.)
m (→‎{{header|AppleScript}}: Speeded up with additional subgrouping of digit list storage. Number range constrained to start at 1.)
Line 307:
 
=={{header|AppleScript}}==
<lang applescript>on Lychrels(rangenumberLimit, iterationLimit)
script o
property digits : missing value -- Digits of the current startNumber or a derived sum.
property stigid : missing value -- Reverse thereof.
property digitLists : missing value -- Copies of the digit lists in the current sequence.
-- The digit lists from theearlier, Lychrel sequences for earlier Lychrels, grouped byinto lastten digitgroups toof speedten searches.subgroups
-- based on their last two digits (or one if that's all) to speed up searches for them.
property earlierDigitLists : {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}}
property checkListearlierDigitLists : missing value -- Picked-out sublist from earlierDigitLists.{}
property subgroup : missing value -- Subgroup list picked from earlierDigitLists.
-- Lychrel output.
property seeds : {}
Line 322 ⟶ 323:
-- Subhandler: Has the current list of digits come up in the sequence for an earlier Lychrel?
on isRelated()
-- Look for it in sublist (its last digit + 1) of list (its penultimate digit + 1) of earlierDigitLists.
set i ifto (item iend of my checkList = digits) then return+ true1
set j to 1
if ((count my digits) > 1) then set j to j + (item -2 of my digits)
set subgroup to item i of item j of my earlierDigitLists
-- It's faster to test this using a repeat than with 'is in'!
setrepeat checkListwith i from 1 to item ((end ofcount my digitssubgroup) + 1) of my earlierDigitLists
repeat with i from 1 toif (countitem i of my checkListsubgroup = digits) then return true
if (item i of my checkList = digits) then return true
end repeat
return false
end isRelated
end script
repeat 10 times
propertyset end of o's earlierDigitLists :to {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}}
end repeat
repeat with startNumber from (beginning of range)1 to (end of range)numberLimit
-- Get the number's digits and their reverse.
set o's digits to {}
Line 388 ⟶ 396:
end if
if (startNumberIsPalindrome) then set end of o's palindromics to startNumber
-- AppendStore thethis sequence's digit lists to the appropriate same-last-digit groups in earlierDigitLists.
repeat with thisList in o's digitLists
set-- endEach ofgoes itemin sublist ((endits oflast thisList)digit + 1) of o'slist earlierDigitLists(its topenultimate thisList'sdigit contents+ 1) of earlierDigitLists.
set i to (end of thisList) + 1
set j to 1
if (digitCount > 1) then set j to j + (item -2 of thisList)
set end of item i of item j of o's earlierDigitLists to thisList's contents
end repeat
end if
Line 408 ⟶ 420:
 
on task()
set numberRangenumberLimit to {1, 10000}
set iterationLimit to 500
set {seeds:seeds, relateds:relateds, palindromics:palindromics} to Lychrels(numberRangenumberLimit, iterationLimit)
set output to {"Lychrel numbers between " & beginning of numberRange & ("1 and " & end of numberRangenumberLimit & ":"), ""}
set end of output to ((count seeds) as text) & " seed Lychrels: " & join(seeds, ", ")
set end of output to ((count relateds) as text) & " related Lychrels"
557

edits