Skip to content

Instantly share code, notes, and snippets.

View scvalex's full-sized avatar

Alexandru Scvorțov scvalex

View GitHub Profile
(** Run:
coretop existential.ml
*)
open Core.Std
type ('a, 'b) entity = {
id : unit -> string;
step : 'b -> 'b some_entity;
to_string : unit -> string;
(** Run:
corebuild -pkg core_bench mat_mult.native && sleep 1 && ./mat_mult.native
*)
open Core.Std
open Core_bench.Std
module Array_mat = struct
type mat3 = float array array
type vec3 = float array
@scvalex
scvalex / Makefile
Last active August 29, 2015 14:12
Faster Fibonacci
run: fib
./fib
fib: fib.ml
ocamlfind ocamlopt nums.cmxa fib.ml -o fib
@scvalex
scvalex / Makefile
Last active August 29, 2015 14:13
Variable Length Arrays in C
all: var_len_arrays.c
gcc -O1 var_len_arrays.c -S
@scvalex
scvalex / Makefile
Last active August 29, 2015 14:14
pingcat
all: pingcat.c
gcc -o pingcat pingcat.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 5000000
int n;
char c[MAXN], *a[MAXN];
int pstrcmp(const void **x, const void **y) {
@scvalex
scvalex / bigdiv.py
Created March 13, 2010 20:58
Binary search division in Python
import sys
def bigmul(a, b):
"""r = a * b
where
r = bidmul(a, b)"""
xs = list(reversed(digits(a)))
ys = list(reversed(digits(b)))
zs = [0 for i in xrange(300)]
for i in xrange(len(ys)):
main :: IO ()
main = do
x <- getLine
y <- getLine
print (read x + read y)
@scvalex
scvalex / bdiv.hs
Created March 21, 2010 11:16
Binary search division
ultraDiv ∷ Integer → Integer → (Integer, Integer)
-- pre: 1 <= d <= n
-- post: a*d + b == n
-- && (A) x > d . (a*x > n)
-- where
-- (a, b) = ultraDiv n d
ultraDiv n d = bDiv n d 1 n
where
-- pre: same as ultraDiv
-- && d * lower <= n && d * upper > n
@scvalex
scvalex / gausselim.hs
Created April 3, 2010 19:21
Gaussian elimination on lists
-- PRE: xs is sorted and not null
-- POST: r is xs without the elements that vary by
-- more than err from the median
gausselim ∷ (Num a, Ord a) ⇒ a → [a] → [a]
gausselim err xs = filter good xs
where
middle = xs ‼ (length xs `div` 2)
good x = abs (x - middle) < err