Aliquot sequence classifications: Difference between revisions

Scala solution
m (→‎{{header|REXX}}: added the REXX language. -- ~~~~)
(Scala solution)
Line 751:
non-terminating: [15355717786080, 44534663601120, 144940087464480]
</pre>
 
=={{header|Scala}}==
<lang Scala>import annotation.tailrec
import collection.parallel.mutable.ParSeq
 
def factorize(n: Long): List[Long] = {
@tailrec
def factors(tuple: (Long, Long, List[Long], Int)): List[Long] = {
tuple match {
case (1, _, acc, _) => acc
case (n, k, acc, _) if (n % k == 0) => factors((n / k, k, acc ++ ParSeq(k), Math.sqrt(n / k).toInt))
case (n, k, acc, sqr) if (k < sqr) => factors(n, k + 1, acc, sqr)
case (n, k, acc, sqr) if (k >= sqr) => factors((1, k, acc ++ ParSeq(n), 0))
}
}
factors((n, 2, List[Long](), Math.sqrt(n).toInt))
}
def properDivisors(n: Long) = {
val factors = factorize(n)
val products = (1 until factors.length).map(i =>
factors.combinations(i).map(_.product).toList).flatten
(1L +: products).filter(_ < n)
}
 
def createAliquotSeq(n: Long, step: Int, list: List[Long]): (String, List[Long]) = {
val sum = properDivisors(n).sum
if (sum == 0) ("terminate", list ::: List(sum))
else if (step >= 16 || sum > 140737488355328L) ("non-term", list)
else {
val index = list.indexOf(sum)
index match {
case -1 => createAliquotSeq(sum, step + 1, list ::: List(sum))
case 0 => if (step == 0) ("perfect", list ::: List(sum))
else if (step == 1) ("amicable", list ::: List(sum))
else ("sociable-" + (step - index + 1), list ::: List(sum))
case _ => if (step == index) ("aspiring", list ::: List(sum))
else ("cyclic-" + (step - index + 1), list ::: List(sum))
}
}
}
val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 28, 496, 220, 1184,
12496, 1264460, 790, 909, 562, 1064, 1488, 15355717786080L)
val result = numbers.map(i => createAliquotSeq(i, 0, List(i)))
 
result foreach { v =>
println(f"${v._2.head}%14d ${v._1}%10s [${v._2 mkString " "}]" ) }</lang>
 
{{out}}
<pre> 1 terminate [1 0]
2 terminate [2 1 0]
3 terminate [3 1 0]
4 terminate [4 3 1 0]
5 terminate [5 1 0]
6 perfect [6 6]
7 terminate [7 1 0]
8 terminate [8 7 1 0]
9 terminate [9 4 3 1 0]
10 terminate [10 8 7 1 0]
11 terminate [11 1 0]
12 terminate [12 16 15 9 4 3 1 0]
28 perfect [28 28]
496 perfect [496 496]
220 amicable [220 284 220]
1184 amicable [1184 1210 1184]
12496 sociable-5 [12496 14288 15472 14536 14264 12496]
1264460 sociable-4 [1264460 1547860 1727636 1305184 1264460]
790 aspiring [790 650 652 496 496]
909 aspiring [909 417 143 25 6 6]
562 cyclic-2 [562 284 220 284]
1064 cyclic-2 [1064 1336 1184 1210 1184]
1488 non-term [1488 2480 3472 4464 8432 9424 10416 21328 22320 55056 95728 96720 236592 459792 881392 882384 1474608]
15355717786080 non-term [15355717786080 44534663601120]</pre>
 
=={{header|zkl}}==
Anonymous user