Skip to content

Instantly share code, notes, and snippets.

View Parcly-Taxel's full-sized avatar

Jeremy Tan Jie Rui Parcly-Taxel

View GitHub Profile
#!/usr/bin/env python3
from mpmath import *
from math import comb, factorial
def A(r, s):
if r == 0 or s in (0, 2):
return 1
if s == 1:
return 0
bits_needed = int((loggamma(s*r+1) - s*loggamma(r+1)) / log(2)) + 40
@Parcly-Taxel
Parcly-Taxel / pokersplits.py
Created March 9, 2024 14:04
Splitting poker hands (Sage)
from itertools import combinations
# paste into SageMathCell
# https://puzzling.stackexchange.com/q/125902/75318
straights = [list(range(i, i+5)) for i in range(1, 6)]
# For simplicity we assign (35, 36, 37, 38, 39) as the royal flush;
# since any 0-4 straight would then prevent four of a kind we omit that from the straights list
def hand_strength(hand):
#!/usr/bin/env python3
from subprocess import run
# Requires the compiled sharpSAT solver (https://github.com/marcthurley/sharpSAT) to be in the same folder as this script
def a(N):
if N < 2:
return 1
fam = {i: [] for i in range(2, N+1)}
for i in range(2, N+1):
@Parcly-Taxel
Parcly-Taxel / weigh.py
Created November 21, 2022 09:33
6-ball weighing for 2nd heaviest/lightest (SageMath)
from itertools import combinations
# https://puzzling.stackexchange.com/q/118794/75318
root = DiGraph({v: [] for v in range(6)}, immutable=True)
G = DiGraph({root: []})
unproc = G.vertices(sort=False)
while unproc:
H = unproc.pop()
HG = H.automorphism_group()
# Find orbit representatives of incomparable pairs
incomps = [(i,j) for (i,j) in combinations(range(6), 2) if H.distance(i,j) == H.distance(j,i) == infinity]
@Parcly-Taxel
Parcly-Taxel / octafold.py
Created November 16, 2022 17:55
7 octahedral nets into 1 octahedron
#!/usr/bin/env python3
from sage.combinat.matrices.dancing_links import dlx_solver
from itertools import combinations
import numpy as np
t7 = np.array([[3,2], [3,1], [4,1], [4,3], [4,4], [2,2], [1,1]])
t7r = np.stack([t7[:,0]-t7[:,1],t7[:,0]], axis=1)
t14 = np.concatenate([t7, t7r])
tt = np.concatenate([t14, t14-[5,1], t14+[1,-4], t14+[6,-3]]) # triangles
ttl = tt.tolist()
@Parcly-Taxel
Parcly-Taxel / cubefold.py
Last active January 12, 2023 12:24
CUBENET×5=CUBE
#!/usr/bin/env python3
from sage.combinat.matrices.dancing_links import dlx_solver
compass = "RULD"
rings = ("0R 1L 5D 11D 16D 23D 24R 25R 26R 27R", # blue
"2R 3R 4R 5R 6D 13D 18D 24D 28L 29R", # red
"9R 10R 11R 12R 13R 14R 20D 26D 29D 21U", # orange
"15R 16R 17R 18R 19R 20R 8U 0D 3D 9D", # yellow
"21R 22R 23R 17U 12U 6R 7R 8R 27D 2D", # green
"4D 10D 15D 22D 28D 25U 19U 14U 7U 1U") # violet
rings = [[(int(c[:-1]), c[-1]) for c in s.split()] for s in rings]
@Parcly-Taxel
Parcly-Taxel / fon.py
Last active November 21, 2022 01:59
Formation of numbers from 1, 2, 3, 4 and maybe 5
#!/usr/bin/env python3
from math import isqrt, factorial, log
from itertools import combinations, permutations, chain
# https://puzzling.stackexchange.com/q/118708/75318
lim = factorial(12)
sqrt = (lambda x: x > 1 and isqrt(x)**2 == x,
isqrt,
lambda s: f"{s} r")
fact = (lambda x: 3 <= x <= 12,
@Parcly-Taxel
Parcly-Taxel / astroid.py
Last active September 27, 2022 09:46
Arc lengths of super-astroids
#!/usr/bin/env python3
from mpmath import *
mp.dps = 50
# https://math.stackexchange.com/a/4539522/357390
def l(n):
return 2*n*quad(lambda v: sqrt(power(v,n-2)+power(1-v,n-2)), [0,1])
def L(n):
@Parcly-Taxel
Parcly-Taxel / cover.cpp
Created August 25, 2022 06:08
Nurmela–Östergard simulated annealing for covering numbers
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <algorithm>
#include <numeric>
#include <random>
typedef uint32_t rk_t; // rank type
typedef uint8_t el_t; // element type
struct cover_data {
rk_t** neighbours;
@Parcly-Taxel
Parcly-Taxel / bayes.py
Created February 2, 2022 15:29
PSE #114692
#!/usr/bin/env python3
import random
from collections import Counter
rng = random.SystemRandom()
import numpy as np
wheel = [0]*7 + [1,2,3,4,5,6,7,8,9]*2
def guess():
probs = np.full(10, 1/10)
trials = 0