Skip to content

Instantly share code, notes, and snippets.

import nodal
import sys
OUTFILE = "vll-sparse.csv"
RESISTANCE = 1
CURRENT = 1
def formattuple(coords, groundcoords):
if coords == groundcoords:
return "g"
@EnricoMiccoli
EnricoMiccoli / vlrl-356.py
Last active March 29, 2019 18:02
Very Large Resistor Lattice, as in xkcd 356
import sys
import numpy as np
import csv
import logging
from math import ceil
logging.basicConfig(level=logging.WARNING)
OUTFILE = "vll.csv"
RESISTANCE = 1
CURRENT = 1
@EnricoMiccoli
EnricoMiccoli / pythagorean_triples.py
Created July 4, 2016 21:01
Slowly compute pythagorean triples with pyDatalog
from pyDatalog import pyDatalog as pyd
import sys
pyd.create_terms('L1, L2, I, triples')
def triples(maxleg):
return (L1.in_(range(1,maxleg)) &
L2.in_(range(1,maxleg)) &
I.in_(range(1, 2 * maxleg)) &
(I ** 2 == (L1 ** 2 + L2 ** 2))
)
@EnricoMiccoli
EnricoMiccoli / e.hs
Created January 15, 2016 17:29
Computes e with arbitrary precision via Taylor series
import Data.Number.CReal
e :: (Integral a, Fractional b) => a -> b
-- The last term of the Taylor series is (1 / x!)
e x = snd $ foldl (\(below, result) x -> (below * x, result + (1 / fromIntegral (below * x)))) (1,1) [1..x]
safeE :: Int -> String
-- n is the number of decimal places
safeE n = showCReal n $
head [ e i | i <- [0..], showCReal n (e i) == showCReal n (e (i + 1))]
@EnricoMiccoli
EnricoMiccoli / bisection.py
Created December 28, 2015 12:20
Simple python implementation of the bisection method
MAXITER = 1000
TOL = 1e-6
def bis(func, lower, upper, maxiter=MAXITER, tol=TOL):
if lower >= upper:
raise ValueError('Range error: lower >= upper')
if func(lower) == 0:
return lower
elif func(upper) == 0:
@EnricoMiccoli
EnricoMiccoli / pigrec.hs
Last active December 28, 2015 12:09
Approximates pi using Archimedes' method
module Pigrec where
pigrec :: Int -> Float
pigrec x = foldl (ptwon) (2 * sqrt 2) (map (\x -> 2^x) [2..x])
ptwon :: Double -> Int -> Double
-- Given the perimeter pn of a n-sided polygon inscribed in a
-- circle of diameter 1 returns the perimeter of the 2n-sided
-- polygon inscribed in the same circle
ptwon pn n = n' * ( sqrt (1 + l) - sqrt (1 - l))