Skip to content

Instantly share code, notes, and snippets.

@kazua
kazua / problem01.scala
Created November 26, 2012 11:32
Project Euler Problem 01
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%201
//K.A
object problem01 {
def getMtp(mn : Int) : Int = {
Range(1, mn).toList.filter(i => i % 3 == 0 || i % 5 == 0).sum
}
def main(args : Array[String]) {
val mn = 1000
println(getMtp(mn))
@kazua
kazua / problem02.scala
Created November 26, 2012 12:30
Project Euler Problem 02
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%202
//K.A
object problem02 {
def fbnEvnSum(mn : Int) : BigInt = {
lazy val fbn: Stream[Int] = 0 #:: fbn.scanLeft(1)(_+_).takeWhile(_ <= mn)
fbn.filter(_ % 2 == 0).sum
}
def main(args : Array[String]) {
val mn = 4000000
@kazua
kazua / problem03.scala
Created November 26, 2012 15:02
Project Euler Problem 03
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%203
//K.A
import scala.math._
object problem03 {
def getMaxPfc(tn : Long) : Int = {
val pr = 2 #:: Stream.from(3)
pr.filter(i=> pr.takeWhile(j=> pow(j, 2) <= i).forall(i % _ > 0)).takeWhile(_ <= sqrt(tn)).filter(tn % _ == 0).max
}
@kazua
kazua / problem04.scala
Created November 27, 2012 14:11
Project Euler Problem 04
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%204
//K.A
import scala.math._
object problem04 {
def getPldNum(dig : Int) : Int = {
val minNum : Int = 1 * pow(10,(dig - 1)).toInt
val maxNum : Int = (1 * pow(10, dig)).toInt - 1
(minNum to maxNum).flatMap(i => (i to maxNum).map(_ * i)).filter(n => n.toString == n.toString.reverse).max
@kazua
kazua / problem05.scala
Created November 27, 2012 15:59
Project Euler Problem 05
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%205
//K.A
object problem05 {
def getDivMin(sl : List[Int]) : Int = {
Range(sl.last, Int.MaxValue).takeWhile(i=> !sl.forall(i % _ == 0)).max + 1
}
def main(args : Array[String]) {
val sl = (1 to 20).toList
println(getDivMin(sl))
@kazua
kazua / problem50.scala
Created November 28, 2012 09:53
Project Euler Problem 50
//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) {
@kazua
kazua / problem23.scala
Created December 4, 2012 10:53
Project Euler Problem 23
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2023
//K.A
object problem23 {
def getNoAbdNum(mn : Int) : Int = {
val an = (0 to mn).map(i => Range(1, i).filter(i % _ == 0).sum).zipWithIndex.filter(j => j._1 > j._2).map(_._2)
val on = an.flatMap(i => an.takeWhile(_ <= mn).map(_ + i))
(1 to mn).diff(on).sum
}
@kazua
kazua / problem12.scala
Created December 4, 2012 12:44
Project Euler Problem 12
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2012
//K.A
import scala.math._
object problem12 {
def getTrgFst(mn : Int) : Int = {
lazy val tn: Stream[Int] = 1 #:: Stream.from(2).scanLeft(1)(_+_)
def divCnt(tn: Int) = (0 to Int.MaxValue -1).takeWhile(i => pow(i, 2) <= tn).reduceLeft((l, r) => if(tn % r == 0) l + 2 else l)
@kazua
kazua / problem15.scala
Created December 10, 2012 12:46
Project Euler Problem 15
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2015
//K.A
object problem15 {
def getRootCnt(n: Int, k: Int): BigInt = {
(BigInt(1) to n).product/(BigInt(1) to k).product.pow(2)
}
def main(args : Array[String]) {
val mCnt = 20
println(getRootCnt(mCnt*2, mCnt))
@kazua
kazua / problem29.scala
Created December 13, 2012 18:38
Project Euler Problem 29
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2029
//K.A
object problem29 {
def getDtntSize(min: Int, max: Int): Int = {
(min to max).map(i => (BigInt(min) to max).map(j=> j.pow(i))).reduceLeft(_ ++ _).distinct.size
}
def main(args : Array[String]) {
val min = 2
val max = 100