Lychrel numbers: Difference between revisions

Content added Content deleted
(Add Swift)
m (→‎{{Header|REXX}}: changed whitespace and indentations.)
Line 2,855: Line 2,855:
numeric digits 250 /*ensure enough decimal digits for adds*/
numeric digits 250 /*ensure enough decimal digits for adds*/
parse arg high . /*obtain optional argument from the CL.*/
parse arg high . /*obtain optional argument from the CL.*/
if high='' | high=="," then high=10000 /*Not specified? Then use the default.*/
if high='' | high=="," then high= 10000 /*Not specified? Then use the default.*/
limit=500 /*limit: number of Lychrel iterations.*/
limit= 500 /*limit: number of Lychrel iterations.*/
T.=0; @.=0; #.=0; R=; w=length(high) /*W: is used for formatting numbers. */
T.= 0; @.= T.; #.=@.; w= length(high) /*W: is used for formatting numbers. */
$= /*the list of Lychrel numbers. */
$= /*the list of Lychrel numbers. */
do j=1 for high; call Lychrel j /*find the Lychrel numbers. */
do j=1 for high; call Lychrel j /*find the Lychrel numbers. */
end /*j*/
end /*j*/
p= /*the list of palindromes. */
p=; R= /*P: list of palindromes; R: related #s*/
do k=1 for high
do k=1 for high
if #.k then $=$ k /*build a list of Lychrel numbers. */
if #.k then $= $ k /*build a list of Lychrel numbers. */
if T.k then R=R k /* " " " " " related nums.*/
if T.k then R= R k /* " " " " " related nums.*/
if T.k & k==reverse(k) then p=p k /* " " " " " palindromes. */
if T.k & k==reverse(k) then p= p k /* " " " " " palindromes. */
end /*k*/
end /*k*/


say 'Found in the range 1 to' high " (limiting searches to " limit ' steps):'
say 'Found in the range 1 to ' high " (limiting searches to " limit ' steps):'
say
say
say right(words($) , w) 'Lychrel numbers:' $
say right(words($) , w) 'Lychrel numbers:' $
Line 2,876: Line 2,876:
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
Lychrel: procedure expose limit @. #. T.; parse arg x 1 z /*set X and Z to argument 1.*/
Lychrel: procedure expose limit @. #. T.; parse arg x 1 z /*set X and Z to argument 1.*/
rels=0 /*# related numbers (so far)*/
rels= 0 /*# related numbers (so far)*/
do limit; z=z + reverse(z) /*add the reverse of Z ··· */
do limit; z= z + reverse(z) /*add the reverse of Z ··· */
if z==reverse(z) then return /*is the new Z a palindrome?*/
if z==reverse(z) then return /*is the new Z a palindrome?*/
rels=rels + 1; !.rels=z /*add to the related numbers*/
rels= rels + 1; !.rels= z /*add to the related numbers*/
end /*limit*/ /* [↑] only DO limit times.*/
end /*limit*/ /* [↑] only DO limit times.*/
#.x=1 /*mark number as a Lychrel.*/
#.x= 1 /*mark number as a Lychrel.*/
T.x=1; do a=1 for rels; _=!.a /*process "related" numbers.*/
T.x= 1; do a=1 for rels; _= !.a /*process "related" numbers.*/
if @._ then #.x=0 /*unmark number as Lychrel.*/
if @._ then #.x= 0 /*unmark number as Lychrel.*/
else @._=1 /* mark " " " */
else @._= 1 /* mark " " " */
T._=1 /*mark number as "related".*/
T._= 1 /*mark number as "related".*/
end /*a*/
end /*a*/
return</lang>
return</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Found in the range 1 to 10000 (limiting searches to 1000 steps):
Found in the range 1 to 10000 (limiting searches to 1000 steps):


5 Lychrel numbers: 196 879 1997 7059 9999
5 Lychrel numbers: 196 879 1997 7059 9999