Skip to content

Instantly share code, notes, and snippets.

@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;
@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
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:
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 / 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)))
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) /
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)]))

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 / 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])
@juliusgeo
juliusgeo / ioccc_pi.py
Last active March 10, 2023 00:43
IOCCC 1988 Underscore Pi Python Recreation
print(''.join(b:=open(__file__).readlines()[1:]).count("#")/len(b)/4)
#######
#################
#######--------########
######-------------#######
######-----------------######
#####------------------######
######-------------------######
#####--------------------######
#####---------!\---------######