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(
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
-- based on their last two digits (or one if that's all) to speed up searches for them.
property earlierDigitLists : {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}}▼
property
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 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'!
▲ if (item i of my checkList = digits) then return true
end repeat
return false
end isRelated
end script
repeat 10 times
end repeat
repeat with startNumber from
-- 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
--
repeat with thisList in o's digitLists
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
set iterationLimit to 500
set {seeds:seeds, relateds:relateds, palindromics:palindromics} to Lychrels(
set output to {"Lychrel numbers between
set end of output to ((count seeds) as text) & " seed Lychrels: " & join(seeds, ", ")
set end of output to ((count relateds) as text) & " related Lychrels"
|