Factors of an integer: Difference between revisions

m
→‎version 1: added/changed whitespace and comments, changed wording in the REXX section header.
(clarified (I hope); try to avoid pedantic footnote that '1' has only one factor.)
m (→‎version 1: added/changed whitespace and comments, changed wording in the REXX section header.)
Line 2,309:
=={{header|REXX}}==
===optimized version===
This REXX version has no effective limits on the number of decimal digits in the number to be factored   [by adjusting the number of digits (precision)].
<br>This REXX version also supports negative integers and zero.
<br>It also indicates &nbsp; '''primes''' &nbsp; in the output as well as the number of factors.
Line 2,317:
w=length(high)+1; numeric digits max(9,w); $='∞' /*digits for // */
@.=left('',7); @.1='{unity}'; @.2='[prime]'; @.$=' {'$"} " /*some literals. */
say center('n',1+w) '#divisors' center('divisors',60) /*show the header*/
say copies('═',1+w) '═════════' copies('═' ,60) /* " " sep. */
 
do n=bot to top by inc; divs=divisors(n); #=words(divs)
if divs==$ then do; #=$; divs=' (infinite)'; end /*handle infinity*/
p=@.#; if n<0 then p=@.. /*handle negative*/
say right(n,w)" " center('['#"]",9) "──► " p ' ' divs
end /*n*/ /* [↑] process a range of integers. */
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────DIVISORS subroutine───────────────────────*/
divisors: procedure expose $; parse arg x; x=abs(x/1); if x==1 then return 1
odd=x//2; b=x; if x==0 then return $
a=1 /* [↓] process only EVEN|ODD integers.*/
do j=2+odd by 1+odd while j*j<x /*divide by all integers up to √x. */
if x//j==0 then do; a=a j; b=x%j b; end /*if ÷, add factors to α&ß lists if ÷.*/
end /*j*/ /* [↑] % is the REXX integer dividedivision. */
/* [↓] adjust for a square. _ ____*/
if j*j==x then return a j b /*Was X a square? If so, add √x. √ x */
return a b /*return all the divisors (both lists). */</lang>
'''output''' &nbsp; when the input used is: &nbsp; <tt> -6 &nbsp; 200 </tt>
<pre style="height:65ex">
n #divisors divisors