Factors of an integer: Difference between revisions

→‎{{header|REXX}}: add alternate version (restructured from original)
(→‎{{header|REXX}}: add alternate version (restructured from original))
Line 1,927:
n = 200 divisors = 1 2 4 5 8 10 20 25 40 50 100 200
</pre>
===Alternate Version===
<lang>/* REXX ***************************************************************
* Program to calculate and show divisors of positive integer(s).
* 03.08.2012 Walter Pachl simplified the above somewhat
* in particular I see no benefit from divAdd procedure
**********************************************************************/
Parse arg low high .
Select
When low='' Then Parse Value '1 200' with low high
When high='' Then high=low
Otherwise Nop
End
do j=low to high
say ' n = ' right(j,6) " divisors = " divs(j)
end
exit
divs: procedure; parse arg x .;
Parse Value '' With lo hi /*initialize lists to empty */
if x==1 then return 1 /*handle special case of 1 */
do j=2 By 1 While j*j<x /*divide by integers<sqrt(x) */
if x//j==0 then Do /*Divisible? Add two divisors:*/
lo=lo j /* list low divisors */
hi=x%j hi /* list high divisors */
End
End
if j*j==x then /* special case: a square */
lo=lo j /* add the root lo low list */
return space(1 lo hi x) /* return border values and both lists */</lang>
 
=={{header|Ruby}}==
2,295

edits