Skip to content

Instantly share code, notes, and snippets.

@PM2Ring
PM2Ring / ApollonianTriangle.svg
Last active March 26, 2024 11:27
Miscellaneous SVGs
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@PM2Ring
PM2Ring / HorizonsBatch.py
Created January 21, 2022 03:45
Run Horizons batch file in Sage
""" Retrieve data from Horizons using a batch-file
Written by PM 2Ring 2021.12.27
"""
import re, requests
url = "https://ssd.jpl.nasa.gov/api/horizons_file.api"
pat = re.compile(r"(?:[^'\s]|'[^']*')+")
@interact
def go(batch=InputBox(width=40, height=24)):
if not batch: return
@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 / 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 / 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 / carlson_asin.py
Last active October 4, 2023 06:58
Carlson arcsin demo. Calculates pi.
""" pi from 10 * asin((phi-1)/2)
Uses Carlson's accelerated version of
Borchardt's modified AGM algorithm
Written by PM 2Ring 2022.05.11
"""
from itertools import count
from decimal import Decimal as D, getcontext
@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 / miller_rabin.py
Last active September 1, 2023 21:48
Deterministic Miller-Rabin primality test
""" Deterministic Miller-Rabin primality test
See https://en.m.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
and https://miller-rabin.appspot.com/
Written by PM 2Ring 2015.04.29
Updated 2022.04.20
"""
small_primes = [