Jump to content

Lychrel numbers: Difference between revisions

m
→‎{{Header|REXX}}: changed whitespace and indentations.
(Add Swift)
m (→‎{{Header|REXX}}: changed whitespace and indentations.)
Line 2,855:
numeric digits 250 /*ensure enough decimal digits for adds*/
parse arg high . /*obtain optional argument from the CL.*/
if high='' | high=="," then high=10000 10000 /*Not specified? Then use the default.*/
limit=500 500 /*limit: number of Lychrel iterations.*/
T.= 0; @.=0 T.; #.=0@.; R=; w= length(high) /*W: is used for formatting numbers. */
$= /*the list of Lychrel numbers. */
do j=1 for high; call Lychrel j call Lychrel j /*find the Lychrel numbers. */
end /*j*/
p=; R= /*theP: 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:' $
Line 2,876:
/*──────────────────────────────────────────────────────────────────────────────────────*/
Lychrel: procedure expose limit @. #. T.; parse arg x 1 z /*set X and Z to argument 1.*/
rels=0 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 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</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Found in the range 1 to 10000 (limiting searches to 1000 steps):
 
5 Lychrel numbers: 196 879 1997 7059 9999
Cookies help us deliver our services. By using our services, you agree to our use of cookies.