Abundant, deficient and perfect number classifications: Difference between revisions

attempt to restore major damage from Feb 9
(attempt to restore major damage from Feb 9)
Line 84:
Abundant: 4953
Deficient: 15043
</pre>
 
=={{header|C}}==
<lang c>
#include<stdio.h>
#define d 0
#define p 1
#define a 2
int main(){
int sum_pd=0,i,j;
int try_max=0;
//1 is deficient by default and can add it deficient list
int count_list[3]={1,0,0};
for(i=2;i<=20000;i++){
//Set maximum to check for proper division
try_max=i/2;
//1 is in all proper division number
sum_pd=1;
for(j=2;j<try_max;j++){
//Check for proper division
if (i%j)
continue; //Pass if not proper division
//Set new maximum for divisibility check
try_max=i/j;
//Add j to sum
sum_pd+=j;
if (j!=try_max)
sum_pd+=try_max;
}
//Categorize summation
if (sum_pd<i){
count_list[d]++;
continue;
}
else if (sum_pd>i){
count_list[a]++;
continue;
}
count_list[p]++;
}
printf("\nThere are %d deficient,",count_list[d]);
printf(" %d perfect,",count_list[p]);
printf(" %d abundant numbers between 1 and 20000.\n",count_list[a]);
return 0;
}
</lang>
{{out}}
<pre>
There are 15043 deficient, 4 perfect, 4953 abundant numbers between 1 and 20000.
</pre>
 
Line 107 ⟶ 156:
{{out}}
<pre>[Tuple!(Class, uint)(deficient, 15043), Tuple!(Class, uint)(perfect, 4), Tuple!(Class, uint)(abundant, 4953)]</pre>
 
=={{header|Haskell}}==
<lang Haskell>divisors :: (Integral a) => a -> [a]
divisors n = filter ((0 ==) . (n `mod`)) [1 .. (n `div` 2)]
 
classOf :: (Integral a) => a -> Ordering
classOf n = compare (sum $ divisors n) n
 
main :: IO ()
main = do
let classes = map classOf [1 .. 20000 :: Int]
printRes w c = putStrLn $ w ++ (show . length $ filter (== c) classes)
printRes "deficient: " LT
printRes "perfect: " EQ
printRes "abundant: " GT</lang>
{{out}}
<pre>deficient: 15043
perfect: 4
abundant: 4953</pre>
 
=={{header|J}}==
Line 137 ⟶ 205:
 
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|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>classify[n_Integer] := Sign[Total[Most@Divisors@n] - n]
 
StringJoin[
Flatten[Tally[
Table[classify[n], {n, 20000}]] /. {-1 -> "deficient: ",
0 -> " perfect: ", 1 -> " abundant: "}] /.
n_Integer :> ToString[n]]</lang>
 
{{out}}<pre>deficient: 15043 perfect: 4 abundant: 4953</pre>
 
=={{header|ML}}==
==={{header|mLite}}===
<lang ocaml>fun proper
(number, count, limit, remainder, results) where (count > limit) = rev results
| (number, count, limit, remainder, results) =
proper (number, count + 1, limit, number rem (count+1), if remainder = 0 then
count :: results
else
results)
| number = (proper (number, 1, number div 2, 0, []))
;
 
fun is_abundant number = number < (fold (op +, 0) ` proper number);
fun is_deficient number = number > (fold (op +, 0) ` proper number);
fun is_perfect number = number = (fold (op +, 0) ` proper number);
 
val one_to_20000 = iota 20000;
 
print "Abundant numbers between 1 and 20000: ";
println ` fold (op +, 0) ` map ((fn n = if n then 1 else 0) o is_abundant) one_to_20000;
 
print "Deficient numbers between 1 and 20000: ";
println ` fold (op +, 0) ` map ((fn n = if n then 1 else 0) o is_deficient) one_to_20000;
 
print "Perfect numbers between 1 and 20000: ";
println ` fold (op +, 0) ` map ((fn n = if n then 1 else 0) o is_perfect) one_to_20000;
</lang>
Output
<pre>
Abundant numbers between 1 and 20000: 4953
Deficient numbers between 1 and 20000: 15043
Perfect numbers between 1 and 20000: 4
</pre>
 
=={{header|Oforth}}==
 
<lang Oforth>Integer method: properDivs { seq(self 2 / ) filter(#[ self swap rem 0 == ]) }
 
func: numberClasses
{
| i deficient perfect s |
0 0 ->deficient ->perfect
0 20000 loop: i [
i properDivs sum ->s
s i < ifTrue: [ deficient 1 + ->deficient continue ]
s i == ifTrue: [ perfect 1 + ->perfect continue ]
1 +
]
"Deficients : " print deficient println
"Perfects : " print perfect println
"Abundant : " print println
}</lang>
 
{{out}}
<pre>
numberClasses
Deficients : 15043
Perfects : 4
Abundant : 4953
</pre>
 
=={{header|Pascal}}==
using the http://rosettacode.org/wiki/Amicable_pairs#Alternative.
the program there is now extended by an array and a line to count.
<lang pascal>
type
tdpa = array[0..2] of LongWord; // 0 = deficient,1= perfect,2 = abundant
var
..
DpaCnt : tdpa;
..
in function Check
// SumOfProperDivs
s := DivSumField[i]-i;
//in Pascal boolean true == 1/false == 0
inc(DpaCnt[Ord(s>=i)-Ord(s<=i)+1]);
</lang>
output
<pre>
Max= 20000
15043 deficient
4 perfect
4953 abundant
0.3292561324 ratio abundant/deficient
 
MAX = 499*1000*1000
375440837 deficient
5 perfect
123559158 abundant
0.3291042045 ratio abundant/deficient
</pre>
 
=={{header|Perl}}==
Line 155 ⟶ 326:
{{out}}
<pre>Perfect: 4 Deficient: 15043 Abundant: 4953</pre>
 
=={{header|Perl 6}}==
<lang perl6>sub propdivsum (\x) {
[+] (1 if x > 1), gather for 2 .. x.sqrt.floor -> \d {
my \y = x div d;
if y * d == x { take d; take y unless y == d }
}
}
 
say bag map { propdivsum($_) <=> $_ }, 1..20000</lang>
{{out}}
<pre>bag(Less(15043), Same(4), More(4953))</pre>
 
=={{header|PL/I}}==
Anonymous user