Avatar of Ric0chet

Ric0chet's solution

to Perfect Numbers in the Scala Track

Published at Sep 23 2019 · 0 comments
Instructions
Test suite
Solution

Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for natural numbers.

The Greek mathematician Nicomachus devised a classification scheme for natural numbers, identifying each as belonging uniquely to the categories of perfect, abundant, or deficient based on their aliquot sum. The aliquot sum is defined as the sum of the factors of a number not including the number itself. For example, the aliquot sum of 15 is (1 + 3 + 5) = 9

  • Perfect: aliquot sum = number
    • 6 is a perfect number because (1 + 2 + 3) = 6
    • 28 is a perfect number because (1 + 2 + 4 + 7 + 14) = 28
  • Abundant: aliquot sum > number
    • 12 is an abundant number because (1 + 2 + 3 + 4 + 6) = 16
    • 24 is an abundant number because (1 + 2 + 3 + 4 + 6 + 8 + 12) = 36
  • Deficient: aliquot sum < number
    • 8 is a deficient number because (1 + 2 + 4) = 7
    • Prime numbers are deficient

Implement a way to determine whether a given number is perfect. Depending on your language track, you may also need to implement a way to determine whether a given number is abundant or deficient.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

For more detailed info about the Scala track see the help page.

Source

Taken from Chapter 2 of Functional Thinking by Neal Ford. http://shop.oreilly.com/product/0636920029687.do

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

PerfectNumbersTest.scala

import org.scalatest.{Matchers, FunSuite}

/** @version 1.1.0 */
class PerfectNumbersTest extends FunSuite with Matchers {

  test("Smallest perfect number is classified correctly") {
    PerfectNumbers.classify(6) should be(Right(NumberType.Perfect))
  }

  test("Medium perfect number is classified correctly") {
    pending
    PerfectNumbers.classify(28) should be(Right(NumberType.Perfect))
  }

  test("Large perfect number is classified correctly") {
    pending
    PerfectNumbers.classify(33550336) should be(Right(NumberType.Perfect))
  }

  test("Smallest abundant number is classified correctly") {
    pending
    PerfectNumbers.classify(12) should be(Right(NumberType.Abundant))
  }

  test("Medium abundant number is classified correctly") {
    pending
    PerfectNumbers.classify(30) should be(Right(NumberType.Abundant))
  }

  test("Large abundant number is classified correctly") {
    pending
    PerfectNumbers.classify(33550335) should be(Right(NumberType.Abundant))
  }

  test("Smallest prime deficient number is classified correctly") {
    pending
    PerfectNumbers.classify(2) should be(Right(NumberType.Deficient))
  }

  test("Smallest non-prime deficient number is classified correctly") {
    pending
    PerfectNumbers.classify(4) should be(Right(NumberType.Deficient))
  }

  test("Medium deficient number is classified correctly") {
    pending
    PerfectNumbers.classify(32) should be(Right(NumberType.Deficient))
  }

  test("Large deficient number is classified correctly") {
    pending
    PerfectNumbers.classify(33550337) should be(Right(NumberType.Deficient))
  }

  test("Edge case (no factors other than itself) is classified correctly") {
    pending
    PerfectNumbers.classify(1) should be(Right(NumberType.Deficient))
  }

  test("Zero is rejected (not a natural number)") {
    pending
    PerfectNumbers.classify(0) should be(
      Left("Classification is only possible for natural numbers."))
  }

  test("Negative integer is rejected (not a natural number)") {
    pending
    PerfectNumbers.classify(-1) should be(
      Left("Classification is only possible for natural numbers."))
  }
}
import NumberType.NumberType
import scala.collection.mutable.ListBuffer
//  09-23-19

object PerfectNumbers {
  def classify(num: Int): Either[String, NumberType] = {
    if (num < 1)
      Left("Classification is only possible for natural numbers.")
    else {
      val tot = usingBruteForce(num)
//      val tot = usingListBuffer(num)
//      val tot = usingFilterSum(num)
//      val tot = usingFoldLeft(num)

      Right(NumberType(tot compare num))
    }
  }

  // 306 ms
  def usingBruteForce(num: Int): Int = {
    var acc = 0
    for (i <- 1 to num / 2) { if (num % i == 0) acc += i }
    acc
  }

  // 473 ms
  def usingListBuffer(num: Int): Int = {
    var buf = ListBuffer[Int]()
    for (i <- 1 to num / 2) { if (num % i == 0) buf += i }
    buf.sum
  }

  // 556 ms
  def usingFilterSum(num: Int): Int = (1 to num / 2)
    .filter { num % _ == 0 }.sum

  // 681 ms
  def usingFoldLeft(num: Int): Int = (1 to num / 2)
    .foldLeft(0) { (acc, i) => if (num % i == 0) acc + i else acc }

}

object NumberType extends Enumeration {
  type NumberType = Value

  val Deficient = Value(-1)
  val Perfect   = Value(0)
  val Abundant  = Value(1)
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

Ric0chet's Reflection

Includes four solutions for benchmark comparison (all use the math shortcut (1 to n/2) ). This clearly demonstrates the overhead incurred from using higher-order functions.