Skip to content

Instantly share code, notes, and snippets.

trait Monoid[A] {
def mappend(a: A, b: A): A
def mzero: A
}
object Monoid {
implicit val IntMonoid: Monoid[Int] = new Monoid[Int] {
def mappend(a: Int, b: Int) = a + b
def mzero = 0
}
// compile error
import scala.annotation.tailrec
@tailrec
def fib(n: Int): Int = n match {
case 1 | 2 => 1
case _ => fib(n - 1) + fib(n - 2)
}
f1 = 1
f2 = 2
sum = 0
loop {
break if f2 > 4000000
sum += f2 if f2 % 2 == 0
f2 = f1 + f2
f1 = f2 - f1
}
require 'date'
t1 = DateTime.parse(ARGV[0])
t2 = DateTime.parse(ARGV[1])
seconds = ((t2 - t1) * 24 * 60 * 60).to_i
r = seconds.divmod(60)
puts "#{r[0]}min#{r[1]}sec"
require 'redis'
require 'hiredis'
$redis = Redis.new(
host: "127.0.0.1",
port: 6379,
timeout: 30.0,
driver: :hiredis
)
// project euler 28
//
// The sum of the vertices can be expressed as follows. The w is width of the square.
//
// sum = (w - 2)^2 + (w - 1)
// + (w - 2)^2 + 2(w - 1)
// + (w - 2)^2 + 3(w - 1)
// + (w - 2)^2 + 4(w - 1)
// = 4w^2 - 6w + 6
//
import scala.io.Source
val source = Source.fromFile("triangle.txt")
val triangle = collection.mutable.Map[Int, List[Int]]()
source.getLines foreach { line =>
triangle += triangle.size -> line.split(" ").map(_.toInt).toList
}
def neighborsMaxList(list: List[Int]): List[Int] = list match {
case l if l.length == 1 => Nil
@yutaono
yutaono / eratosthenes.py
Last active August 29, 2015 14:01
Sieve of Eratosthenes by python.
# coding: utf-8
import math
def eratosthenes(x):
sieve = []
prime = []
for num in range(2, x):
sieve.append(num)
@yutaono
yutaono / gcd.java
Created May 2, 2014 08:32
最大公約数をユークリッドの互除法で求める
private int gcd(int m, int n) {
int r = m % n;
if (r == 0) {
return n;
}
return gcd(n, r);
}
@yutaono
yutaono / gist:10612219
Last active August 29, 2015 13:59
.emacs
(load (expand-file-name (concat (getenv "HOME") "/.emacs.d/common.el")))