Abundant, deficient and perfect number classifications: Difference between revisions
Content added Content deleted
(Scala solution) |
|||
Line 373: | Line 373: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
<lang Scala>def properDivisors(n: Int) = (1 to n/2).filter(i => n % i == 0) |
<lang Scala>def properDivisors(n: Int) = (1 to n/2).filter(i => n % i == 0) |
||
def divider(divisors: (Int, Seq[Int])) = divisors. |
def divider(divisors: (Int, Seq[Int])) = divisors._2.sum compare divisors._1 |
||
val groupNames = Vector("Deficient", "Perfect", "Abundant") |
val groupNames = Vector("Deficient", "Perfect", "Abundant") |
||
val divisors = (1 to 20000).map( i => (i, properDivisors(i)) ) |
val divisors = (1 to 20000).map( i => (i, properDivisors(i)) ) |
||
Line 379: | Line 379: | ||
groups.foreach( v => println(groupNames(v._1 + 1) + ": " + v._2.length) )</lang> |
groups.foreach( v => println(groupNames(v._1 + 1) + ": " + v._2.length) )</lang> |
||
{{out}} |
{{out}} |
||
<pre>Deficient: |
<pre>Deficient: 15043 |
||
Abundant: |
Abundant: 4953 |
||
Perfect: 4</pre> |
Perfect: 4</pre> |
||