Skip to content

Instantly share code, notes, and snippets.

View paulhankin's full-sized avatar

Paul Hankin paulhankin

View GitHub Profile
This file has been truncated, but you can view the full file.
0 = 0
1 = 1
2 = 2
3 = 3
4 = 4
5 = 5
6 = 6
7 = 7
8 = 8
9 = 9
@paulhankin
paulhankin / strat.txt
Last active September 3, 2021 09:26
Optimal strategy for 8-card game
/ - / : WINS: none | LOSSES: none | DRAWS: 1, 2, 3, 4, 5, 6, 7, 8
/ - 1 : WINS: none | LOSSES: none | DRAWS: 2, 3, 4, 5, 6, 7, 8
/ - 2 : WINS: none | LOSSES: 1, 3, 4 | DRAWS: 5, 6, 7, 8
/ - 3 : WINS: none | LOSSES: 1, 2, 6 | DRAWS: 4, 5, 7, 8
/ - 4 : WINS: none | LOSSES: 1, 2, 7 | DRAWS: 3, 5, 6, 8
/ - 5 : WINS: none | LOSSES: 1, 3, 7 | DRAWS: 2, 4, 6, 8
/ - 6 : WINS: none | LOSSES: 1, 2, 3, 4, 7, 8 | DRAWS: 5
/ - 7 : WINS: none | LOSSES: 1, 4, 8 | DRAWS: 2, 3, 5, 6
/ - 8 : WINS: none | LOSSES: 1, 2, 3, 4, 6, 7 | DRAWS: 5
1 - 2 : WINS: none | LOSSES: 3 | DRAWS: 4, 5, 6, 7, 8
def g0(sx, sy, px, py, cache):
if sx < min(px, py) or sy < min(px, py):
return 0
if sx*sy < px*py:
return 0
if sx*sy == px*py:
return 1 if (sx==px and sy==py) or (sx==py and sy==px) else 0
best = 0
for i in range(1, sx):
best = max(best, guillotine(i, sy, px, py, cache) + guillotine(sx-i, sy, px, py, cache))
@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
#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 / 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>
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
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))
@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 = []
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]