Skip to content

Instantly share code, notes, and snippets.

View telliott99's full-sized avatar

Tom Elliott telliott99

View GitHub Profile
@telliott99
telliott99 / rational_sqrt3.py
Created December 23, 2020 20:42
Rational approximations to square roots
m = 2
N = 500
# pre-compute squares
D = dict()
for n in range(2,2*N):
D[n] = n**2
for d in range(3, N):
n = m
import sys
s = int(sys.argv[1])
def next(n):
n*= 10
e = (len(str(n))-1) * 2
t = s * 10**e
m = n
for i in range(10):
import sys
def first_prime_factor(n,pL):
for p in pL:
if n % p == 0:
return p, n/p
return 1,n
def get_primes(N):
pL = [2,3]
from math import *
# requires x != 0
def radial(x,y):
r = (x**2 + y**2)**0.5
t = atan(1.0*y/x)
return r,t
def cartesian(r,t):
x = cos(t) * r
import sys
from operator import itemgetter
# greatest common divisor
from fractions import gcd
# pre-computed triples
fh = open('triples.txt')
data = fh.read().strip().split('\n')
fh.close()
import sys
from operator import itemgetter
# greatest common divisor
from fractions import gcd
# pre-computed triples
fh = open('triples.txt')
data = fh.read().strip().split('\n')
fh.close()
# greatest common divisor
from fractions import gcd
n = 100
# pre-compute squares
N = 15*n
L = [False]
L.extend([i**2 for i in range(1,N)])
rL = list()
def next(S,C,T):
C2 = (0.5 * (1 + C))**0.5
S2 = 0.5 * S / C2
T2 = S/(1+C)
return S2,C2,T2
S = 1/2**0.5
C = S
T = S/C
n = 4
'''
radius 1
inscribed square
a = (2/sqrt(2))^2 = 2
circumscribed square
A = 2^2 = 4
'''
a = 2
A = 4
s = "%5d %3.10f %3.10f"
@telliott99
telliott99 / pi.py
Last active September 30, 2018 21:07
Archimedes method to compute pi
p = 4.0/(2**0.5)
P=4
def one_round(t):
p,P = t
P2 = 2*p*P/(p+P)
p2 = (p*P2)**0.5
return p2,P2
s = '%3.10f %3.10f'