Skip to content

Instantly share code, notes, and snippets.

@onemouth
onemouth / gist:5593339
Created May 16, 2013 17:08
11 -> 10(-1)
trans2NegForm :: Integer -> [Int]
trans2NegForm x = loop x "init" []
where
trans 0 "init" = (0, "init")
trans 1 "init" = (-1, "trans")
trans 1 "trans" = (0, "trans")
trans 0 "trans" = (1, "init")
loop 0 "trans" ans = 1:ans
loop 0 "init" ans = ans
loop i status ans = loop (i `shiftR` 1) newStatus (newBit:ans)
/*
* linux 2.6.37-3.x.x x86_64, ~100 LOC
* gcc-4.6 -O2 semtex.c && ./a.out
* 2010 [email protected], salut!
*
* update may 2013:
* seems like centos 2.6.32 backported the perf bug, lol.
* jewgold to 115T6jzGrVMgQ2Nt1Wnua7Ch1EuL9WXT2g if you insist.
*/
@onemouth
onemouth / sieve.hs
Last active December 17, 2015 21:49
Sieve of Eratosthenes
sieve [] = []
sieve (x:xs) = x: sieve notMutipleOfX
where notMutipleOfX = filter (\n -> n `mod` x /= 0 ) xs
primes = sieve [2..]
main = print $ take 5000 primes

Client-side SSL

For excessively paranoid client authentication.

Using self-signed certificate.

Create a Certificate Authority root (which represents this server)

Organization & Common Name: Some human identifier for this server CA.

openssl genrsa -des3 -out ca.key 4096
openssl req -new -x509 -days 365 -key ca.key -out ca.crt
@onemouth
onemouth / sieve.py
Last active December 21, 2015 18:49
import itertools
from itertools import islice
from itertools import ifilter
def sieve(numbers):
head = next(numbers)
yield head
for x in sieve(ifilter(lambda x: x % head, numbers)):
yield x
@onemouth
onemouth / gist:6405402
Last active December 22, 2015 02:48
sieve.hs
import Data.List
import qualified Data.Map as Map
sieve xs = sieve' xs Map.empty
sieve' [] table = []
sieve' (x:xs) table =
case Map.lookup x table of
Nothing -> x: sieve' xs (Map.insert (x+x) [x] table)
Just facts -> sieve' xs (foldl' reinsert (Map.delete x table) facts)
@onemouth
onemouth / gist:6405584
Created September 1, 2013 16:36
sieve.py
from itertools import ifilter
from itertools import islice
def sieve_worker(numbers, table):
head = next(numbers)
if head not in table:
yield head
table[head+head] = [head]
else:
@onemouth
onemouth / gist:c22aa764a61709f641f1
Last active August 29, 2015 14:06
Exchange Money
(defn exchange [table coin]
(let [table-size (count table)]
(reduce #(assoc %1 %2 (+ (nth %1 %2) (nth %1 (- %2 coin))))
table
(range coin table-size))))
(defn total-exchange [money coins]
(let [table (->> (take money (repeat 0N))
(#(conj % 1N))
vec)]
(def atom?
(fn [a]
(not (seq? a))))
(def null?
(fn [a]
(or
(nil? a)
(= () a))))
(def b '[3 - - - - 5 - 1 -
- 7 - - - 6 - 3 -
1 - - - 9 - - - -
7 - 8 - - - - 9 -
9 - - 4 - 8 - - 2
- 6 - - - - 5 - 1
- - - - 4 - - - 6
- 4 - 7 - - - 2 -
- 2 - 6 - - - - 3])