Created
November 28, 2012 09:53
-
-
Save kazua/4160206 to your computer and use it in GitHub Desktop.
Project Euler Problem 50
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%2050 | |
| //K.A | |
| import scala.math._ | |
| object problem50 { | |
| def getPrimeSumMax(mn : Long) : Long = { | |
| lazy val pr : Stream[Int] = 2 #:: Stream.from(3).filter(i => pr.takeWhile(j => pow(j,2) <= i).forall(i % _ > 0)).takeWhile(_ <= mn) | |
| val pr2 = pr.takeWhile(i => pr.takeWhile(_ <= i).map(_.toLong).sum <= mn) | |
| for (i <- pr2.length to 1 by -1) { | |
| if (pr2.sliding(i).map(_.sum).filter(j => pr.exists(_ == j)).hasNext) return pr2.sliding(i).map(_.sum).filter(j => pr.exists(_ == j)).max | |
| } | |
| 0 | |
| } | |
| def main(args : Array[String]) { | |
| val mn = 1000000 | |
| println(getPrimeSumMax(mn)) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment