Abundant, deficient and perfect number classifications: Difference between revisions
Content added Content deleted
(scheme implementation) |
(→{{header|REXX}}: added the REXX language. -- ~~~~) |
||
Line 524: | Line 524: | ||
15043 deficient numbers |
15043 deficient numbers |
||
4 perfect numbers |
4 perfect numbers |
||
</pre> |
|||
=={{header|REXX}}== |
|||
<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.*/ |
|||
/*──────────────────────────────────SIGMA subroutine────────────────────*/ |
|||
sigma: procedure; parse arg x; if x<2 then return 0; odd=x//2 |
|||
s=1 /* [↓] use only EVEN|ODD integers*/ |
|||
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 the sum*/ |
|||
end /*j*/ /* [↑] % is REXX integer divide*/ |
|||
/* [↓] adjust for square. _ */ |
|||
if j*j==x then s=s+j /*Was X a square? If so, add √x.*/ |
|||
return s /*return the sum of the divisors.*/</lang> |
|||
'''output''' when using the default inputs: |
|||
<pre> |
|||
═════════integers from 1 to 20000═════════ |
|||
the number of perfect numbers: 4 |
|||
the number of abundant numbers: 4953 |
|||
the number of deficient numbers: 15043 |
|||
</pre> |
</pre> |
||