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://projecteuler.net/problem=14 | |
* http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2014 | |
*/ | |
object collatz { | |
def apply(i: Int) = calc(i :: Nil).reverse | |
private def calc(xs: List[Long]): List[Long] = { |
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://projecteuler.net/problem=13 | |
* http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2013 | |
*/ | |
val xs = List( | |
BigInt("37107287533902102798797998220837590246510135740250"), | |
BigInt("46376937677490009712648124896970078050417018260538"), | |
BigInt("74324986199524741059474233309513058123726617309629"), | |
BigInt("91942213363574161572522430563301811072406154908250"), |
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://projecteuler.net/problem=12 | |
* http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2012 | |
*/ | |
def numbers(n: Long): Stream[Long] = n #:: numbers(n + 1L) | |
val triangle = numbers(2L).scanLeft(1L){(a,b) => a + b } | |
val divisors = (n: Long) => { |
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://projecteuler.net/problem=11 | |
* http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2011 | |
*/ | |
val xs: List[List[Int]] = List( | |
List(8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8), | |
List(49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0), | |
List(81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65), | |
List(52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91), |
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://projecteuler.net/problem=10 | |
* http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2010 | |
*/ | |
def isPrime(n: Long) = Iterator.from(2).takeWhile(p => p * p <= n).forall(p => n % p != 0) | |
def xs = 2L until 2000000L | |
val ret = xs filter isPrime sum |
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://projecteuler.net/problem=9 | |
* http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%209 | |
*/ | |
def triplesOfTotal1000: List[(Int, Int, Int)] = { | |
def search(a: Int, b: Int, c: Int, xs: List[(Int, Int, Int)]): List[(Int, Int, Int)] = { | |
val ys = if (a + b + c == 1000) (a, b, c) :: xs else xs |
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://projecteuler.net/problem=8 | |
* http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%208 | |
*/ | |
val s = "7316717 ... 63450" // 途中省略してます. | |
val xs = s.toList.map(_.toString.toInt) | |
def product(xs: List[Int], ys: List[Int]): List[Int] = { | |
xs match { |
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://projecteuler.net/problem=7 | |
* http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%207 | |
*/ | |
val isPrime = (n: Int) => Iterator.from(2).takeWhile(p => p * p <= n).forall(p => n % p != 0) | |
def xs(n: Int): Stream[Int] = n #:: xs(n + 1) | |
val ret = xs(2).filter(isPrime).take(10001).last | |
println(ret) |
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://projecteuler.net/problem=6 | |
* http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%206 | |
*/ | |
val xs = 1 to 100 | |
import scala.math.pow | |
val ret = pow(xs.sum, 2).toInt - xs.map(pow(_, 2)).sum.toInt | |
println(ret) |
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://projecteuler.net/problem=5 | |
* http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%205 | |
*/ | |
def findAnswer(n: Int): Int = { | |
val l = List.range(1, 21, + 1) | |
if (l.forall(n % _ == 0)) { | |
n | |
} else { | |
findAnswer(n + 1) |