Skip to content

Instantly share code, notes, and snippets.

@PM2Ring
PM2Ring / fpe_feistel.py
Created January 17, 2024 11:01
Format preserving encryption using a Feistel network
""" Format preserving encryption using a Feistel network
See https://stackoverflow.com/a/51429458/4014959
Written by PM 2Ring 2016.08.22
Version using Blake2 hashing 2021.06.14
Added pair permutation 2024.01.17
"""
from hashlib import blake2s as blake
from random import Random
@PM2Ring
PM2Ring / ellipse3d.py
Created November 2, 2023 04:17
Plot filled ellipse in 3D
""" Plot filled ellipse in 3D.
With major axis and line of nodes
Written by PM 2Ring 2023.9.24
"""
var('th')
deg = n(pi / 180)
def ellipse(a, ec):
@PM2Ring
PM2Ring / hyper.svg
Created November 1, 2023 23:12
Hyperbolic color demo
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@PM2Ring
PM2Ring / AGM_pi.py
Created October 31, 2023 18:39
The Salamin / Brent / Gauss Arithmetic-Geometric Mean pi formula, using fixed point arithmetic
""" Gauss / Salamin / Brent true AGM pi
Binary fixed point
Written by PM 2Ring 2023.11.1
"""
from math import isqrt
def AGM_pi(loops, bits):
a, b = 1 << bits, isqrt(1 << 2*bits-1)
s = 1 << bits-2
@PM2Ring
PM2Ring / chromostereo_blue.py
Created October 27, 2023 16:48
Chromostereopsis illusion, with blue noise
""" Chromostereopsis illusion
With generated blue noise dither (void & cluster),
Poisson disk, white noise, checks, and undithered.
See https://mathematica.stackexchange.com/q/289992
"""
import numpy as np
from scipy import ndimage
from sage.repl.image import Image
@PM2Ring
PM2Ring / bberg32.py
Last active September 8, 2023 04:47
Fast primality for 32 bit integers, using a single SPRP test
""" BBerg32
Primality test for 32 bit integers using a single SPRP test.
The SPRP base comes from a hash table.
Hash and bases by Bradley Berg
See https://techneon.com/download/is.prime.32.base.data
"""
bases = [
@PM2Ring
PM2Ring / 6x6_faultline_counts.txt
Last active August 10, 2023 23:57
Domino tiling
(0, 1) : 50
(1, 0) : 50
(0, 2) : 258
(1, 1) : 600
(2, 0) : 258
(0, 3) : 186
(1, 2) : 1086
(2, 1) : 1086
(3, 0) : 186
(0, 4) : 5
@PM2Ring
PM2Ring / MaxwellBoltzmann.py
Created January 19, 2023 10:32
Plot a Maxwell Boltzmann distribution graph using Sage
""" Maxwell–Boltzmann distribution
See https://en.m.wikipedia.org/wiki/Maxwell%E2%80%93Boltzmann_distribution
Written by PM 2Ring 2021.06.20
"""
# Gas constant g⋅m^2⋅s^−2⋅K^−1⋅mol^−1
R = 8314.46261815324
@interact
def _(T=('temp (°C)', 20), mass=('mass (g/mol)', 29),
auto_update=False):
@PM2Ring
PM2Ring / lorenz.py
Created January 4, 2023 11:59
Lorenz attractor
""" Lorenz attractor
From https://en.wikipedia.org/wiki/Lorenz_system
Modified for Sagemath by PM 2Ring 2023.01.04
"""
import numpy as np
from scipy.integrate import odeint
# Build Bezier curves
@PM2Ring
PM2Ring / borwein_pi.py
Created January 2, 2023 07:11
Borwein quadratic pi
""" Borwein + Borwein quadratic pi
Algorithm 2.1 from "Pi and the AGM" (Wiley, 1987)
Written by PM 2Ring 2023.1.2
"""
from math import floor, log, pi
from decimal import Decimal as D, getcontext
def borwein_pi(loops):
x = D(2).sqrt()