Last active
December 19, 2015 00:09
-
-
Save kazua/5867072 to your computer and use it in GitHub Desktop.
Project Euler Problem 44
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%2044 | |
| //write kazua | |
| import scala.math._ | |
| object problem44 { | |
| def isPenNum(d : Double) = (sqrt(1.0 + 24.0 * d) + 1.0) / 6.0 == ((sqrt(1.0 + 24.0 * d) + 1.0) / 6.0).toInt | |
| def problem44(fn : Int, sn : Int, an : Int) : Int = an match { | |
| case a if a > 0 => a | |
| case _ => { | |
| val b = fn * (3 * fn - 1) / 2 | |
| val s = sn * (3 * sn - 1) / 2 | |
| val bs = isPenNum(b - s) | |
| val ba = isPenNum(b + s) | |
| if (bs && ba && sn > 0) problem44(fn, sn, b - s) | |
| else if (!(bs && ba) && sn > 0) problem44(fn, sn - 1, 0) | |
| else problem44(fn + 1, fn, 0) | |
| } | |
| } | |
| def main(args : Array[String]) { | |
| println(problem44(1, 0, 0)) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment