Anonymous user
Abundant, deficient and perfect number classifications: Difference between revisions
Abundant, deficient and perfect number classifications (view source)
Revision as of 18:20, 1 August 2015
, 8 years ago→{{header|REXX}}: added/changed whitespace and comments, added optimization.
m (→{{header|REXX}}: added/changed whitespace and comments, added optimization.) |
|||
Line 1,162:
=={{header|REXX}}==
===version 1===
<lang rexx>/*REXX pgm counts the
parse arg low high . /*get optional
high=word(high low 20000,1); low=word(low 1,1) /*get the LOW and HIGH
say center('integers from ' low " to " high, 45, "═")
do j=low to high; $=sigma(j) /*find the sigma for an integer range. */
end /*j*/▼
▲ 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 all done. */
/*────────────────────────────────────────────────────────────────────────────*/
sigma: procedure; parse arg x; if x<2 then return 0;
s=1 /* [↓]
do j=2+odd by 1+odd while j*j<x /*divide by all integers up to √x. */
if x//j==0 then s=s+j+ x%j /*add the two divisors to
end /*j*/ /* [↑] % is the REXX integer
/* [↓] adjust for a square.
if j*j==x then s=s+j /*Was X a square? If so, add
return s /*return
'''output''' when using the default inputs:
<pre>
Line 1,197 ⟶ 1,196:
===version 1.5===
This version is pretty much the same as the 1<sup>st</sup> version but uses an ''integer square root'' function to calculate the limit of the '''do''' loop in the '''sigma''' function.
<lang rexx>/*REXX pgm counts the
parse arg low high . /*get optional
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 /*
do j=low to high; $=sigma(j) /*find the sigma for an
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 all done. */
/*────────────────────────────────────────────────────────────────────────────*/
iSqrt: procedure; parse arg x; q=1; r=0; do while q<=x; q=q*4;
do while q>1; q=q%4; _=x-r-q; r=r%2;
end /*while
return r
/*────────────────────────────────────────────────────────────────────────────*/
sigma: procedure; parse arg x;
if x//j==0 then s=s+j+ x%j /*add the two divisors to (sigma) sum. */
▲s=1 /* [↓] use only EVEN|ODD integers*/
if sqx*sqx==x then
return
'''output''' is the same as the 1<sup>st</sup> version.
|