Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / worldproblem1_3.scala
Created March 23, 2013 10:53
世界で闘うプログラマ本の1-3
//write Kazua
object worldproblem1_3 {
def PermutationsCheck(chkstr1 : String, chkstr2 : String) : Boolean = {
if (chkstr1.length != chkstr2.length) return false
val chgchkstr1 = chkstr1.toList.sortWith((o1, o2) => o1.toString.compareToIgnoreCase(o2.toString) < 0).mkString
val chgchkstr2 = chkstr2.toList.sortWith((o1, o2) => o1.toString.compareToIgnoreCase(o2.toString) < 0).mkString
chgchkstr1.equals(chgchkstr2)
@kazua
kazua / worldproblem1_5.scala
Last active December 15, 2015 11:39
世界で闘うプログラマ本の1-5
//write Kazua
object worldproblem1_5 {
def ZipStr(tgt : String, zip : String = "", acl : String = "", bktgt : String = "") : String = tgt match {
case t if t.size == 0 => acl + zip.take(1) + zip.length
case t => acl match {
case a if a.length > 0 && a.length >= bktgt.length => bktgt
case a => zip match {
case z if z == "" => ZipStr(t.tail, z + t.head, a, t)
case z if z.head == t.head => ZipStr(t.tail, z + t.head, a, bktgt)
@kazua
kazua / worldproblem1_7.scala
Last active December 15, 2015 15:49
世界で闘うプログラマ本の1-7
//write Kazua
import scala.collection.mutable._
object worldproblem1_7 {
def ZeroUpdate(tgt : Array[Array[Int]]) = {
val rowbol = Map[Int, Int]()
val colbol = Map[Int, Int]()
for (i <- 0 until tgt.size) {
@kazua
kazua / daydiff.scala
Created April 4, 2013 11:15
年月日の全ての数字が異なる年月日のリスト生成
//write kazua
//年月日の中に同じ数値が現れない年月日のリスト
import java.util.Calendar
import java.text.SimpleDateFormat
object daydiff {
def sdf = new SimpleDateFormat("yyyyMd")
def daydiff(stday : Calendar, edday : Calendar, acl : List[String]) : List[String] = stday match {
case s if s.compareTo(edday) > 0 => acl.reverse