Anonymous user
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 (
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) /*
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.*/
say m __", and it's for: " subword(@.m,3)
say /* [↓] handle any given extra numbers.*/
do i=1 for words(xtra);
numeric digits max(9,length(n)+1);
say right(n,max(20,w)) 'has'
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;
z=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 /*j*/ /* [
if j*j==x then return a j b
return a b /*return the divisors (both lists). */</lang>
'''output''' is identical to the 2<sup>nd</sup> REXX version. <br><br>
|