How to resolve the algorithm Executable library step by step in the Scala programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Executable library step by step in the Scala programming language

Table of Contents

Problem Statement

The general idea behind an executable library is to create a library that when used as a library does one thing; but has the ability to be run directly via command line. Thus the API comes with a CLI in the very same source code file. Task detail Notes:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Executable library step by step in the Scala programming language

Source code in the scala programming language

object HailstoneSequence extends App { // Show it all, default number is 27.
  def hailstone(n: Int): LazyList[Int] =
    n #:: (if (n == 1) LazyList.empty else hailstone(if (n % 2 == 0) n / 2 else n * 3 + 1))

  Hailstone.details(args.headOption.map(_.toInt).getOrElse(27))
  HailTest.main(Array())
}

object Hailstone extends App { // Compute a given or default number to Hailstone sequence
  def details(nr: Int): Unit = {
    val collatz = HailstoneSequence.hailstone(nr)

    println(s"Use the routine to show that the hailstone sequence for the number: $nr.")
    println(collatz.toList)
    println(s"It has ${collatz.length} elements.")
  }

  details(args.headOption.map(_.toInt).getOrElse(27))
}

object HailTest extends App { // Compute only the < 100000 test
  println(
    "Compute the number < 100,000, which has the longest hailstone sequence with that sequence's length.")
  val (n, len) = (1 until 100000).map(n => (n, HailstoneSequence.hailstone(n).length)).maxBy(_._2)
  println(s"Longest hailstone sequence length= $len occurring with number $n.")
}


  

You may also check:How to resolve the algorithm Simple windowed application step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Textonyms step by step in the Racket programming language
You may also check:How to resolve the algorithm UTF-8 encode and decode step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Huffman coding step by step in the Nim programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the Phix programming language