Lychrel numbers: Difference between revisions

m
→‎{{header|AppleScript}}: Further minor optimisation.
m (→‎{{header|AppleScript}}: Speeded up with additional subgrouping of digit list storage. Number range constrained to start at 1.)
m (→‎{{header|AppleScript}}: Further minor optimisation.)
Line 312:
property stigid : missing value -- Reverse thereof.
property digitLists : missing value -- Copies of the digit lists in the current sequence.
-- The digit lists from earlier, Lychrel sequences, grouped into ten groups of ten1000 subgroupssublists
-- basedindexed onby their last twothree digits (or+ one if that's all1) to speed up searches for them.
property earlierDigitLists : {}listOfLists(1000)
property subgroup : missing value -- Subgroup listSublist picked from earlierDigitLists.
-- Lychrel output.
property seeds : {}
Line 323:
-- Subhandler: Has the current list of digits come up in the sequence for an earlier Lychrel?
on isRelated()
-- Look forUnless it inonly sublisthas (its lastone digit, +look 1)for ofit listin (its penultimate digitthe +appropriate 1)subgroup of earlierDigitLists.
set idigitCount to (end ofcount my digits) + 1
setif j(digitCount to> 1) then
if ((count my digits) > 1) then set ji to j + (item -2 of my digits) * 10 + (end of my digits) + 1
if (digitCount > 2) then set subgroupi to item i of+ (item j-3 of my earlierDigitListsdigits) * 100
-- It's faster to testset thissubgroup using ato repeatitem thani withof 'ismy in'!earlierDigitLists
repeat with i from 1-- It's faster to (counttest this using a repeat than with my'is subgroup)in'!
ifrepeat (itemwith i offrom 1 to (count my subgroup = digits) then return true
if (item i of my subgroup = digits) then return true
end repeat
end if
return false
end isRelated
end script
repeat 10 times
set end of o's earlierDigitLists to {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}}
end repeat
repeat with startNumber from 1 to numberLimit
Line 396 ⟶ 395:
end if
if (startNumberIsPalindrome) then set end of o's palindromics to startNumber
-- Store this sequence's digit lists based on their last three digits. There won't be any
-- single-digit lists (they're palindromes), but there may be some with only two digits.
repeat with thisList in o's digitLists
--set Eachi goes in sublistto (itsitem last-2 digitof + 1thisList) of* list10 + (itsend penultimateof digitthisList) + 1) of earlierDigitLists.
set idigitCount to (end ofcount thisList) + 1
if (digitCount > 2) then set ji to 1i + (item -3 of thisList) * 100
ifset (digitCountend >of 1)item theni setof jo's earlierDigitLists to jthisList's + (item -2 of thisList)contents
set end of item i of item j of o's earlierDigitLists to thisList's contents
end repeat
end if
Line 410 ⟶ 409:
return {seeds:o's seeds, relateds:o's relateds, palindromics:o's palindromics}
end Lychrels
 
on listOfLists(len)
script o
property lst : {}
end script
repeat 10len times
set end of o's lst to {}
end repeat
return o's lst
end listOfLists
 
on join(lst, delim)
557

edits