Skip to content

Instantly share code, notes, and snippets.

@zerosum
Last active December 12, 2015 02:18
Show Gist options
  • Select an option

  • Save zerosum/4697216 to your computer and use it in GitHub Desktop.

Select an option

Save zerosum/4697216 to your computer and use it in GitHub Desktop.
Project Euler: Problem 28
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