How to resolve the algorithm Composite numbers k with no single digit factors whose factors are all substrings of k step by step in the Scala programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Composite numbers k with no single digit factors whose factors are all substrings of k step by step in the Scala programming language
Table of Contents
Problem Statement
Find the composite numbers k in base 10, that have no single digit prime factors and whose prime factors are all a substring of k.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Composite numbers k with no single digit factors whose factors are all substrings of k step by step in the Scala programming language
Source code in the scala programming language
def isComposite(num: Int): Boolean = {
val numStr = num.toString
def iter(n: Int, start: Int): Boolean = {
val limit = math.sqrt(n).floor.toInt
(start to limit by 2).dropWhile(n % _ > 0).headOption match {
case Some(v) if v < 10 => false
case Some(v) =>
if (v == start || numStr.contains(v.toString)) iter(n / v, v)
else false
case None => n < num && numStr.contains(n.toString)
}
}
iter(num, 3)
}
def composites = Iterator.from(121, 2).filter(isComposite(_))
@main def main = {
val start = System.currentTimeMillis
composites.take(20)
.grouped(10)
.foreach(grp => println(grp.map("%8d".format(_)).mkString(" ")))
val time = System.currentTimeMillis - start
println(s"time elapsed: $time ms")
}
You may also check:How to resolve the algorithm Singly-linked list/Element insertion step by step in the Fortran programming language
You may also check:How to resolve the algorithm Search in paragraph's text step by step in the Nim programming language
You may also check:How to resolve the algorithm Zig-zag matrix step by step in the Tcl programming language
You may also check:How to resolve the algorithm Morse code step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Loops/While step by step in the Trith programming language