Created
September 18, 2012 06:36
-
-
Save pokutuna/3741595 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 isPrime(n: Int): Boolean = (2 to math.sqrt(n).toInt).forall(n % _ != 0) | |
def nextCorner(st: Stream[Int]) = st.zip(st.tail).map( n => n._2 + (n._2 - n._1 + 8) ) | |
val upperRight: Stream[Int] = 1 #:: 3 #:: nextCorner(upperRight) | |
val upperLeft: Stream[Int] = 1 #:: 5 #:: nextCorner(upperLeft) | |
val lowerLeft: Stream[Int] = 1 #:: 7 #:: nextCorner(lowerLeft) | |
val lowerRight: Stream[Int] = 1 #:: 9 #:: nextCorner(lowerRight) | |
val zipped: Stream[List[Int]] = Stream.from(1).map( idx => | |
List(upperRight(idx), upperLeft(idx), lowerLeft(idx), lowerRight(idx)) ) | |
@annotation.tailrec | |
def calc58(numSize: Int, primeSize: Int, zipped: Stream[List[Int]]): Int = { | |
val (ns, ps) = (numSize + 4, primeSize + zipped.head.count(isPrime)) | |
if ( (ps.toDouble / ns) < 0.1 ) return math.sqrt(zipped.head(3)).toInt | |
calc58(ns, ps, zipped.tail) | |
} | |
println(calc58(1, 0, zipped)) // => 226241 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment