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= |
if high='' | high=="," then high= 10000 /*Not specified? Then use the default.*/ |
||
limit= |
limit= 500 /*limit: number of Lychrel iterations.*/ |
||
T.=0; @.= |
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; |
do j=1 for high; call Lychrel j /*find the Lychrel numbers. */ |
||
end /*j*/ |
end /*j*/ |
||
p= |
p=; R= /*P: list of palindromes; R: related #s*/ |
||
do k=1 for high |
do k=1 for high |
||
if #.k then $=$ k |
if #.k then $= $ k /*build a list of Lychrel numbers. */ |
||
if T.k then R=R k |
if T.k then R= R k /* " " " " " related nums.*/ |
||
if T.k & k==reverse(k) then p=p k |
if T.k & k==reverse(k) then p= p k /* " " " " " palindromes. */ |
||
end /*k*/ |
end /*k*/ |
||
say 'Found in the range 1 to |
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= |
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= |
#.x= 1 /*mark number as a Lychrel.*/ |
||
T.x=1; |
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</lang> |
return</lang> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= 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 |