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

Published on 12 May 2024 09:40 PM

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

Table of Contents

Problem Statement

A Disarium number is an integer where the sum of each digit raised to the power of its position in the number, is equal to the number.

135 is a Disarium number: 11 + 32 + 53 == 1 + 9 + 125 == 135 There are a finite number of Disarium numbers.

Let's start with the solution:

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

Source code in the scala programming language

object Disarium extends App {
  def power(base: Int, exp: Int): Int = {
    var result = 1
    for (i <- 1 to exp) {
      result *= base
    }
    return result
  }
  def is_disarium(num: Int): Boolean = {
    val digits = num.toString.split("")
    var sum = 0
    for (i <- 0 to (digits.size - 1)) {
      sum += power(digits(i).toInt, i + 1)
    }
    return num == sum
  }

  var i = 0
  var count = 0
  while (count < 19) {
    if (is_disarium(i)) {
      count += 1
      printf("%d ", i)
    }
    i += 1
  }
  println("")
}


  

You may also check:How to resolve the algorithm Hello world/Graphical step by step in the OxygenBasic programming language
You may also check:How to resolve the algorithm Brownian tree step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Topswops step by step in the Potion programming language
You may also check:How to resolve the algorithm Extreme floating point values step by step in the bc programming language
You may also check:How to resolve the algorithm Include a file step by step in the Dragon programming language