Jump to content

Abundant, deficient and perfect number classifications: Difference between revisions

J
m (mention the special case at 1 in the definition of proper divisors.)
(J)
Line 15:
* [[Proper divisors]]
* [[Amicable pairs]]
 
=={{header|J}}==
 
[[Proper divisors#J|Supporting implementation]]:
 
<lang J>factors=: [: /:~@, */&>@{@((^ i.@>:)&.>/)@q:~&__
properDivisors=: factors -. -.&1</lang>
 
We can subtract the sum of a number's proper divisors from itself to classify the number:
 
<lang J> (- +/@properDivisors&>) 1+i.10
0 1 2 1 4 0 6 1 5 2</lang>
 
Except, we are only concerned with the sign of this difference:
 
<lang J> *(- +/@properDivisors&>) 1+i.30
0 1 1 1 1 0 1 1 1 1 1 _1 1 1 1 1 1 _1 1 _1 1 1 1 _1 1 1 1 0 1 _1</lang>
 
Also, we do not care about the individual classification but only about how many numbers fall in each category:
 
<lang J> #/.~ *(- +/@properDivisors&>) 1+i.20000
5 15042 4953</lang>
 
So: 5 perfect, 15042 deficient and 4953 abundant numbers in this range.
 
How do we know which is which? We look at the unique values (which are arranged by their first appearance, scanning the list left to right):
 
<lang J> ~. *(- +/@properDivisors&>) 1+i.20000
0 1 _1</lang>
 
The sign of the difference is negative for the abundant case - where the sum is greater than the number. And we rely on order being preserved in sequences (this happens to be a fundamental property of computer memory, also).
 
=={{header|Python}}==
6,962

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.