Factors of an integer: Difference between revisions
Content added Content deleted
(→{{header|Sidef}}: mentioned the built-in function) |
m (→optimized version: added/changed whitespace and comments.) |
||
Line 4,106: | Line 4,106: | ||
<lang rexx>/*REXX program displays divisors of any [negative/zero/positive] integer or a range.*/ |
<lang rexx>/*REXX program displays divisors of any [negative/zero/positive] integer or a range.*/ |
||
parse arg LO HI inc . /*obtain the optional args*/ |
parse arg LO HI inc . /*obtain the optional args*/ |
||
HI=word(HI LO 20, 1); LO=word(LO 1, |
HI= word(HI LO 20, 1); LO= word(LO 1,1); inc= word(inc 1,1) /*define the range options*/ |
||
w=length( |
w= length(HI) + 2; numeric digits max(9, w-2); $= '∞' /*decimal digits for // */ |
||
@.=left('',7); |
@.=left('',7); @.1= "{unity}"; @.2= '[prime]'; @.$=" {"$'} '/*define some literals. */ |
||
say center('n', w) "#divisors" center('divisors', 60) /*display the header. */ |
say center('n', w) "#divisors" center('divisors', 60) /*display the header. */ |
||
say copies('═', w) "═════════" copies('═' , 60) /* " " separator. */ |
say copies('═', w) "═════════" copies('═' , 60) /* " " separator. */ |
||
p#=0 /*count of prime numbers. */ |
p#=0 /*count of prime numbers. */ |
||
do n=LO to HI by inc; divs=divisors(n); #=words(divs) /*get list of divs; # divs*/ |
do n=LO to HI by inc; divs=divisors(n); #=words(divs) /*get list of divs; # divs*/ |
||
if divs==$ then do; #= |
if divs==$ then do; #= $; divs= ' (infinite)'; end /*handle case for infinity*/ |
||
p=@.#; |
p= @.#; if n<0 then if n\==-1 then p= @.. /* " " " negative*/ |
||
if p==@.2 then p#=p#+ |
if p==@.2 then p#= p# + 1 /*Prime? Then bump counter*/ |
||
say center(n, w) center('['#"]", 9) "──► " p ' ' divs |
say center(n, w) center('['#"]", 9) "──► " p ' ' divs |
||
end /*n*/ /* [↑] process a range of integers. */ |
end /*n*/ /* [↑] process a range of integers. */ |
||
say |
say |
||
say |
say right(p#, 20) ' primes were found.' /*display the number of primes found. */ |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
divisors: procedure; parse arg x 1 b; a= |
divisors: procedure; parse arg x 1 b; a= 1 /*set X and B to the 1st argument. */ |
||
if x<2 then do; x=abs(x); if x==1 then return 1; if x==0 then return '∞'; b=x; end |
if x<2 then do; x= abs(x); if x==1 then return 1; if x==0 then return '∞'; b=x; end |
||
odd=x//2 /* [↓] process EVEN or ODD ints. ___*/ |
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 */ |
do j=2+odd by 1+odd while j*j<x /*divide by all the integers up to √ x */ |
||
Line 4,342: | Line 4,342: | ||
200 [12] ──► 1 2 4 5 8 10 20 25 40 50 100 200 |
200 [12] ──► 1 2 4 5 8 10 20 25 40 50 100 200 |
||
46 primes were found. |
|||
Primes that were found: 46 |
|||
</pre> |
</pre> |
||