Lychrel numbers: Difference between revisions
Content added Content deleted
(Added AppleScript.) |
m (→{{header|AppleScript}}: Speeded up with additional subgrouping of digit list storage. Number range constrained to start at 1.) |
||
Line 307: | Line 307: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
<lang applescript>on Lychrels( |
<lang applescript>on Lychrels(numberLimit, iterationLimit) |
||
script o |
script o |
||
property digits : missing value -- Digits of the current startNumber or a derived sum. |
property digits : missing value -- Digits of the current startNumber or a derived sum. |
||
property stigid : missing value -- Reverse thereof. |
property stigid : missing value -- Reverse thereof. |
||
property digitLists : missing value -- Copies of the digit lists in the current sequence. |
property digitLists : missing value -- Copies of the digit lists in the current sequence. |
||
-- The digit lists from |
-- The digit lists from earlier, Lychrel sequences, grouped into ten groups of ten subgroups |
||
-- based on their last two digits (or one if that's all) to speed up searches for them. |
|||
⚫ | |||
property |
property earlierDigitLists : {} |
||
property subgroup : missing value -- Subgroup list picked from earlierDigitLists. |
|||
-- Lychrel output. |
-- Lychrel output. |
||
property seeds : {} |
property seeds : {} |
||
Line 322: | Line 323: | ||
-- Subhandler: Has the current list of digits come up in the sequence for an earlier Lychrel? |
-- Subhandler: Has the current list of digits come up in the sequence for an earlier Lychrel? |
||
on isRelated() |
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'! |
-- It's faster to test this using a repeat than with 'is in'! |
||
repeat with i from 1 to (count my subgroup) |
|||
if (item i of my subgroup = digits) then return true |
|||
⚫ | |||
end repeat |
end repeat |
||
return false |
return false |
||
end isRelated |
end isRelated |
||
end script |
end script |
||
repeat 10 times |
|||
⚫ | |||
end repeat |
|||
repeat with startNumber from |
repeat with startNumber from 1 to numberLimit |
||
-- Get the number's digits and their reverse. |
-- Get the number's digits and their reverse. |
||
set o's digits to {} |
set o's digits to {} |
||
Line 388: | Line 396: | ||
end if |
end if |
||
if (startNumberIsPalindrome) then set end of o's palindromics to startNumber |
if (startNumberIsPalindrome) then set end of o's palindromics to startNumber |
||
-- |
-- Store this sequence's digit lists. |
||
repeat with thisList in o's digitLists |
repeat with thisList in o's digitLists |
||
-- Each goes in sublist (its last digit + 1) of list (its penultimate digit + 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 repeat |
||
end if |
end if |
||
Line 408: | Line 420: | ||
on task() |
on task() |
||
set |
set numberLimit to 10000 |
||
set iterationLimit to 500 |
set iterationLimit to 500 |
||
set {seeds:seeds, relateds:relateds, palindromics:palindromics} to Lychrels( |
set {seeds:seeds, relateds:relateds, palindromics:palindromics} to Lychrels(numberLimit, iterationLimit) |
||
set output to {"Lychrel numbers between |
set output to {"Lychrel numbers between 1 and " & numberLimit & ":", ""} |
||
set end of output to ((count seeds) as text) & " seed Lychrels: " & join(seeds, ", ") |
set end of output to ((count seeds) as text) & " seed Lychrels: " & join(seeds, ", ") |
||
set end of output to ((count relateds) as text) & " related Lychrels" |
set end of output to ((count relateds) as text) & " related Lychrels" |