Created
December 23, 2012 04:56
-
-
Save kazua/4362034 to your computer and use it in GitHub Desktop.
Project Euler Problem 41
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
| //http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2041 | |
| //K.A | |
| object problem41 { | |
| def isPrime(i : Int) = { | |
| if (i < 2) false | |
| else (2 until i).exists(i % _ == 0) == false | |
| } | |
| def getPandigitalPrime(nowNumdgt : Int, acl : Int) : Int = nowNumdgt match { | |
| case n if n == 2 || acl != 0 => acl | |
| case n if (1 to n).reduceLeft(_ + _) % 3 != 0 => getPandigitalPrime(nowNumdgt - 1, (1 to n).permutations.toList.map(_.mkString.toInt).reverse.dropWhile(!isPrime(_)).max) | |
| case n if (1 to n).reduceLeft(_ + _) % 3 == 0 => getPandigitalPrime(nowNumdgt - 1, acl) | |
| } | |
| def main(args : Array[String]) { | |
| println(getPandigitalPrime(9, 0)) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment