Skip to content

Instantly share code, notes, and snippets.

@juliusgeo
juliusgeo / pi_day.py
Last active February 28, 2024 01:59
4 Ways to Calculate Pi
from functools import reduce
from math import factorial, comb, ceil, log2
from decimal import getcontext, Decimal as Dec
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)))
return abs(bs[-1])

20 million digits of pi in under a minute with Julia

I recently discovered a relatively obscure algorithm for calculating the digits of pi: https://en.wikipedia.org/wiki/Gauss–Legendre_algorithm. Well, at least obscure compared to Chudnovsky's. Wikipedia notes that it is "memory-intensive" but is it really? Let's compare to the MPFR pi function:

function gauss_legendre(prec)
    setprecision(BigFloat, prec, base=10)
    GC.enable(false)
@juliusgeo
juliusgeo / zipy.py
Last active November 19, 2023 18:53
Obfuscated ZIP File Compressor in Python
import sys;itls,colls,binasc,struct,time=[\
__import__(i) for i in("itertools",\
"collections","binascii","struct","time")];\
s=sys.stdin.read();(MOFFSET,MLENGTH,\
FILE_NAME,ZIP_NAME,
idx)=(2047,31,"a.txt"
, "a.zip", 0); \
compressed=[((*\
lo,s[idx]),idx:=\
idx+lo[1])[0] for\
@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 / README.md
Created July 10, 2023 23:45
Bits and Pieces: zlib compliant DEFLATE from scratch

Bits and Pieces: zlib compliant DEFLATE from scratch

zlib underlies most zip file decompressors, and DEFLATE is one the binary formats used to store compressed data in a bitstream. The goal of this article is to walk through how my Python DEFLATE compressor implementation works. There are many guides on the internet that describe how to implement each step of DEFLATE, but very few end up producing a bitstream that can actually be parsed by a library like zlib. This article assumes that you roughly know how each step of the DEFLATE algorithm is implemented, but are having trouble with some of the finer points that are often glossed over.

The code can be found in "deflate.py".

@juliusgeo
juliusgeo / main.py
Last active June 21, 2023 10:58
ASCII Eulerian Fluid Simulation in 2kB of Python
import os
timestep=1./60
gravity=-9.81
tg=timestep*gravity
s_x,s_y=10,20
u=v=[[.0 for A in range(s_y)]for A in range(s_x)]
pr=[[.0 for A in range(s_y)]for A in range(s_x)]
st=[[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],[0,1,1,1,1,0,.1,0,0,.1,.1,.1,.1,.1,.1,.1,0,0,0,0],[0,1,1,1,1,1,.1,.1,.1,.1,.1,.1,.1,.1,.1,0,0,0,0,0],[0,1,1,1,1,1,.1,.1,.1,.1,.1,.1,.1,.1,.1,0,0,0,0,0],[0,.1,0,1,1,1,.1,.1,.1,.1,.1,.1,.1,.1,.1,0,0,0,0,0],[0,.1,0,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,0,0,0,0,0,0],[0,.1,0,.2,.1,.1,.1,0,.1,.1,.1,.1,0,0,0,0,0,0,0,0],[0,.1,0,0,.2,.1,.1,0,.1,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
C=1e3/timestep
loop=[(A,B)for A in range(1,len(u)-1)for B in range(1,len(u[0])-1)]
@juliusgeo
juliusgeo / cursed.md
Last active May 21, 2023 10:53
'Hello, World!' Simplified: Only 24 Lines of Python
h = [
    lambda a, b, c, d: 0,
    lambda a: 0,
    lambda a, b, c, d, e, f, g, h: 0,
    lambda a, b, c, d, e, f, g, h: 0,
    lambda a, b, c, d, e, f, g, h, i, j, k: 0,
    None,
    lambda a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, x, t: 0,
 lambda a, b, c, d, e, f, g, h, i, j, k, l: 0,
@juliusgeo
juliusgeo / bom.py
Last active May 16, 2023 10:07
Constrict Your Ram With Python
from multiprocessing import Process
import psutil
from time import sleep
def ascii_bomb():
return [
"""
.,-:-,,.
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);
@juliusgeo
juliusgeo / typyngtest.py
Last active April 16, 2023 02:35
A pretty nice typing test in 80 lines of Python.
import os,select,tty,sys
from time import time
from functools import partial
# Need this because Unix-esque OSes are different from Windows.
try:
from msvcrt import getch as cf
except ImportError:
tty.setcbreak(sys.stdin.fileno())
cf=partial(sys.stdin.read,1)