Anonymous user
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 '''primes''' 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)
say copies('═',1+w) '═════════' copies('═' ,60)
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)
end /*n*/ /* [↑] process a range of integers. */
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
divisors: procedure expose $; parse arg x; x=abs(x
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
end /*j*/ /* [↑] % is
/* [↓] adjust for a square.
if j*j==x then return a j b /*Was X a square?
return a b /*return all the divisors
'''output''' when the input used is: <tt> -6 200 </tt>
<pre style="height:65ex">
n #divisors divisors
|