Skip to content

Instantly share code, notes, and snippets.

@kazua
kazua / problem35.scala
Created January 17, 2013 12:51
Project Euler Problem 35
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2035
//K.A
object problem35 {
lazy val pr : Stream[Int] = 2 #:: Stream.from(3).filter(i => pr.takeWhile(j => BigInt(j).pow(2) <= i).forall(i % _ > 0)).takeWhile(_ <= 1000000)
def isPrime(i : Int) = {
if (i < 2) false
else (2 until i).exists(i % _ == 0) == false
}
def problem35 = pr.filter(i => (List(0, 2, 4, 5, 6, 8).forall(j => i.toString.indexOf(j.toString) == -1) || i.toString.length == 1) && (1 until i.toString.length).map(n => i.toString.drop(n) + i.toString.take(n)).forall(k => isPrime(k.toInt) == true)).size
@kazua
kazua / problem38.scala
Created January 17, 2013 12:52
Project Euler Problem 38
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2038
//K.A
object problem38 {
def problem38 = (1 to 9999).map(i => (1 to 9).map(j => (1 to j).map(k => i * k).mkString)).flatten.filter(n => n.length == 9 && (1 to 9).map(m => n.indexOf(m.toString)).forall(_ >= 0)).max
def main(args : Array[String]) {
println(problem38)
}
}
@kazua
kazua / problem36.scala
Last active December 11, 2015 07:08
Project Euler Problem 36
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2036
//K.A
object problem36 {
def problem36 = (1 until 1000000).filter(i => i.toString == i.toString.reverse && i.toBinaryString == i.toBinaryString.reverse).sum
def main(args : Array[String]) {
println(problem36)
}
}
@kazua
kazua / problem39.scala
Created January 20, 2013 04:13
Project Euler Problem 39
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2039
//K.A
object problem39 {
def problem39 = (3 to 1000).map(i => for (b <- 2 until i; a <- 1 until b; c = i - a - b if a * a + b * b == c * c && c > 0) yield List(i, a, b, c)).filter(_ != Nil).map(n => List(n.head.head, n.size)).sortBy(s => s.last).last.head
def main(args : Array[String]) {
println(problem39)
}
}
@kazua
kazua / problem63.scala
Created January 21, 2013 12:09
Project Euler Problem 63
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2063
//K.A
import scala.math._
object problem63 {
def problem63 = (1 until 10).map(i => (log(10D) / log(10D / i)).toInt).sum
def main(args : Array[String]) {
println(problem63)
}
@kazua
kazua / problem55.scala
Created January 21, 2013 13:13
Project Euler Problem 55
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2055
//K.A
object problem55 {
def bolLychrel(nm : BigInt, acl : Int) : Boolean = nm match {
case a if (a + BigInt(a.toString.reverse)).toString == (a + BigInt(a.toString.reverse)).toString.reverse => false
case a => if (acl < 50) bolLychrel(a + BigInt(a.toString.reverse), acl + 1) else true
}
def problem55 = (1 until 10000).map(i => bolLychrel(i, 0)).filter(_ == true).size
def main(args : Array[String]) {
@kazua
kazua / problem17.scala
Created January 22, 2013 13:37
Project Euler Problem 17
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2017
//K.A
object problem17 {
val u20 = List(0, 3, 3, 5, 4, 4, 3, 5, 5, 4, 3, 6, 6, 8, 8, 7, 7, 9, 8, 8)
val t10 = List(0, 0, 6, 6, 5, 5, 5, 7, 6, 6)
def calcChrLen(nm : Int, acl : Int) : Int = nm match {
case a if a < 20 => acl + (if (acl > 0 && a > 0) 3 else 0) + u20(a)
case a if a < 100 => calcChrLen(a % 10, acl + t10(a / 10))
case a if a < 1000 => calcChrLen(a % 100, acl + u20(a / 100) + 7)
@kazua
kazua / problem27.scala
Created January 23, 2013 12:03
Project Euler Problem 27
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2027
//K.A
import scala.collection.mutable._
import scala.math._
object problem27 {
lazy val pr : Stream[Int] = 2 #:: Stream.from(3).filter(i => pr.takeWhile(j => pow(j, 2) <= i).forall(i % _ > 0)).takeWhile(_ < 1000)
val memo = Map[Int, Boolean]()
def isPrime(n : Int) = if (n < 2)
@kazua
kazua / problem97.scala
Created January 23, 2013 15:33
Project Euler Problem 97
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2097
//K.A
object problem97 {
def problem97 = (28433 * (BigInt(1) << 7830457) + 1) % BigInt(10).pow(10)
def main(args : Array[String]) {
println(problem97)
}
}
@kazua
kazua / perfectnum.scala
Created January 26, 2013 11:37
完全数リスト生成
import scala.math._
object perfectnum {
def getPfnList(mn : Int) : Seq[BigInt] = {
lazy val pr : Stream[Int] = 2 #:: Stream.from(3).filter(i => pr.takeWhile(j => pow(j, 2) <= i).forall(i % _ > 0)).takeWhile(_ <= sqrt(mn * 2))
(1 to sqrt(mn * 2).toInt).map(i => (BigInt(1) << i) - 1).filter(pr.contains).map(j => j * (j + 1) / 2)
}
def main(args : Array[String]) {
val mn = 100000000