Jump to content

Composite numbers k with no single digit factors whose factors are all substrings of k: Difference between revisions

Line 1,327:
</pre>
=={{header|Scala}}==
for Scala3
<syntaxhighlight lang="scala">
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).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, 2)
}
 
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")
}
</syntaxhighlight>
 
64

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.