Skip to content

Instantly share code, notes, and snippets.

@goulu
goulu / gogolplex.py
Created May 28, 2021 05:30
"stores" and prints gogol and gogolplex numbers
from itertools import product
from Goulib.itertools2 import ilen
ten = range(10)
def tenpow(p):
# p is the log of exponent
return product(ten, repeat=p)
def tenprint(iter):
@goulu
goulu / passall.py
Last active May 16, 2019 18:26
very (un)useful class for test driven development: objects of the PassAll class will pass any test you can think of :-D
import inspect
import re
regexes=[
r"assert\((.*)(==|!=|>|<)(.*)\)",
#TODO: expand for test frameworks like unittest, nose ...
]
def _make_true(op,value):
# returns a value that satisfies comparizon
@goulu
goulu / index.html
Last active June 27, 2018 10:45
Table using D3.js and clusterize.js
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>D3Table</title>
<link rel="stylesheet" href="https://rawgit.com/goulu/Clusterize.js/master/clusterize.css"></script>
<script src="https://rawgit.com/goulu/Clusterize.js/master/clusterize.js"></script>
<script src='http://d3js.org/d3.v3.js' type='text/javascript'></script>
<script src="./table.js" type='text/javascript'></script>
</head>
@goulu
goulu / python_finesses.ipynb
Created June 23, 2018 08:53
Finesses de python
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@goulu
goulu / image.js
Last active June 11, 2018 08:06
D3 zoomable canvas class
const margin = {top: 20, right: 10, bottom: 20, left: 60};
class Figure {
constructor(div,width,height) {
this.width = width;
this.height = height;
// Canvas is drawn first, and then SVG over the top.
this.canvas = div.append("canvas")
@goulu
goulu / metercst.py
Last active May 26, 2017 08:54
Finds a better constant than 299792458 to define the meter
from fractions import gcd
s=9192631770 # number of caesium oscillations in a second
bestm=299792458 # fraction of a second in a meter
bestgcd=gcd(s,bestm) #14 . we want a larger one
for m in range(299792458, 300000000):
g=gcd(s,m)
if g>bestgcd:
bestm,bestgcd=m,g
print(bestgcd,bestm,s/bestgcd,m/bestgcd)
@goulu
goulu / fibonacci.py
Last active April 26, 2017 05:20
compute fibonacci(n)%m for very large n
from Goulib.math2 import digits, identity
def mod_matmul(A,B, mod=0):
return [[sum(a*b for a,b in zip(A_row,B_col))%mod for B_col in zip(*B)] for A_row in A]
def mod_matpow(M, power, mod=0):
result = identity(2)
for power in digits(power,2,True):
if power:
result = mod_matmul(result, M, mod)
@goulu
goulu / triples.py
Last active December 17, 2016 10:38
An inefficient way to generate Pythagorean triples
import itertools, math
def triples():
""" generates Pythagorean triples sorted by z,y,x with x<y<z
"""
for z in itertools.count(5):
for y in range(z-1,3,-1):
x=math.sqrt(z*z-y*y)
if x<y and abs(x-round(x))<1e-12:
yield (int(x),y,z)
@goulu
goulu / munchausen.py
Last active October 4, 2016 11:42
Efficient search of Munchausen numbers
#!/usr/bin/env python
# coding: utf8
"""
efficient search of Münchausen numbers
https://en.wikipedia.org/wiki/Munchausen_number
https://oeis.org/A046253
motivated by http://www.johndcook.com/blog/2016/09/19/munchausen-numbers/
"""
__author__ = "Philippe Guglielmetti"
@goulu
goulu / faulhaber.py
Last active July 15, 2016 11:51
Faulhaber formula to calculate sum of powers of integers using a generator for Bernouilli numbers
from scipy.special import binom as binomial
def bernouilli_gen(init=1):
"""generator of Bernouilli numbers
:param init: int -1 or +1.
* -1 for "first Bernoulli numbers" with B1=-1/2
* +1 for "second Bernoulli numbers" with B1=+1/2
https://en.wikipedia.org/wiki/Bernoulli_number
https://rosettacode.org/wiki/Bernoulli_numbers#Python:_Optimised_task_algorithm
"""