Non-Abundant Sums
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of \(28\) would be \(1 + 2 + 4 + 7 + 14 = 28\), which means that \(28\) is a perfect number.
A number \(n\) is called deficient if the sum of its proper divisors is less than \(n\) and it is called abundant if this sum exceeds \(n\).
As \(12\) is the smallest abundant number, \(1 + 2 + 3 + 4 + 6 = 16\), the smallest number that can be written as the sum of two abundant numbers is \(24\). By mathematical analysis, it can be shown that all integers greater than \(28123\) can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.
Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
Solution
We can obtain the divisors by reusing the code from Problem 21:
private def divisors(n: Int): Seq[Int] = 1 +: (2 to Math.sqrt(n).toInt).flatMap {
case i if n % i == 0 => if (i != n / i) List(i, n / i) else List(i)
case _ => List()
}
Now it is easy to check whether a number is abundant. Simply compare the sum of its divisors with the number itself:
private def isAbundant(n: Int): Boolean = divisors(n).sum > n
But how do we tell if a number is the sum of two abundant numbers? Checking all possibilities would be inefficient. In the worst case, we would need to check every pair of numbers, which leads to \(O(n^2))\) complexity. But there’s a way to make it linear!
If we subtract an abundant number \(a\) of ouf the number \(n\) we’re checking, then we only need to see whether
the different is also abundant. There is one problem, however, with our simple isAbundant method. It recomputes the
divisors every time, which is wasteful.
Once we know all the abundant numbers within a given range, we determine whether a number is abundant
(within that range) by simply checking if it’s inside the set of known abundant numbers.
The wording here is crucial, string Set rather than a List to do that search is crucial.
Here’s the complete solution:
object Euler023 extends EulerApp {
override def execute(): Any = {
val limit = 28123
val abundantNumbers = (1 to limit).filter(isAbundant)
val abundantNumbersSet = abundantNumbers.toSet
(1 to limit).filterNot { n =>
isSumOfTwoAbundant(n, abundantNumbers, abundantNumbersSet)
}.sum
}
private def isSumOfTwoAbundant(
n: Int,
abundantNumbers: Seq[Int],
abundantNumbersSet: Set[Int]
): Boolean =
abundantNumbers.takeWhile(_ <= n).exists { a =>
abundantNumbersSet.contains(n - a)
}
private def isAbundant(n: Int): Boolean = divisors(n).sum > n
private def divisors(n: Int): Seq[Int] = 1 +: (2 to Math.sqrt(n).toInt).flatMap {
case i if n % i == 0 => if (i != n / i) List(i, n / i) else List(i)
case _ => List()
}
}
On my notebook it runs in less than a second.