Skip to content

Instantly share code, notes, and snippets.

@kazua
kazua / problem25.scala
Created January 6, 2013 05:21
Project Euler Problem 25
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2025
//K.A
object problem025 {
def getFbnDgtFst(dc : Int) : BigInt = {
lazy val fbn: Stream[BigInt] = BigInt(0) #:: fbn.scanLeft(BigInt(1))(_+_).takeWhile(_.toString.length < dc)
fbn.size
}
def main(args : Array[String]) {
val dc = 1000
@kazua
kazua / problem26.scala
Created January 9, 2013 14:42
Project Euler Problem 26
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2026
//K.A
object problem026 {
def getRecDecMax(mn : Int) : Int = {
val rd = (2 until mn).map(i => (1 until mn).dropWhile(BigInt(10).modPow(_, i) != 1)).map(k => k match { case a if a.isEmpty => 0 case a => a.min })
rd.indexOf(rd.max) + 2
}
def main(args : Array[String]) {
val mn = 1000
@kazua
kazua / problem18.scala
Created January 9, 2013 17:41
Project Euler Problem 18
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2018
//K.A
object problem18 {
val triangle = List(List(75),
List(95, 64),
List(17, 47, 82),
List(18, 35, 87, 10),
List(20, 04, 82, 47, 65),
List(19, 01, 23, 75, 03, 34),
@kazua
kazua / problem67.scala
Last active December 10, 2015 23:48
Project Euler Problem 67
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2067
//K.A
object problem67 {
val triangle = List(
List("59"),
List("73", "41"),
List("52", "40", "09"),
List("26", "53", "06", "34"),
List("10", "51", "87", "86", "81"),
@kazua
kazua / problem30.scala
Created January 12, 2013 17:24
Project Euler Problem 30
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2030
//K.A
object problem30 {
def getMpNumEqNum(dc : Int) : Int = {
val mc = (BigInt(10).pow((Stream.from(1).dropWhile(s => BigInt(10).pow(s) - 1 <= (BigInt(10).pow(s) - 1).toString.map(t => BigInt(t.toString).pow(dc)).sum)).take(1).min) - 1).toInt
(2 to mc).filter(i => i.toString.map(j => BigInt(j.toString.toInt).pow(dc)).sum == i).sum
}
def main(args : Array[String]) {
val dc = 5
@kazua
kazua / problem11.scala
Created January 13, 2013 12:51
Project Euler Problem 11
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2011
//K.A
object problem11 {
val square = List(
List("08", "02", "22", "97", "38", "15", "00", "40", "00", "75", "04", "05", "07", "78", "52", "12", "50", "77", "91", "08"),
List("49", "49", "99", "40", "17", "81", "18", "57", "60", "87", "17", "40", "98", "43", "69", "48", "04", "56", "62", "00"),
List("81", "49", "31", "73", "55", "79", "14", "29", "93", "71", "40", "67", "53", "88", "30", "03", "49", "13", "36", "65"),
List("52", "70", "95", "23", "04", "60", "11", "42", "69", "24", "68", "56", "01", "32", "56", "71", "37", "02", "36", "91"),
List("22", "31", "16", "71", "51", "67", "63", "89", "41", "92", "36", "54", "22", "40", "40", "28", "66", "33", "13", "80"),
@kazua
kazua / problem14.scala
Created January 13, 2013 19:17
Project Euler Problem 14
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2014
//K.A
object problem14 {
def getCollatzDgt(nm : Long, acl : Int) : Int = nm match {
case a if a == 1 => acl + 1
case a => {
val nn = if (nm % 2 == 0) nm / 2 else (3 * nm) + 1
getCollatzDgt(nn, acl + 1)
}
@kazua
kazua / problem34.scala
Last active December 11, 2015 01:59
Project Euler Problem 34
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2034
//K.A
object problem34 {
def getLimited(dc : Int) : Int = dc match {
case a if ((1 to 9).product * a < (BigInt(10).pow(a + 1) - 1)) => (BigInt(10).pow(a + 1)).toInt - 1
case a => getLimited(dc + 1)
}
def getFactSumEq() : Int = {
(0 to getLimited(1)).map(_.toString.toList).map(_.map(c => (1 to c.asDigit).product).sum).zipWithIndex.filter(t => t._1 == t._2).map(_._1).sum - 3
@kazua
kazua / problem32.scala
Created January 15, 2013 11:36
Project Euler Problem 32
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2032
//K.A
object problem32 {
def problem32 = (1 to 99).map(i => if (i < 10) (1000 to 9999).map(j => List(i, j, (i * j)))
else (100 to 999).map(j => List(i, j, (i * j)))
).flatten.filter(_.mkString.length == 9)
.filter(k => (1 to 9).map(m => k.mkString.indexOf(m.toString)).forall(n => n >= 0))
.map(_.last).distinct.sum
def main(args : Array[String]) {
@kazua
kazua / problem37.scala
Created January 16, 2013 15:55
Project Euler Problem 37
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2037
//K.A
object problem37 {
def isPrime(i : Int) = {
if (i < 2) false
else (2 until i).exists(i % _ == 0) == false
}
def getRPrime(pl : List[Int], acl : List[Int]) : List[Int] = pl match {
case a if a.size == 0 => acl.reverse