Skip to content

Instantly share code, notes, and snippets.

@kazua
kazua / problem22.scala
Created November 15, 2012 11:25
Project Euler Problem 22
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2022
//K.A
import java.io._
import scala.io.Source
object problem22 {
def sum(acl : Int, s : String) : Int = s match {
case null => acl
case "" => acl
@kazua
kazua / problem06.scala
Created November 15, 2012 16:37
Project Euler Problem 06
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%206
//K.A
import scala.math._
object problem06 {
def sum(acl : Long, xs : List[Int]) : Long = xs match {
case Nil => acl
case y :: ys => sum(acl + pow(y,2).toLong, ys)
}
@kazua
kazua / problem07.scala
Created November 17, 2012 09:03
Project Euler Problem 07
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%207
//K.A
import scala.math._
object problem07 {
def getPrime(cnt : Int) : Int = {
val pr = 2 #:: Stream.from(3)
pr.filter(i=> pr.takeWhile(j=> pow(j, 2) <= i).forall(i % _ > 0))(cnt - 1)
}
@kazua
kazua / problem08.scala
Created November 17, 2012 13:26
Project Euler Problem 08
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%208
//K.A
object problem08 {
def calcSliceProduct(nums : String) : Int ={
if (!nums.forall(_.isDigit)){
println("数字でない文字がまじっています。")
0
}else{
nums.map(_.asDigit).sliding(5).map(_.product).max
@kazua
kazua / problem09.scala
Created November 17, 2012 15:49
Project Euler Problem 09
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%209
//K.A
import scala.math._
object problem09 {
def pythagoras(mc : Int) : Int = {
val pg = for (b <- 2 until mc; a <- 1 until b; c = mc - (a + b) if pow(a, 2) + pow(b, 2) == pow(c, 2)) yield a * b * c
pg.sum
}
@kazua
kazua / problem10.scala
Created November 18, 2012 05:19
Project Euler Problem 10
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2010
//K.A
import scala.math._
object problem10 {
def getSumPrime(max : Int) : Long = {
lazy val pr : Stream[Int] = 2 #:: Stream.from(3).filter(i=> pr.takeWhile(j=> pow(j, 2) <= i).forall(i % _ > 0)).takeWhile(_ < max)
pr.map(_.toLong).reduceLeft(_+_)
}
@kazua
kazua / problem13.scala
Created November 21, 2012 11:45
Project Euler Problem 13
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2013
//K.A
object problem13 {
def calcSliceSum(nums : String) : String = {
if (!nums.forall(_.isDigit)) {
"数字でない文字がまじっています。"
} else {
nums.sliding(50,50).map(_.take(11).toLong).sum.toString.take(10)
}
@kazua
kazua / problem16.scala
Created November 23, 2012 02:53
Project Euler Problem 16
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2016
//K.A
import scala.math._
object problem16 {
def getMtPNum(mp : Int) : String = {
BigInt(2).pow(mp).toString.map(_.asDigit).sum.toString
}
def main(args : Array[String]) {
@kazua
kazua / problem48.scala
Created November 23, 2012 12:15
Project Euler Problem 48
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2048
//K.A
import scala.math._
object problem48 {
def getMtpSum(mn : Int) : String = {
(1 to mn).map(i => BigInt(i).pow(i)).reduceLeft(_+_).toString.takeRight(10)
}
def main(args : Array[String]) {
@kazua
kazua / problem19.scala
Created November 24, 2012 18:48
Project Euler Problem 19
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2019
//K.A
object problem19 {
def getSunCnt : Int = {
val dayCnt = List(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
def mlf(st : Int, ed : Int) = for(y <- st to ed; m <- 1 to 12) yield if (if (m != 2 || (m == 2 && ((y % 100 == 0 && y % 400 != 0) || (y % 4 != 0)))) false else true) 29 else dayCnt(m - 1)
val ml1900 = mlf(1900,1900)
val ml = mlf(1901,2000)