Skip to content

Instantly share code, notes, and snippets.

View davorb's full-sized avatar
👋

Davor Babić davorb

👋
View GitHub Profile
ruby-1.9.3-rc1 :001 > @proc = lambda { ScratchPad.record :proc_trap }
=> #<Proc:0x8eb361c@(irb):1 (lambda)>
ruby-1.9.3-rc1 :002 > @saved_trap = Signal.trap(:HUP, @proc)
=> "DEFAULT"
ruby-1.9.3-rc1 :003 > Signal.trap :HUP, nil
=> #<Proc:0x8eb361c@(irb):1 (lambda)>
ruby-1.9.3-rc1 :004 > Signal.trap(:HUP, @saved_trap)
=> nil
def gcd_recursive(u, v)
return u|v if u==0 or v==0
if u.even?
if v.even?
return gcd(u>>1, v>>1)<<1
else
return gcd(u>>1, v) if v.odd?
end
elsif u.odd? and v.even?
================================ rubinius
/ 0 0.0 0i 0
1 ZeroDivisionError Infinity ZeroDivisionError ZeroDivisionError
1.0 Infinity Infinity Complex(NaN, NaN) Infinity
9223372036854775808 ZeroDivisionError Infinity ZeroDivisionError ZeroDivisionError
0 ZeroDivisionError NaN ZeroDivisionError ZeroDivisionError
0.0 NaN NaN Complex(NaN, NaN) NaN
0i ZeroDivisionError Complex(NaN, NaN) ZeroDivisionError ZeroDivisionError
0 ZeroDivisionError NaN ZeroDivisionError ZeroDivisionError
@davorb
davorb / fast_sqroot.java
Created December 1, 2011 15:46
fast square root java
/** Calculate the square root of a BigInteger in logarithmic time */
public BigInteger squareRoot(BigInteger x) {
BigInteger right = x, left = BigInteger.ZERO, mid;
while(right.subtract(left).compareTo(BigInteger.ONE) > 0) {
mid = (right.add(left)).shiftRight(1);
if(mid.multiply(mid).compareTo(x) > 0)
right = mid;
else
left = mid;
}
@davorb
davorb / .screenrc
Created December 21, 2011 12:22
emacs friendly screen
escape ^Tt
import org.schwering.irc.lib.IRCConnection;
import org.schwering.irc.lib.IRCEventListener;
import org.schwering.irc.lib.IRCModeParser;
import org.schwering.irc.lib.IRCUser;
import org.schwering.irc.lib.ssl.SSLIRCConnection;
import org.schwering.irc.lib.ssl.SSLTrustManager;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
let smallerSorted = quicksort [a | a <- xs, a <= x]
biggerSorted = quicksort [a | a <- xs, a > x]
in smallerSorted ++ [x] ++ biggerSorted
qs [] = []
qs (x:xs) = qs [a|a<-xs, a<=x]++[x]++qs [a|a<-xs, a>x]
@davorb
davorb / euler1.hs
Created March 29, 2012 15:45
project euler 1
-- the sum of all the multiples of 3 or 5 below 1000
numbers = [x | x <- [1..], x `mod` 3 == 0 || x `mod` 5 == 0]
answer = sum $ takeWhile (<1000) numbers
@davorb
davorb / euler2.hs
Created March 29, 2012 16:05
project euler 2
-- the sum of the even valued values of the fibonacci sequence
-- whose values do not exeed four million
fibonacci 0 = 0
fibonacci 1 = 1
fibonacci n = fibonacci (n-1) + fibonacci (n-2)
fib = [fibonacci n | n <- [0..]]
answer2 = sum $ filter even $ takeWhile (<4000000) fib
main = putStrLn $ show answer2