How to resolve the algorithm Abundant odd numbers step by step in the Scala programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Abundant odd numbers step by step in the Scala programming language

Table of Contents

Problem Statement

An Abundant number is a number n for which the   sum of divisors   σ(n) > 2n, or,   equivalently,   the   sum of proper divisors   (or aliquot sum)       s(n) > n.

12   is abundant, it has the proper divisors     1,2,3,4 & 6     which sum to   16   ( > 12 or n);        or alternately,   has the sigma sum of   1,2,3,4,6 & 12   which sum to   28   ( > 24 or 2n).

Abundant numbers are common, though even abundant numbers seem to be much more common than odd abundant numbers. To make things more interesting, this task is specifically about finding   odd abundant numbers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Abundant odd numbers step by step in the Scala programming language

Source code in the scala programming language

import scala.collection.mutable.ListBuffer

object Abundant {
  def divisors(n: Int): ListBuffer[Int] = {
    val divs = new ListBuffer[Int]
    divs.append(1)

    val divs2 = new ListBuffer[Int]
    var i = 2

    while (i * i <= n) {
      if (n % i == 0) {
        val j = n / i
        divs.append(i)
        if (i != j) {
          divs2.append(j)
        }
      }
      i += 1
    }

    divs.appendAll(divs2.reverse)
    divs
  }

  def abundantOdd(searchFrom: Int, countFrom: Int, countTo: Int, printOne: Boolean): Int = {
    var count = countFrom
    var n = searchFrom
    while (count < countTo) {
      val divs = divisors(n)
      val tot = divs.sum
      if (tot > n) {
        count += 1
        if (!printOne || !(count < countTo)) {
          val s = divs.map(a => a.toString).mkString(" + ")
          if (printOne) {
            printf("%d < %s = %d\n", n, s, tot)
          } else {
            printf("%2d. %5d < %s = %d\n", count, n, s, tot)
          }
        }
      }
      n += 2
    }

    n
  }

  def main(args: Array[String]): Unit = {
    val max = 25
    printf("The first %d abundant odd numbers are:\n", max)
    val n = abundantOdd(1, 0, max, printOne = false)

    printf("\nThe one thousandth abundant odd number is:\n")
    abundantOdd(n, 25, 1000, printOne = true)

    printf("\nThe first abundant odd number above one billion is:\n")
    abundantOdd((1e9 + 1).intValue(), 0, 1, printOne = true)
  }
}


  

You may also check:How to resolve the algorithm Prime conspiracy step by step in the Sidef programming language
You may also check:How to resolve the algorithm Sequence: nth number with exactly n divisors step by step in the Ring programming language
You may also check:How to resolve the algorithm Sieve of Pritchard step by step in the Wren programming language
You may also check:How to resolve the algorithm Sorting algorithms/Counting sort step by step in the Phix programming language
You may also check:How to resolve the algorithm Hello world/Newbie step by step in the X86-64 Assembly programming language