Jump to content

Lychrel numbers: Difference between revisions

Added AppleScript.
(Added AppleScript.)
Line 305:
There are 3 palindromic Lychrel numbers up to 10000: 4994 8778 9999
</pre>
 
=={{header|AppleScript}}==
<lang applescript>on Lychrels(range, 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 the sequences for earlier Lychrels, grouped by last digit to speed searches.
property earlierDigitLists : {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}}
property checkList : missing value -- Picked-out sublist from earlierDigitLists.
-- Lychrel output.
property seeds : {}
property relateds : {}
property palindromics : {}
-- Subhandler: Has the current list of digits come up in the sequence for an earlier Lychrel?
on isRelated()
-- It's faster to test this using a repeat than with 'is in'!
set checkList to item ((end of my digits) + 1) of my earlierDigitLists
repeat with i from 1 to (count my checkList)
if (item i of my checkList = digits) then return true
end repeat
return false
end isRelated
end script
repeat with startNumber from (beginning of range) to (end of range)
-- Get the number's digits and their reverse.
set o's digits to {}
set temp to startNumber
repeat until (temp = 0)
set beginning of o's digits to temp mod 10
set temp to temp div 10
end repeat
set o's stigid to reverse of o's digits
-- Note if the number itself is palindromic.
set startNumberIsPalindrome to (o's digits = o's stigid)
if (o's isRelated()) then
-- It(s digits) occurred in the sequence for an earlier Lychrel, so it's a related Lychrel.
set end of o's relateds to startNumber
else
-- Otherwise run its sequence.
set o's digitLists to {}
set digitCount to (count o's digits)
-- Sequence loop.
repeat iterationLimit times
-- Add the reversed digits to those of the current number.
set carry to 0
repeat with i from digitCount to 1 by -1
set d to (item i of o's digits) + (item i of o's stigid) + carry
set item i of o's digits to d mod 10
set carry to d div 10
end repeat
if (carry > 0) then
set beginning of o's digits to carry
set digitCount to digitCount + 1
end if
-- If the sum's digits are palindromic, the start number's not a Lychrel.
set o's stigid to reverse of o's digits
set sumIsPalindrome to (o's digits = o's stigid)
if (sumIsPalindrome) then exit repeat
-- Otherwise, if the digits occurred in an earlier Lychrel sequence, the start number's a related Lychrel.
set startNumberIsRelated to (o's isRelated())
if (startNumberIsRelated) then
set sumIsPalindrome to false
exit repeat
end if
-- Otherwise keep a copy of the digits and go for the next number in the sequence.
copy o's digits to end of o's digitLists
end repeat
if (not sumIsPalindrome) then
-- No palindrome other than the start number occurred in the sequence,
-- so the number's a Lychrel. Store it as the relevant type.
if (startNumberIsRelated) then
set end of o's relateds to startNumber
else
set end of o's seeds to startNumber
end if
if (startNumberIsPalindrome) then set end of o's palindromics to startNumber
-- Append the sequence's digit lists to the appropriate same-last-digit groups in earlierDigitLists.
repeat with thisList in o's digitLists
set end of item ((end of thisList) + 1) of o's earlierDigitLists to thisList's contents
end repeat
end if
end if
end repeat
return {seeds:o's seeds, relateds:o's relateds, palindromics:o's palindromics}
end Lychrels
 
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
on task()
set numberRange to {1, 10000}
set iterationLimit to 500
set {seeds:seeds, relateds:relateds, palindromics:palindromics} to Lychrels(numberRange, iterationLimit)
set output to {"Lychrel numbers between " & beginning of numberRange & (" and " & end of numberRange & ":"), ""}
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 palindromics) as text) & " palindromic Lychrels: " & join(palindromics, ", ")
return join(output, linefeed)
end task
 
task()</lang>
 
{{output}}
<lang applescript>"Lychrel numbers between 1 and 10000:
 
5 seed Lychrels: 196, 879, 1997, 7059, 9999
244 related Lychrels
3 palindromic Lychrels: 4994, 8778, 9999"</lang>
 
=={{header|BASIC}}==
557

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.