Proper divisors: Difference between revisions

m
→‎version 3, the easy way: added/changed comments and whitespace, changed indentations.
m (→‎version 2: added/changed comments and whitespace, changed indentations.)
m (→‎version 3, the easy way: added/changed comments and whitespace, changed indentations.)
Line 1,975:
 
It accomplishes a faster speed by incorporating the calculation of an   ''integer square root''   of an integer   (without using any floating point arithmetic).
<lang rexx>/*REXX program finds proper divisors (&and count) of integer ranges; &finds the max count.*/
parse arg bot top inc range xtra /*get optional arguments from CL.*/
top=word(top bot 10,1); bot=word(bot 1,1); inc=word(inc 1,1) /*1stfirst range.*/
if range=='' then range=top /*use TOP for the 2nd range. */
w=length(top)+1; numeric digits max(9,w) /*'nuff digits for // operation*/
m=0
do n=bot to top by inc; q=Pdivs(n); #=words(q); if q=='∞' then #=q
Line 1,987:
do r=1 for range; q=Pdivs(r); #=words(q); if #<m then iterate
@.#=@.# @. r; m=#
end /*r*/ /* [↑] process 2nd range of integers.*/
__= 'is the highest number of proper divisors in range 1──►'range
say m __", and it's for: " subword(@.m,3)
say /* [↓] handle any given extra numbers.*/
do i=1 for words(xtra); n=word(xtra,i)
numeric digits max(9,length(n)+1); q=Pdivs(n); #=words(q)
say right(n,max(20,w)) 'has' center(#,4) "proper divisors."
end /*i*/ /* [↑] support extra specified integers*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
Pdivs: procedure; parse arg x,b; x=abs(x); if x==1 then return ''
odd=x//2; if x==0 then return '∞' /* ___ ___*/
z=x; r=0; q=1; do while q<=z; q=q*4; end /*R: an integer /*Rwhich will be the integer √ X */
do while q>1; q=q%4; _=z-r-q; r=r%2; if _>=0 then do; z=_; r=r+q; end; end
a=1 /* [↓] use all, or only odd #s. ___ */
do j=2+odd by 1+odd to r-(r*r==x) /*divide by some integers up to √ X */
if x//j==0 then do; a=a j; b=x%j b; end /*if ÷, add both divisors to α & ß. */
end /*j*/ /* [↑] % is the REXX integer division*/end
end /*j*/ /* [] adjust for a square. % is the REXX ___integer division*/
if j*j==x then return a j b /*Was [↓] X adjust for a square? . If so, add X ___*/
if j*j==x then return a j b return a b /*Was X /*return thea divisorssquare? (both lists). If so, add √ X */</lang>
return a b /*return the divisors (both lists). */</lang>
'''output''' &nbsp; is identical to the 2<sup>nd</sup> REXX version. <br><br>