Factors of an integer: Difference between revisions
Content added Content deleted
m (→using Prime decomposition: using TIO.RUN .delete text accidently inserted by middle mouse click) |
(→optimized version: further optimized the code using memoization.) |
||
Line 5,261: | Line 5,261: | ||
It also displays a final count of the number of primes found. |
It also displays a final count of the number of primes found. |
||
This REXX version is about '''22%''' faster than the alternate REXX version (2<sup>nd</sup> version). |
|||
<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*/ |
||
Line 5,268: | Line 5,270: | ||
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. */ |
||
pn= |
pn= 0 /*count of prime numbers. */ |
||
do k=2 until sq.k>=HI; sq.k= k*k /*memoization for squares.*/ |
|||
end /*k*/ |
|||
do n=LO to HI by inc; $= divs(n); #= words($) /*get list of divs; # divs*/ |
do n=LO to HI by inc; $= divs(n); #= words($) /*get list of divs; # divs*/ |
||
if $==! then do; #= !; $= ' (infinite)'; end /*handle case for infinity*/ |
if $==! then do; #= !; $= ' (infinite)'; end /*handle case for infinity*/ |
||
Line 5,279: | Line 5,284: | ||
exit 0 /*stick a fork in it, we're all done. */ |
exit 0 /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
divs: procedure; parse arg x 1 b; |
divs: procedure expose sq.; 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 |
if x<2 then do; x= abs(x); if x==1 then return 1; if x==0 then return '∞'; b=x |
||
end |
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 |
do j=2+odd by 1+odd while sq.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 divisors to α and ß lists*/ |
if x//j==0 then do; a=a j; b=x%j b; end /*÷? Add divisors to α and ß lists*/ |
||
end /*j*/ /* [↑] % ≡ integer division. ___*/ |
end /*j*/ /* [↑] % ≡ integer division. ___*/ |
||
if |
if sq.j==x then return a j b /*Was X a square? Then insert √ x */ |
||
return a b |
return a b /*return the divisors of both lists. */</lang> |
||
{{out|output|text= when using the input of: <tt> -6 200 </tt>}} |
{{out|output|text= when using the input of: <tt> -6 200 </tt>}} |
||