How to resolve the algorithm Largest number divisible by its digits step by step in the Scala programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Largest number divisible by its digits step by step in the Scala programming language

Table of Contents

Problem Statement

Find the largest base 10 integer whose digits are all different,   and   is evenly divisible by each of its individual digits.

These numbers are also known as   Lynch-Bell numbers,   numbers   n   such that the (base ten) digits are all different (and do not include zero)   and   n   is divisible by each of its individual digits.

135   is evenly divisible by   1,   3,   and   5.

Note that the digit zero (0) can not be in the number as integer division by zero is undefined. The digits must all be unique so a base ten number will have at most 9 digits. Feel free to use analytics and clever algorithms to reduce the search space your example needs to visit, but it must do an actual search. (Don't just feed it the answer and verify it is correct.)

Do the same thing for hexadecimal.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Largest number divisible by its digits step by step in the Scala programming language

Source code in the scala programming language

import scala.collection.mutable

def largestDecimal: Int = Iterator.from(98764321, -1).filter(chkDec).next
def chkDec(num: Int): Boolean = {
  val set = mutable.HashSet[Int]()
  num.toString.toVector.map(_.asDigit).forall(d => (d != 0) && (num%d == 0) && set.add(d))
}


import spire.math.SafeLong
import spire.implicits._

object LargestNumDivisibleByDigits {
  def main(args: Array[String]): Unit = {
    for(b <- Seq(10, 16)){
      val tStart = System.currentTimeMillis
      val res = getLargestNum(b).toBigInt.toString(b)
      val tDur = System.currentTimeMillis - tStart
      println(s"Base $b: $res [${tDur}ms]")
    }
  }
  
  def getLargestNum(base: SafeLong): SafeLong = {
    def chkNum(digits: Vector[SafeLong])(num: SafeLong): Boolean = {
      val lst = LazyList.iterate((num%base, num/base)){case (_, src) => (src%base, src/base)}.take(digits.length).map(_._1)
      lst.diff(digits).isEmpty
    }
    
    def chkChunk(combo: Vector[SafeLong]): Option[SafeLong] = {
      val lcm = combo.reduce(_.lcm(_))
      val ulim = combo.zipWithIndex.map{case (n, i) => n*(base ** i)}.reduce(_+_)
      Iterator.iterate(ulim - (ulim%lcm))(_ - lcm).takeWhile(_ > 0).find(chkNum(combo))
    }
  
    val baseDigits: Vector[SafeLong] = Vector.range(1, base.toInt).map(SafeLong(_))
    def chkBlock(digits: Iterator[Vector[SafeLong]]): Option[SafeLong] = digits.map(chkChunk).collect{case Some(n) => n}.maxOption
    Iterator.from(base.toInt - 1, -1).map(len => chkBlock(baseDigits.combinations(len))).collect{case Some(n) => n}.next
  }
}


  

You may also check:How to resolve the algorithm Sort numbers lexicographically step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Sorting algorithms/Shell sort step by step in the Io programming language
You may also check:How to resolve the algorithm Odd word problem step by step in the Lua programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the min programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean/Calculate Pi step by step in the Mathematica/Wolfram Language programming language