Last active
January 28, 2017 17:13
-
-
Save Raffaello/48f76abfb3122a88a1d13c0fc2bbc98a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
https://curiosity.com/topics/can-you-solve-the-abcde-math-puzzle-curiosity/?utm_source=androidapp