Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
17
Task/Truncatable-primes/Scala/truncatable-primes.scala
Normal file
17
Task/Truncatable-primes/Scala/truncatable-primes.scala
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
object TruncatablePrimes {
|
||||
def main(args: Array[String]): Unit = {
|
||||
val max = 1000000
|
||||
|
||||
println(
|
||||
s"""|ltPrime: ${ltPrimes.takeWhile(_ <= max).last}
|
||||
|rtPrime: ${rtPrimes.takeWhile(_ <= max).last}
|
||||
|""".stripMargin)
|
||||
}
|
||||
|
||||
def ltPrimes: LazyList[Int] = 2 #:: LazyList.from(3, 2).filter(isLeftTruncPrime)
|
||||
def rtPrimes: LazyList[Int] = 2 #:: LazyList.from(3, 2).filter(isRightTruncPrime)
|
||||
|
||||
def isPrime(num: Int): Boolean = (num > 1) && !LazyList.range(3, math.sqrt(num).toInt + 1, 2).exists(num%_ == 0)
|
||||
def isLeftTruncPrime(num: Int): Boolean = !num.toString.contains('0') && Iterator.unfold(num.toString){str => if(str.nonEmpty) Some((str.toInt, str.tail)) else None}.forall(isPrime)
|
||||
def isRightTruncPrime(num: Int): Boolean = !num.toString.exists(_.asDigit%2 == 0) && Iterator.unfold(num.toString){str => if(str.nonEmpty) Some((str.toInt, str.init)) else None}.forall(isPrime)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue