Anonymous user
Factors of an integer: Difference between revisions
m
→optimized version: simplifed code, changed whitespace and comments.
No edit summary |
m (→optimized version: simplifed code, changed whitespace and comments.) |
||
Line 5,056:
===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. ▼
It also indicates '''primes''' in the output listing as well as the number of factors.▼
<br>It also displays a final count of the number of primes found.▼
▲It also indicates '''primes''' in the output listing as well as the <u>number</u> of factors.
<lang rexx>/*REXX program displays divisors of any [negative/zero/positive] integer or a range.*/
parse arg LO HI inc . /*obtain the optional args*/
HI= word(HI LO 20, 1); LO= word(LO 1,1); inc= word(inc 1,1) /*define the range options*/
w= length(HI) + 2; numeric digits max(9, w-2);
@.=left('',7); @.1= "{unity}"; @.2= '[prime]'; @.
say center('n', w) "#divisors" center('divisors', 60) /*display the header. */
say copies('═', w) "═════════" copies('═' , 60) /* " " separator. */
do n=LO to HI by inc;
if
p= @.#;
if p==@.2 then
say center(n, w) center('['#"]", 9) "──► " p ' '
end /*n*/ /* [↑] process a range of integers. */
say
say right(
exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
if x<2 then do; x= abs(x); if x==1 then return 1;
odd= x // 2 /* [↓] process EVEN or ODD ints. ___*/
do j=2+odd by 1+odd while j*j<x /*divide by all the integers up to √ x */
if x//j==0 then do; a=a j; b=x%j b; end /*÷? Add factors to α and ß lists.*/
end /*j*/ /* [↑] % ≡ integer division. ___*/
if j*j==x then return a j b
{{out|output|text= when using the input of: <tt> -6 200 </tt>}}
|