Skip to content

Instantly share code, notes, and snippets.

View paulhankin's full-sized avatar

Paul Hankin paulhankin

View GitHub Profile
@paulhankin
paulhankin / change.py
Created April 26, 2015 05:14
Making change
def change(coins, N):
x = 1
for c in coins:
x *= ((1<<N*c*(N//c+2))-1)//((1<<N*c)-1)
return (x>>N*N)&((1<<N)-1)
print change([1, 5, 10, 25, 50, 100], 100)
@paulhankin
paulhankin / gist:2d4d39428214ed36b478
Created May 18, 2015 08:02
Closed-hand Chinese poker probabilities
front
1.01 : 7-5-3
2.01 : 9-5-4
3.05 : T-6-4
4.01 : J-3-2
5.02 : J-8-5
6.12 : J-T-8
7.02 : Q-7-3
8.07 : Q-9-5
9.04 : Q-T-8
@paulhankin
paulhankin / gist:2a05abd22be59bfb8268
Last active August 29, 2015 14:21
Full closed-hand Chinese poker data
front
0.11 : 4-3-2
0.19 : 5-3-2
0.24 : 5-4-2
0.30 : 5-4-3
0.41 : 6-3-2
0.48 : 6-4-2
0.56 : 6-4-3
0.61 : 6-5-2
0.69 : 6-5-3
import itertools
import random
def power_two(x):
return x and (x & (x - 1) == 0)
def asort(A, i, j):
assert power_two(j - i + 1), '%d - %d + 1 not power of 2' % (j, i)
if A[i-1] > A[j-1]:
A[i-1], A[j-1] = A[j-1], A[i-1]
@paulhankin
paulhankin / poker.py
Created December 30, 2016 23:25
Compute optimal strategy for simple poker game
# Player A and B each get a number from 0 to n-1.
# Player A can:
# check: -1, 0, +1
# raise: B fold then +1
# : B call then -2, 0, +2
# strat[i] is probability that A raises with hand i.
# Then counterB(n, strat) gives B's optimal counter strategy.
def counterB(strat):
n = len(strat)
result = []
def primes(n):
x = [False] * (n + 1)
for i in xrange(2, n + 1):
if x[i]: continue
yield i
for j in xrange(2 * i, n + 1, i):
x[j] = True
def prime_pairs(n):
ps = list(primes(n))
def concat_optimal(xs, i=0, j=None, cache=None):
if cache is None:
i, j, cache = 0, len(xs), dict()
if (i, j) not in cache:
cache[i, j] = concat_optimal0(xs, i, j, cache)
return cache[i, j]
def concat_optimal0(xs, i, j, cache):
if j == i+1:
return 0, xs[i], i
@paulhankin
paulhankin / triangle.c.diff
Created December 22, 2018 13:39
patch for triangle.c to make it 64bit clean
diff --git a/triangle.c b/triangle.c
index f9e23fa..06cba71 100644
--- a/triangle.c
+++ b/triangle.c
@@ -342,11 +342,8 @@
#define ONETHIRD 0.333333333333333333333333333333333333333333333333333333333333
-
-#include <inttypes.h>
#include <stdio.h>
int main() {
int result = 1;
int n = 17;
for (int i=0; i < (2^64); i++) {
result = (result * n) % 115763;
}
printf("%d\n", result);
return 0;
@paulhankin
paulhankin / a_test.go
Created February 27, 2020 09:11
monte carlo pi
package main
import (
"fmt"
"testing"
"github.com/db47h/rand64/xoroshiro"
)
const N = 10_000_000