Skip to content

Instantly share code, notes, and snippets.

@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:
(defn decreasing-from-last [v]
(loop [start (dec (.length v))]
(let
[prev-start (dec start)]
(cond
(< prev-start 0) 0
(neg? (compare (get v prev-start) (get v start))) start
:else (recur (dec start))))))
(defn next-permutation [v]
(defn veckey-to-set [veckey]
(reduce (fn [[column set-key] k]
(if (contains? set-key k)
[column set-key]
[(conj column k) (conj set-key k)]))
[[] #{}]
veckey))
(defn handle-rest-char [set-key]
(let [char-seq (map char (range (int \A) (inc (int \Z))))]
import timeit
t1 = timeit.default_timer()
# run a long long time
t2 = timeit.default_timer()
print(t2 - t1)
[1,[[2],3]] |> List.flatten |> Enum.reverse |> Enum.map(fn(x) -> x * x end)
(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])