Skip to content

Instantly share code, notes, and snippets.

@hdf
hdf / bellard.py
Last active March 29, 2024 00:05
Bellard's formula for calculating PI in Python
import sys
from decimal import Decimal, getcontext
k = int(sys.argv[1]) if len(sys.argv) > 1 else 70
def bellard(n):
getcontext().prec = n + 1
return Decimal(1.0/(2**6)) * sum([Decimal(-1)**k/(1024**k) * (Decimal(256)/(10*k+1) + Decimal(1)/(10*k+9) - Decimal(64)/(10*k+3) - Decimal(32)/(4*k+1) - Decimal(4)/(10*k+5) - Decimal(4)/(10*k+7) - Decimal(1)/(4*k+3)) for k in range(n)])
print(bellard(k))
@hdf
hdf / dirlist.py
Last active June 9, 2023 15:11
Usage:dirlist.py c:\ > c_list.txt
import os, sys
cdir = '.'
if len(sys.argv) > 1:
cdir = sys.argv[1]
for dirname, dirnames, filenames in os.walk(cdir):
dirname = os.path.realpath(dirname)
ending = ''
try:
@hdf
hdf / combine2len.py
Last active January 5, 2023 14:39
Crossword puzzle solving helpers.
# Usage:
# ---
# python combine2len.py [total_len [num_num_spaces ['words.txt' ['banned.txt']]]]
from itertools import combinations
from pprint import pprint
from sys import argv
def get_combinations(strings, num_strings, total_length):
result = []
@hdf
hdf / crc32_quine.cpp
Last active November 2, 2022 11:33 — forked from praetoriansentry/main.go
Program to find crc32 collision, generates output like: `Hello, my hash is: 1A745BD7! :)`
// To compile:
// nvcc -O3 -Xcompiler -openmp crc32_quine.cpp -o crc32_quine
// Inspired by: https://gist.github.com/praetoriansentry/03b6dc2e68e174ffd8168aef7d85f910
// (Can be timed with: http://www.pc-tools.net/win32/ptime/)
// (Was not able to make it faster than the insiration, golang is impressive.)
#include <stdio.h> // printf, sprintf
#include "Crc32.cpp" // From: https://github.com/stbrumme/crc32
@hdf
hdf / animate.py
Last active September 6, 2022 23:18
Comb particle evolution data from multiple files into one moment to moment, system state based file. Also visualize the data using matplotlib.
# From: https://gist.github.com/hdf/42f0895a091b4a6a6d5d9eeb5cca8edf
import time, sys, os#, arrow
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.colors as colors
import matplotlib.ticker as ticker
import json
@hdf
hdf / div_hist.py
Created June 8, 2021 18:52
Recursive divisibility histogram
import sys
import numpy as np
import matplotlib.pyplot as plt
## Recursive divisibility histogram
# How many times can we divide a number by an integer, and by how many integers?
# Divisors start from 2 and increase by 1 until we reach half of the number.
# Show 3D histogram.
n = int(sys.argv[1]) if len(sys.argv) > 1 and sys.argv[1].isdigit() else 180
@hdf
hdf / q.txt
Created February 27, 2021 13:11
Dilemma solutions.
Chicken and egg dilemma solution:
---
Description of the dilemma:
What came first, the chicken or the [chicken] egg? (Eggs in general certainly existed before chickens did.)
Statistical approach:
Almost all chickens lay eggs. But most eggs do not hatch to be chickens. (Most are not fertilized, or it is a rooster that hatches.) Thus it is far more likely for an egg to come from a chicken, (thus chicken coming first) than for a chicken to come from an egg.
Nomenclaturical approach:
The egg that the first chicken came out of was laid by a proto-chicken. Since it could not be known ahead of time, what would emerge from the egg, it is logical to say, that a proto-chicken lays proto-chicken eggs, even if it is a chicken that emerges from it. Here as well the chicken came first (as it emerged from a proto-chicken egg).
Meta approach:
@hdf
hdf / index.html
Last active February 5, 2021 23:17 — forked from hynekcer/maxsubstring.py
fast longest common substring - by suffix array
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Redundancy checker</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--<script src="lrs.min.js"></script>-->
<style>
html {
font-family: 'Verdana', sans-serif;
@hdf
hdf / conway.py
Last active October 24, 2020 14:42
My solution(s) to John Conway's 10 Digit number puzzle: (https://www.quantamagazine.org/three-math-puzzles-inspired-by-john-horton-conway-20201015/)
import string
from sys import argv, stderr
from time import time
def spinner_f():
while True:
for c in '|/-\\':
yield c
spinner = spinner_f()
@hdf
hdf / dir.py
Last active July 22, 2020 16:53
Generate html file with foldable recursive directory listing. (For directories with no auto indexing.)
import os, sys
from datetime import datetime
from zipfile import ZipFile
dir = '.'
out = 'dir.html'
if len(sys.argv) > 1:
dir = sys.argv[1]
if len(sys.argv) > 2: