Skip to content

Instantly share code, notes, and snippets.

l=lambda\
x0, y, x1, y1: \
((dx := x1 - x0, dy :=
y1-y,yi:=1,yi:=-1 if dy<0 else yi,
dy:=-dy if dy<0 else dy,d:=(2*dy)-dx,ps:=
set(),[(ps.add((x,y)),y:=y+yi,d:=d+2*(dy-dx))if d>
0 else(d:=d+2*dy)for x in range(x0,x1)]),ps)[1];pp=lambda \
ps:list(map(print, [''.join(["-"if(x, y)in ps else" "for x in range
(127)])for y in range(10)]));pp(set.union(*[l(0, 0, i, 9) for i in range(37, 127)]))
from functools import reduce as red; from math import \
(factorial as fact, comb);import sys;from decimal import \
(getcontext as c,Decimal as dc);(a:=range,
b:=int(sys. argv[1]));c\
().prec=b; ber=lambda\
e,f=[dc(1)]: [f.append(
1-sum(comb(h ,g)*f[g]/(h
- g + 1) for g in a(h)))for
h in a(1, e + 1)] and abs(
f[-1]);print ((2 * fact(b) /
@juliusgeo
juliusgeo / pi_arb.py
Created January 17, 2023 21:10
Pi with arbitrary precision using Bernoulli numbers (the slow way)
from functools import reduce
from math import factorial, comb
from decimal import getcontext, Decimal as Dec
from timeit import timeit
def bernoulli(n):
bs = [Dec(1)]
for m in range(1, n+1):
bs.append(1 - sum(comb(m, k)*b / (m - k + 1) for k, b in zip(range(m), bs)))
n=256;q, t, m, e, p, c, f=(range(n),16,lambda x
,y, r=0:m((h:=x <<1,h^283)[
h&n!= 0],y>> 1,(r,r ^x)
[y&1])if y else r,lambda
a,w= 1,p=n -2:e(m(a, a),(w,m
(w,a))[p&1] ,p>> 1)if p
else w, lambda b: list(
map (print,["%.2x "*t%(*b[r:r+t],)for
r in [*q][::t]])),lambda a,i: (a<<i|a>>8-i)&255
,lambda a: (a^c(a,1)^c(a,2)^c(a,3)^c(a,4))^99);
from dataclasses import dataclass
from functools import reduce
@dataclass
class bff:
"""
An implementation of binary finite field arithmetic on fields of order 2^n using integers and bitwise operators.
Adaptation of algorithms in:
@juliusgeo
juliusgeo / .dockerignore
Last active July 11, 2023 09:36 — forked from sterin/CMakeLists.txt
An example showing how to use sub interpreters and threads in Python
/build/**
/venv*/**
/.idea/**
/repro.egg-info/**
/dist/
**/*.so
@juliusgeo
juliusgeo / longest_palindromic_substring.c
Last active January 11, 2021 18:14
Implementing longest palindromic substring algorithm using pthreads as practice
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
int sum;
struct input {
char* str;
int i;
};
int NUM_THREADS;