Abundant, deficient and perfect number classifications: Difference between revisions

→‎{{header|REXX}}: added version 1.5 which is about 30% than version 1.
m (→‎version 1: align whitespace in a comment.)
(→‎{{header|REXX}}: added version 1.5 which is about 30% than version 1.)
Line 699:
the number of deficient numbers: 15043
</pre>
 
===version 1.5===
This version is pretty much the same as the 1<sup>st</sup> version but uses an &nbsp; ''integer square root'' &nbsp; function to calculate the limit of the &nbsp; '''do''' &nbsp; loop in the &nbsp; '''sigma''' &nbsp; function.
<lang rexx>/*REXX pgm counts the # of abundant/deficient/perfect numbers in a range*/
parse arg low high . /*get optional arguments*/
high=word(high low 20000,1); low=word(low 1,1) /*get the LOW and HIGH. */
say center('integers from ' low " to " high, 45, "═")
!.=0 /*set all types of sums to zero. */
do j=low to high; $=sigma(j) /*find the sigma for an int range*/
if $<j then !.d=!.d+1 /*it's a deficient number*/
else if $>j then !.a=!.a+1 /* " " abundant " */
else !.p=!.p+1 /* " " perfect " */
end /*j*/
 
say ' the number of perfect numbers: ' right(!.p, length(high))
say ' the number of abundant numbers: ' right(!.a, length(high))
say ' the number of deficient numbers: ' right(!.d, length(high))
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────ISQRT subroutine────────────────────*/
iSqrt: procedure; parse arg x; q=1; r=0; do while q<=x; q=q*4; end
do while q>1; q=q%4; _=x-r-q; r=r%2; if _>=0 then do; x=_; r=r+q; end
end /*while q>1*/
return r
/*──────────────────────────────────SIGMA subroutine────────────────────*/
sigma: procedure; parse arg x; odd=x//2 /* [↓] some special cases.*/
if x<5 then do; if x==0 then return 0; return x-1; end
sqX=iSqrt(x) /*calculate the integer square rt*/
s=1 /* [↓] use only EVEN|ODD integers*/
do j=2+odd by 1+odd to sqX /*divide by all integers up to √x*/
if x//j==0 then s=s+j+ x%j /*add the two divisors to the sum*/
end /*j*/ /* [↑] % is REXX integer divide*/
/* [↓] adjust for square. _ */
if sqX*sqX==x then s=s-j /*Is X a square? If so, sub √x.*/
return s /*return the sum of the divisors.*/</lang>
'''output''' is the same as the 1<sup>st</sup> version.
 
===version 2===