Factors of an integer: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added DO-END labels, remove superflous blanks. -- ~~~~)
m (→‎{{header|REXX}}: indented DO loops, added comments. -- ~~~~)
Line 1,692: Line 1,692:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program to calculate & show divisors of positive integer(s). */
<lang rexx>/*REXX program to calculate and show divisors of positive integer(s). */
parse arg low high .; if high=='' then high=low
parse arg low high .; if high=='' then high=low


Line 1,698: Line 1,698:
say 'n='right(j,6) "divisors="divs(j)
say 'n='right(j,6) "divisors="divs(j)
end /*j*/
end /*j*/
exit /*stick a fork in it, we're done.*/
exit
/*──────────────────────────────────DIVS subroutine─────────────────────*/
/*------------------------------DIVS subroutine---------------------*/
divs: procedure; parse arg x .
divs: procedure; parse arg x .; p.='' /*initilize P. lists to null. */
if x==1 then return 1 /*hand special case of unity (1). */
if x==1 then return 1 /*hand special case of unity (1).*/
p.='' /*initilize P.1 & P.2 lists to empty*/


do j=2 /*divide by all divisors < sqrt(x). */
do j=2 /*divide by all divisors √ +x. */
if j*j>=x then leave /*at sqrt(x) or greater? Then stop.*/
if j*j>=x then leave /*at √x or greater? Then stop.*/
if x//j==0 then call divAdd j,x%j /*Divisible? Add 2 divisors.*/
if x//j==0 then call divAdd j,x%j /*Divisible? Add two divisors. */
end
end /*j*/


if j*j==x then call divAdd j /*test for special case: a square. */
if j*j==x then call divAdd j /*test for special case: a square*/
/*up to this point, we just have */
/*calculated the proper divisors.*/
return space(1 p.1 p.2 x) /*return divisors: 1,both lists,x*/
/*──────────────────────────────────DIVADD subroutine───────────────────*/
divAdd: parse arg a,b /*add to "low" and/or high lists.*/


do k=1 for arg()
/*up to this point, we just have */
/*calculated the proper divisors. */
if k==1 then p.1=p.1 arg(k) /*append (ascending) to low list*/
else p.2=arg(k) p.2 /*build (descending) to high list*/

return space(1 p.1 p.2 x) /*return divisors: 1, both lists, x */
end /*k*/return</lang>
/*------------------------------DIVADD subroutine-------------------*/
divAdd: arg a,b /*add to "low" and/or "high" lists. */
do k=1 for arg()
if k==1 then p.1=p.1 arg(k) /*append (ascending) to "low" list.*/
else p.2=arg(k) p.2 /*build (descending) to "high" list.*/
end
return</lang>
'''output''' when the input is: <tt> 1 200 </tt>
'''output''' when the input is: <tt> 1 200 </tt>
<pre style="height:30ex;overflow:scroll">
<pre style="height:30ex;overflow:scroll">