Anonymous user
Proper divisors: Difference between revisions
m
→version 2: changed whitespace and comments, added better support for zero.
m (→{{header|J}}) |
m (→version 2: changed whitespace and comments, added better support for zero.) |
||
Line 1,212:
With the (subroutine) optimization, it's over twenty times faster.
<lang rexx>/*REXX
parse arg
if range=='' then range=
w=length(
m=0
do n=
say right(n,digits()) 'has' center(#,4) "proper divisors: " q
end /*n*/ /* [↑] process a range of
say; @.='and'
do r=1 for range; q=Pdivs(r); #=words(q); if #<m then iterate
▲ end /*r*/ /* [↑] process a range of ints.*/
say m 'is the highest number of proper divisors in range 1──►'range", 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,
say right(n,digits()) 'has' center(#,4) "proper divisors."
end /*i*/ /* [↑] support extra given
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────PDIVS
Pdivs: procedure; parse arg x,b; x=abs(x); if x==1 then return ''
odd=x//2; if x==0 then return '
a=1 /* [↓]
do j=2+odd by 1+odd while j*j<x
if x//j==0 then do; a=a j; b=x%j b; end /*add
end /*j*/
/* [↓] adjust for square. _ */
if j*j==x then return a j b /*Was X a square? If so, add √x. */
return a b /*return the divisors (both lists). */</lang>
'''output''' when using the following input: <tt>
<pre>
0 has ∞ proper divisors: ∞
1 has 0 proper divisors:
2 has 1 proper divisors: 1
|