How to resolve the algorithm Lychrel numbers step by step in the REXX programming language
How to resolve the algorithm Lychrel numbers step by step in the REXX programming language
Table of Contents
Problem Statement
The above recurrence relation when applied to most starting numbers n = 1, 2, ... terminates in a palindrome quite quickly.
If n0 = 12 we get And if n0 = 55 we get Notice that the check for a palindrome happens after an addition.
Some starting numbers seem to go on forever; the recurrence relation for 196 has been calculated for millions of repetitions forming numbers with millions of digits, without forming a palindrome. These numbers that do not end in a palindrome are called Lychrel numbers. For the purposes of this task a Lychrel number is any starting number that does not form a palindrome within 500 (or more) iterations.
Any integer produced in the sequence of a Lychrel number is also a Lychrel number. In general, any sequence from one Lychrel number might converge to join the sequence from a prior Lychrel number candidate; for example the sequences for the numbers 196 and then 689 begin: So we see that the sequence starting with 689 converges to, and continues with the same numbers as that for 196. Because of this we can further split the Lychrel numbers into true Seed Lychrel number candidates, and Related numbers that produce no palindromes but have integers in their sequence seen as part of the sequence generated from a lower Lychrel number.
Show all output here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Lychrel numbers step by step in the REXX programming language
Source code in the rexx programming language
/*REXX program finds and displays Lychrel numbers, related numbers, and palindromes. */
parse arg high limit . /*obtain optional argument from the CL.*/
if high='' | high=="," then high= 10000 /*Not specified? Then use the default.*/
if limit='' | limit=="," then limit= 500 /* " " " " " " */
numeric digits limit % 2 /*ensure enough decimal digits for adds*/
T.= 0; @.= T.; #.=@.; w= length(high) /*W: is used for formatting numbers. */
$= /*the list of Lychrel numbers. */
do j=1 for high; call Lychrel j /*find the Lychrel numbers. */
end /*j*/
p=; R= /*P: list of palindromes; R: related #s*/
do k=1 for high
if #.k then $= $ k /*build a list of Lychrel numbers. */
if T.k then R= R k /* " " " " " related nums.*/
if T.k & k==reverse(k) then p= p k /* " " " " " palindromes. */
end /*k*/
say 'Found in the range 1 to ' high " (limiting searches to " limit ' steps):'
say
say right( words($) , w) 'Lychrel numbers:' $
say right( words(R) - words($), w) 'Lychrel related numbers.'
say right( words(p) , w) 'Lychrel palindromes:' p
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Lychrel: procedure expose limit @. #. T.; parse arg x 1 z /*set X and Z to argument 1.*/
rels= 0 /*# related numbers (so far)*/
do limit; z= z + reverse(z) /*add the reverse of Z ··· */
if z==reverse(z) then return /*is the new Z a palindrome?*/
rels= rels + 1; !.rels= z /*add to the related numbers*/
end /*limit*/ /* [↑] only DO limit times.*/
#.x= 1 /*mark number as a Lychrel.*/
T.x= 1; do a=1 for rels; _= !.a /*process "related" numbers.*/
if @._ then #.x= 0 /*unmark number as Lychrel.*/
else @._= 1 /* mark " " " */
T._= 1 /*mark number as "related".*/
end /*a*/
return
You may also check:How to resolve the algorithm Copy stdin to stdout step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Deepcopy step by step in the C# programming language
You may also check:How to resolve the algorithm Chaocipher step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Order two numerical lists step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Compare a list of strings step by step in the FreeBASIC programming language