Skip to content

Instantly share code, notes, and snippets.

@possibly-wrong
possibly-wrong / compare_binomial.m
Created June 29, 2023 22:24
Probability P(Theta1 > Theta2) for independent beta-distributed random variables
function p = compare_binomial(x1, n1, x2, n2)
% Compute P(Beta(x1+1,n1-x1+1) > Beta(x2+1,n2-x2+1)).
% Compute p(x1, n1, x2, x2).
p = 1;
for k = 0:x2
p = p * (x1 + 1 + k) / (n1 + 2 + k);
end
% Iterate Bayes updates with each of n2 - x2 failures.
@possibly-wrong
possibly-wrong / knights_knaves.txt
Created October 11, 2022 14:33
All 3848 knights/knaves puzzles with 3 inhabitants and a minimally unique solution
Alex: Ben is a knight.
Ben: Chuck is a knight.
Chuck: Ben is a knight and Alex is a knave.
Alex: Ben is a knight.
Ben: Chuck is a knight.
Chuck: Chuck is a knight and Alex is a knave.
Alex: Ben is a knight.
Ben: Chuck is a knight.
@possibly-wrong
possibly-wrong / horse_race.m
Created June 9, 2022 12:46
Across the Board horse racing game
(* Compute probability of given subset of horses scratching. *)
scratch[p_] := Module[
{done = 2^Length[p] - 1, q, k},
q[0] /. Solve[
Table[
q[k] == If[k == done, 1,
Total@MapIndexed[
#1 q[BitOr[k, 2^(#2[[1]] - 1)]] &,
p
]
@possibly-wrong
possibly-wrong / gist:bed5b48b11acba40dbabbd2390abfc4f
Created April 11, 2022 14:44
Make 23 from {2, 3, 6, 16, 22}
3+(6+(22-16/2))
3+(22+(6-16/2))
3+(22-16/(2+6))
3+(22-16/(6+2))
6+(3+(22-16/2))
6-(16-3/(2/22))
6-(16-3*22/2)
6-(16-22/(2/3))
6-(16-22*3/2)
6+(22+(3-16/2))
@possibly-wrong
possibly-wrong / chocolates.cpp
Created December 4, 2020 20:57
Searching for chocolates
// https://possiblywrong.wordpress.com/2015/02/08/searching-for-chocolates/
#include "math_Rational.h" // https://github.com/possibly-wrong/precision
#include <vector>
#include <map>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <ctime>
@possibly-wrong
possibly-wrong / chocolates.m
Last active December 4, 2020 20:59
Searching for chocolates
(* https://possiblywrong.wordpress.com/2015/02/08/searching-for-chocolates/ *)
d[n_, k_] := If[! (0 <= k <= n), 0,
Sum[
(-1)^j Binomial[k, j] (n - j)!,
{j, 0, k}
]
]
v[n_, k_] := v[n, k] = Which[
@possibly-wrong
possibly-wrong / keycount_mod.c
Created October 25, 2020 12:51
6x6 Hotel Key Card Counter mod
/*
Ref: https://gist.github.com/skeeto/1718d6613dc47a7aff13333942a40776
*/
static long long
valid(long long x)
{
int v = 0;
long long a = transpose(x);
v += x == a;
a = flipv(a);
@possibly-wrong
possibly-wrong / skittles.py
Created December 19, 2019 17:08
Monte Carlo simulation of searching for duplicate packs of Skittles
import numpy as np
def sample():
"""Return number of random Skittles packs opened until first duplicate."""
packs = set()
count = 0
while len(packs) == count:
packs.add(tuple(np.bincount(
np.random.randint(0, 5, np.random.binomial(100, 0.6)))))
count = count + 1
1 [1]
2 [1, 2]
3 [1]
4 [3]
5 []
6 []
7 []
8 [6]
9 []
10 []
units = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen',
'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
tens = ['zero', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty',
'seventy', 'eighty', 'ninety']
powers = ['zero', 'thousand', 'million', 'billion', 'trillion',
'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion',
'nonillion', 'decillion']
hundred = 'hundred'
minus = 'minus'