Jump to content

Aliquot sequence classifications: Difference between revisions

Remove proper divisors part from Scala solution
(Scala solution)
(Remove proper divisors part from Scala solution)
Line 753:
 
=={{header|Scala}}==
With [[proper_divisors#Scala]] /Proper divisors for big (long) numbers/ in place:
<lang Scala>import annotation.tailrec
<lang Scala>def createAliquotSeq(n: Long, step: Int, list: List[Long]): (String, List[Long]) = {
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))
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.