Created
February 21, 2013 12:18
-
-
Save zerosum/5004369 to your computer and use it in GitHub Desktop.
Project Euler: Problem 46
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 | |
| import euler.zerosum.Prime._ | |
| import euler.zerosum.Number._ | |
| import euler.zerosum.Divisors._ | |
| object Euler0046 { | |
| private val oddComposites = from(2).filterNot(n => n % 2 == 0 || primes(1, n).contains(n)) | |
| def main(args: Array[String]) { | |
| println(oddComposites.dropWhile(isGoldbachsConjecture(_)).head) | |
| } | |
| private def isGoldbachsConjecture(n: Int): Boolean = { | |
| val rs = from(1).take(n / 2) | |
| val twiceSquares = rs.map(r => 2 * r * r).filter(_ < n) | |
| !twiceSquares.filter(s => isPrime(n - s)).isEmpty | |
| } | |
| private def isPrime(n: Int): Boolean = n != 1 && getDivisors(n).filterNot(_ == 1).isEmpty | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment