Proper divisors: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Optimize, add some verbiage about optimization and concurrency) |
m (→version 2: added whitespace.) |
||
Line 3,642: | Line 3,642: | ||
<lang rexx>/*REXX program finds proper divisors (and count) of integer ranges; finds the max count.*/ |
<lang rexx>/*REXX program finds proper divisors (and count) of integer ranges; finds the max count.*/ |
||
parse arg bot top inc range xtra /*obtain optional arguments from the CL*/ |
parse arg bot top inc range xtra /*obtain optional arguments from the CL*/ |
||
if bot=='' | bot=="," then bot= |
if bot=='' | bot=="," then bot= 1 /*Not specified? Then use the default.*/ |
||
if top=='' | top=="," then top= |
if top=='' | top=="," then top= 10 /* " " " " " " */ |
||
if inc=='' | inc=="," then inc= |
if inc=='' | inc=="," then inc= 1 /* " " " " " " */ |
||
if range=='' | range=="," then range= |
if range=='' | range=="," then range= 20000 /* " " " " " " */ |
||
w= |
w= max( length(top), length(bot), length(range)) /*determine the biggest number of these*/ |
||
numeric digits max(9, w |
numeric digits max(9, w + 1) /*have enough digits for // operator.*/ |
||
@.= 'and' /*a literal used to separate #s in list*/ |
@.= 'and' /*a literal used to separate #s in list*/ |
||
do n=bot to top by inc /*process the first range specified. */ |
do n=bot to top by inc /*process the first range specified. */ |
||
q=Pdivs(n); |
q= Pdivs(n); #= words(q) /*get proper divs; get number of Pdivs.*/ |
||
if q=='∞' then #= |
if q=='∞' then #= q /*adjust number of Pdivisors for zero. */ |
||
say right(n, max(20, w) ) 'has' center(#, 4) "proper divisors: " q |
say right(n, max(20, w) ) 'has' center(#, 4) "proper divisors: " q |
||
end /*n*/ |
end /*n*/ |
||
Line 3,657: | Line 3,657: | ||
m=0 /*M ≡ maximum number of Pdivs (so far).*/ |
m=0 /*M ≡ maximum number of Pdivs (so far).*/ |
||
do r=1 for range /*process the second range specified. */ |
do r=1 for range /*process the second range specified. */ |
||
q=Pdivs(r); |
q= Pdivs(r); #= words(q) /*get proper divs; get number of Pdivs.*/ |
||
if #<m then iterate /*Less then max? Then ignore this #. */ |
if #<m then iterate /*Less then max? Then ignore this #. */ |
||
@.#=@.# @. r; m=# |
@.#= @.# @. r; m=# /*add this Pdiv to max list; set new M.*/ |
||
end /*r*/ /* [↑] process 2nd range of integers.*/ |
end /*r*/ /* [↑] process 2nd range of integers.*/ |
||
Line 3,665: | Line 3,665: | ||
", and it's for: " subword(@.m, 3) |
", and it's for: " subword(@.m, 3) |
||
say /* [↓] handle any given extra numbers.*/ |
say /* [↓] handle any given extra numbers.*/ |
||
do i=1 for words(xtra); n=word(xtra, i) |
do i=1 for words(xtra); n= word(xtra, i) /*obtain an extra number from XTRA list*/ |
||
w=max(w, 1 + length(n) ) |
w= max(w, 1 + length(n) ) /*use maximum width for aligned output.*/ |
||
numeric digits max(9, 1 + length(n) ) /*have enough digits for // operator.*/ |
numeric digits max(9, 1 + length(n) ) /*have enough digits for // operator.*/ |
||
q=Pdivs(n); |
q= Pdivs(n); #= words(q) /*get proper divs; get number of Pdivs.*/ |
||
say right(n, max(20, w) ) 'has' center(#, 4) "proper divisors." |
say right(n, max(20, w) ) 'has' center(#, 4) "proper divisors." |
||
end /*i*/ /* [↑] support extra specified integers*/ |
end /*i*/ /* [↑] support extra specified integers*/ |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Pdivs: procedure; parse arg x,b; x=abs(x); |
Pdivs: procedure; parse arg x,b; x= abs(x); if x==1 then return '' /*unity?*/ |
||
odd=x // 2; |
odd= x // 2; if x==0 then return '∞' /*zero ?*/ |
||
a= 1 /* [↓] use all, or only odd #s. ___*/ |
|||
do j=2+odd by 1+odd while j*j < x /*divide by some integers up to √ X */ |
|||
do while q>1; q=q%4; _=z-r-q; r=r%2; if _>=0 then do; z=_; r=r+q; end |
|||
end /*while q>1*/ /* [↑] compute the integer sqrt of X.*/ |
|||
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 /*if ÷, add both divisors to α & ß. */ |
if x//j==0 then do; a=a j; b=x%j b /*if ÷, add both divisors to α & ß. */ |
||
end |
end |