Skip to content

Instantly share code, notes, and snippets.

@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()
@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 / solar.txt
Created December 6, 2022 04:06
Standard Solar Model
Standard Solar Model (BP2004)
https://www.sns.ias.edu/~jnb/SNdata/Export/BP2004/bp2004stdmodel.dat
astro-ph/0402114
Columns in the Standard Model table (below) represent:
1) Mass fraction in units of the solar mass
2) Radius of the zone in units of one solar radius
3) Temperature in units of deg (K)
4) Density in units of g/cm^3
@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 = [
@PM2Ring
PM2Ring / divisors_sieve.py
Created April 18, 2022 12:26
Find all divisors in a range by sieving
#!/usr/bin/env python3
''' Find all divisors in a range by sieving
Written by PM 2Ring 2017.05.03
Prime GP version
'''
from itertools import product
@PM2Ring
PM2Ring / julian_day.py
Created March 6, 2022 08:13
Julian Day Number conversion, Gregorian & Julian
""" Julian day number to date conversion
Proleptic Gregorian and Julian, with Astronomical years
i.e., 1 AD = year 1, 1 BC = year 0, 2 BC = year -1, etc
Derived from RG Tantzen (1963), ACM.
Algorithm 199: conversions between calendar date and Julian day number.
https://en.wikipedia.org/wiki/Julian_day
Julian day number 0 assigned to the day starting at noon on
January 1, 4713 BC, proleptic Julian calendar