Skip to content

Instantly share code, notes, and snippets.

@kazua
kazua / problem40.scala
Last active December 9, 2015 23:38
Project Euler Problem 40
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2040
//K.A
object problem40 {
def getIrtlNumMlp(max: Int, irtNum: Seq[Int], acl: Int): Int = max match {
case n if n < 1 => acl
case n => getIrtlNumMlp(max / 10, irtNum, acl * irtNum(n - 1))
}
def getMaxdgt(numdgt: Int, maxNumdgt: Int, maxNum: Int, acl: BigInt): BigInt = acl match {
case n if n >= maxNum => (BigInt(10).pow(numdgt - 1)-1) - ((n - maxNum)/(numdgt - 1))
@kazua
kazua / problem41.scala
Created December 23, 2012 04:56
Project Euler Problem 41
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2041
//K.A
object problem41 {
def isPrime(i : Int) = {
if (i < 2) false
else (2 until i).exists(i % _ == 0) == false
}
def getPandigitalPrime(nowNumdgt : Int, acl : Int) : Int = nowNumdgt match {
case n if n == 2 || acl != 0 => acl
@kazua
kazua / problem43.scala
Created December 23, 2012 07:10
Project Euler Problem 43
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2043
//K.A
object problem43 {
def getPandigitalSum(maxNum : Int) : BigInt = {
(0 to maxNum).permutations.map(_.mkString).filter(s =>
((s.substring(0, 1).toInt != 0) &&
(s.substring(1, 4).toInt % 2 == 0) &&
(s.substring(2, 5).toInt % 3 == 0) &&
(s.substring(3, 6).toInt % 5 == 0) &&
@kazua
kazua / problem46.scala
Last active December 10, 2015 05:29
Project Euler Problem 46
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2046
//K.A
import scala.math._
object problem46 {
def getOddCmpsNumMin() : Int = {
lazy val pr : Stream[Int] = 2 #:: Stream.from(3).filter(i => pr.takeWhile(j => BigInt(j).pow(2) <= i).forall(i % _ > 0))
val or : Stream[Int] = Stream.from(2).filter(_ % 2 != 0).filter(i => pr.takeWhile(_ <= i).forall(_ != i))
val ormin : Stream[Int] = or.dropWhile(i => pr.takeWhile(_ <= i).map(j => sqrt((i - j)/2)).forall(k => ceil(k) != floor(k)) == false)
@kazua
kazua / problem47.scala
Created December 28, 2012 12:49
Project Euler Problem 47
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2047
//K.A
import scala.math._
object problem47 {
def getConPfnFst(cnt : Int) : Int = {
lazy val pr : Stream[Int] = 2 #:: Stream.from(3).filter(i=> pr.takeWhile(j=> BigInt(j).pow(2) <= i).forall(i % _ > 0))
Stream.from(2).dropWhile(i=> (0 until cnt).map(j => pr.take(ceil(sqrt(i+j)).toInt).filter((i+j) % _ == 0).size).forall(_ == cnt)==false).take(1).min
}
@kazua
kazua / yeardiff.scala
Last active December 10, 2015 11:08
年の中に同じ数値が現れない年
//K.A
//年の中に同じ数値が現れない年のリスト
object yeardiff {
def yeardiff(maxyear : Int) : Seq[String] = {
(1 to maxyear).map(_.toString.toList).filter(a => a.distinct.size == a.size).map(_.mkString)
}
def main(args : Array[String]) {
val nowyear = 2013
yeardiff(nowyear).foreach(println)
@kazua
kazua / problem52.scala
Created January 2, 2013 13:34
Project Euler Problem 52
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2052
//K.A
object problem52 {
def getPosInt(mc : Int) : BigInt = {
Stream.from(1).takeWhile(i => (2 to mc).map(j => (j * i).toString.toList).filter(_.diff(i.toString.toList).size > 0).isEmpty == false).max + 1
}
def main(args : Array[String]) {
println(getPosInt(6))
}
@kazua
kazua / problem53.scala
Created January 3, 2013 05:26
Project Euler Problem 53
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2053
//K.A
object problem53 {
def getPascNum(lc : Int, mc : Int) : BigInt = {
(1 to mc).map(i => (1 until mc).map(j => (BigInt(1) to i).product / ((BigInt(1) to j).product * (BigInt(1) to (i - j)).product)).filter(_ > lc).size).reduceLeft(_ + _)
}
def main(args : Array[String]) {
val mCnt = 100
val lCnt = 1000000
@kazua
kazua / problem56.scala
Last active December 10, 2015 14:58
Project Euler Problem 56
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2056
//K.A
object problem56 {
def getDigSumMax(mc : Int) : Int = {
(1 until mc).map(i => (1 until mc).map(j => (BigInt(i).pow(j)).toString.toList.map(_.toString.toInt).sum)).reduceLeft(_ ++ _).max
}
def main(args : Array[String]) {
val mCnt = 100
println(getDigSumMax(mCnt))
@kazua
kazua / problem24.scala
Created January 5, 2013 07:40
Project Euler Problem 24
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2024
//K.A
object problem24 {
def getPermNum(sn : String, cnt : Int) : String = {
val bn = sn.toList.map(_.toString.toInt).filter(_ != 0).product
sn.toList.permutations.filter(_.mkString.take(1) == (cnt / bn).toString).take(cnt % bn).map(_.mkString).toList.reverse.head
}
def main(args : Array[String]) {
val cnt = 1000000