Last active
December 12, 2015 02:18
-
-
Save zerosum/4697216 to your computer and use it in GitHub Desktop.
Project Euler: Problem 28
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
| package euler.zerosum | |
| object Euler0028 { | |
| def main(args: Array[String]) { | |
| println(getCorner(3, 1001, from(1).take(1)).sum) | |
| } | |
| private def getCorner(width: Int, maxWidth: Int, corners: Stream[Int]): Stream[Int] = { | |
| val maxValue: Int = maxWidth * maxWidth | |
| if (corners.last > maxValue) { | |
| corners.takeWhile(_ <= maxValue) | |
| } else { | |
| val nextCorners = from((width - 2) * (width - 2) + 1) | |
| .takeWhile(_ <= width * width) | |
| .filter(_ % (width - 1) == 1) | |
| getCorner(width + 2, maxWidth, corners #::: nextCorners) | |
| } | |
| } | |
| private def from(n: Int): Stream[Int] = n #:: from(n + 1) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment