Proper divisors: Difference between revisions
Content added Content deleted
m (Added a partial entry for the PicoLisp language.) |
(Scala solution) |
||
Line 863: | Line 863: | ||
1441440 has 287 proper divisors |
1441440 has 287 proper divisors |
||
</pre> |
</pre> |
||
=={{header|Scala}}== |
|||
<lang Scala>def properDivisors(n: Int) = (1 to n/2).filter(i => n % i == 0) |
|||
def format(i: Int, divisors: Seq[Int]) = f"$i%5d ${divisors.length}%2d ${divisors mkString " "}" |
|||
println(f" n cnt PROPER DIVISORS") |
|||
val (count, list) = (1 to 20000).foldLeft( (0, List[Int]()) ) { (max, i) => |
|||
val divisors = properDivisors(i) |
|||
if (i <= 10 || i == 100) println( format(i, divisors) ) |
|||
if (max._1 < divisors.length) (divisors.length, List(i)) |
|||
else if (max._1 == divisors.length) (divisors.length, max._2 ::: List(i)) |
|||
else max |
|||
} |
|||
list.foreach( number => println(f"$number%5d ${properDivisors(number).length}") )</lang> |
|||
{{out}} |
|||
<pre> n cnt PROPER DIVISORS |
|||
1 0 |
|||
2 1 1 |
|||
3 1 1 |
|||
4 2 1 2 |
|||
5 1 1 |
|||
6 3 1 2 3 |
|||
7 1 1 |
|||
8 3 1 2 4 |
|||
9 2 1 3 |
|||
10 3 1 2 5 |
|||
100 8 1 2 4 5 10 20 25 50 |
|||
15120 79 |
|||
18480 79</pre> |
|||
=={{header|Swift}}== |
=={{header|Swift}}== |