Skip to content

Instantly share code, notes, and snippets.

@Raffaello
Last active January 28, 2017 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raffaello/48f76abfb3122a88a1d13c0fc2bbc98a to your computer and use it in GitHub Desktop.
Save Raffaello/48f76abfb3122a88a1d13c0fc2bbc98a to your computer and use it in GitHub Desktop.
def reverseInt(x: Int): Int = {
x.toString.reverse.toInt
}
def palindromeNumberEquation(x:Int, y:Int): Boolean = {
reverseInt(x) == x*y
}
def differentDigitsInt(x: Int, y: Int): Boolean = {
x.toString.groupBy(c => c).map(m => m._2.length).sum == y
}
def puzzle(x: Int, digits:Int, factor:Int): Boolean = {
differentDigitsInt(x, digits) match {
case true => palindromeNumberEquation(x, factor)
case _ => false
}
}
def findFirstNumberSolvingEquation(x:Int, digits:Int, factor: Int): Boolean = {
puzzle(x, digits, factor) match {
case true => println("Number x= " + x + " * " + factor + " = " + reverseInt(x))
true
case false => findFirstNumberSolvingEquation(x+1, digits, factor)
}
}
findFirstNumberSolvingEquation(10000, 5, 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment