Skip to content

Instantly share code, notes, and snippets.

@dmishin
dmishin / rosier_q
Created March 27, 2025 23:40
Implementation of "Rosier Q function", supports all rationals with odd denominator
from fractions import Fraction
from typing import List, Union, Tuple
def _orbit(x:Fraction)->Tuple[List[int], List[int]]:
"""Returns Collatz orbit of x, as a tuple of two lists: initial path and cycle. Assumes the cycle is always reached."""
if x == 0: return [],[0]
if x.denominator%2 == 0: raise ValueError("2-adically fractional values are not allowed")
visited = {x}
orbit = [x]
while True:
if x.numerator % 2 == 0:
@dmishin
dmishin / rational_in_range.py
Created March 26, 2025 23:51
Calculate the simplest rational number between log_2(3) and log_2(3)-1/2^{70}
import mpmath
from typing import Tuple
def simplest_rational_in_range(a:mpmath.mpf, b:mpmath.mpf)->Tuple[int,int]:
#take integer part of both numbers
d1 = int(mpmath.floor(a))
d2 = int(mpmath.floor(b))
if d1 != d2:
assert d1 < d2
return d1+1, 1
#take fractional part of both numbers
s="3248734278923478923478932798324734287932988934822872347243772437743272733248734278923478923478932798324734287932988934822872347243772437743272733248734278923478923478932798324734287932988934822872347243772437743272733248734278923478923478932798324734287932988934822872347243772437743272733248734278923478923478932798324734287932988934822872347243772437743272733248734278923478923478932798324734287932988934822872347243772437743272733248734278923478923478932798324734287932988934822872347243772437743272733248734278923478923478932798324734287932988934822872347243772437743272733248734278923478923478932798324734287932988934822872347243772437743272733248734278923478923478932798324734287932988934822872347243772437743272734324333248734278923478923478932798324734287932988934822872347243772437743272733248734278923478923478932798324734287932988934822872347243772437743272733248734278923478923478932798324734287932988934822872347243772437743272733248734278923478923478932798324734287932988934822872347243772437743272733248734
@dmishin
dmishin / l1_gravity_planet.py
Created April 2, 2024 00:11
Planet shape in L1 gravity, 2D case
import numpy as np
N = 300 #size of the grid
M = 20000 #number of pixels
potential = np.zeros((N, N), dtype=np.float64)
masses = np.zeros((N, N), dtype=np.bool8)
def potential_of_mass_at(i, j):
"""Returns L1 potential of a mass at (i, j) with mass m
potential is calcualted as:
@dmishin
dmishin / tsp_conjecture_counterexample.py
Created November 11, 2023 23:51
Use linear programming to find counterexample for a conjecture regarding the Traveling Salesman Problem
from scipy.optimize import linprog
import numpy as np
import itertools
#Searching for the counterexample for the following conjecture:
# in the shortest hamiltonian path, there is at least one
# node, connected to its 12 nearest neighbors
# https://www.reddit.com/r/math/comments/17rxc28/travelling_salesman_algorithm/
# Use linear programming to find a counterexample
@dmishin
dmishin / grotsen_iterates.py
Created June 28, 2023 21:05
Calculating and plotting smooth fractional iterates of 1-sqrt(1-x^2)
"""Calculate smooth iterates of the function
f:[0,1]->[0,1]
f(x) = 1-sqrt(1-x^2)
https://mathoverflow.net/questions/449748/what-are-the-iterates-of-x-mapsto-1-sqrt1-x2
https://twitter.com/gro_tsen/status/1674132770203881477
@dmishin
dmishin / thue_morse_hilbert_fft.py
Created October 27, 2022 11:43
Visualize Fourier transform of the Thue-Morse sequence arranged along the Hilbert curve
import numpy as np
from matplotlib import pyplot as plt
import scipy
def ht(n:int)->np.array:
"""Generates Thue-Morse sequence, arranged along the Hilbert curve of order N. Result is (2**n, 2**n) matrix"""
if n < 0: raise ValueError("Bad order")
if n == 0:
return np.array([[1]], dtype=np.int8)
else:
$("#run").click(() => tryCatch(run));
async function run() {
Excel.run(async function (context) {
var sctivesheet = context.workbook.worksheets.getActiveWorksheet();
var table = sctivesheet.tables.add(sctivesheet.getRangeByIndexes(0, 0, 1, 2), true);
var data = [[1, "A"], [2, "B"], [3, "C"]];
table.rows.add(0, data);
@dmishin
dmishin / check_invfib_sum.py
Created April 22, 2020 09:35
Numerically checking surprising identities regarding the sums involving Fibonacci numbers
#%metadata%
#url: https://twitter.com/diegorattaggi/status/1252654580720119810/photo/1
from sympy import *
mfib = Matrix([[0,1],[1,1]])
fib0 = Matrix([0,1])
luc0 = Matrix([2,1])
eye = Matrix([[1,0],[0,1]])
def fibo(n):
@dmishin
dmishin / schreiber.py
Created November 18, 2019 11:22
Detect optimal step for minimal order Schreiber test signal
import numpy as np
import scipy as sp
import scipy.linalg
from math import floor, pi
#Detect optimal step for minimal order Schreiber test signal
#Set to False to disable printing
_verbose = True