Skip to content

Instantly share code, notes, and snippets.

@kazua
kazua / worldproblem1_1.scala
Last active December 15, 2015 04:50
世界で闘うプログラマ本の1-1
//write kazua
import scala.collection.mutable._
object worldproblem1_1 {
def UniqueCheck(chkstr : String) : Boolean = {
if (chkstr.length == 0 && chkstr.length > 256) return false
val chrmp = Map[Char, Boolean]()
@kazua
kazua / problem28.scala
Created March 7, 2013 13:47
Project Euler Problem 28
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2028
//K.A
object problem28 {
def CornerNumSum(len : Int, acl : List[Int]) : List[Int] = len match {
case i if i <= 1 =>
acl ::: List(i)
case l =>
val mn = math.pow(l, 2).toInt
val cn = l - 1
@kazua
kazua / kakeiboPDFPrint.scala
Last active December 13, 2015 18:08
自作家計簿データのPDF出力用バッチ
import java.sql._
import java.io._
import scala.Array
import com.itextpdf.text._
import com.itextpdf.text.pdf._
import com.itextpdf.text.pdf.fonts._
import com.itextpdf.text.pdf.BaseFont._
object kakeiboPDFPrint {
def main(args : Array[String]) {
@kazua
kazua / kakeiboMonthBackup.scala
Last active December 12, 2015 12:19
自作家計簿データのバックアップバッチのscala化
import java.sql._
import java.io._
import scala.Array
object kakeiboMonthBackup {
def using[A <: { def close() : Unit }, B](closable : A)(f : A => B) : B = try { f(closable) } finally { closable.close() }
def main(args : Array[String]) {
try {
val year = args(0)
val month = args(1)
@kazua
kazua / BetrothedNum.scala
Created February 2, 2013 18:27
婚約数リスト生成
//K.A
object BetrothedNum {
def getBetrothedNum(min : Int, max : Int) = (min to max).map(i => List(i, (2 until i).filter(i % _ == 0).sum)).map(j => List(j.head, j.tail.head, (2 until j.tail.head).filter(j.tail.head % _ == 0).sum)).filter(k => k.head == k.tail.tail.head).map(k => List(k.head, k.tail.head)).filter(n => n.head < n.tail.head)
def main(args : Array[String]) {
val min = 1
val max = 10000
getBetrothedNum(min, max).foreach(println)
}
}
@kazua
kazua / semiperfectnum.scala
Created January 29, 2013 15:08
疑似完全数リスト生成
import scala.math._
object semiperfectnum {
def getSemiPfnList(mn : Int) : Seq[Int] = {
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 => pr.filter(_ % 2 != 0).takeWhile(_ < pow(2, i + 1)).map(j => (pow(2, i) * j).toInt).toList.map(k => (1 to (mn / k).toInt).map(n => k * n))).flatten.flatten.distinct.sortWith((a, b) => (a compareTo b) < 0)
}
def main(args : Array[String]) {
val mn = 1000
@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
@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 / 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 / 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)