Skip to content

Instantly share code, notes, and snippets.

View cpdean's full-sized avatar

Conrad cpdean

View GitHub Profile
@cpdean
cpdean / gist:888994
Created March 27, 2011 06:46
line 28 is some bullshit
def fib(n):
if n in (0,1):
return n
else:
return fib(n-1) + fib(n-2)
def memoize(n):
cache = {}
def helper(x):
if x not in cache:
@cpdean
cpdean / collatz.py
Created September 9, 2011 15:29 — forked from msalahi/collatz.py
oheyandy
import time
bound,mem = 1000000,{}
def collatz(n):
if n==1: return [1]
else: return [n] + collatz(n/2 if (n%2==0) else 3*n+1) if (n>1) else [1]
def collatzCount_recursive_memoized(n):
if n not in mem: mem[n]= 1 + collatzCount_recursive_memoized(n/2 if (n%2==0) else 3*n+1) if (n>1) else 1
return mem[n]
@cpdean
cpdean / guy.py
Created September 9, 2011 16:53
python optimizations
import time
import itertools
import hashlib
def regular_for_loop(bound):
_md5 = hashlib.md5()
for i in itertools.repeat("foo", bound):
_md5.update(i)
@cpdean
cpdean / isabundant.py
Created September 14, 2011 17:51
another speedup challenge
import math
def isAbundant(n,f): #make this run fast
div = f(n)
return True if sum(div)>n else False
def factors_for_loop_plus_operator(n):
# bug, feed this a perfect square and the square root
# will be counted twice
div = [1]
@cpdean
cpdean / tests.py
Created September 15, 2011 22:40
testing the 'in' operator #science
from time import time
def test(iterable):
start = time()
for i in range(scale):
if i in iterable:
a = 90.0 + 6000
return time()-start
def aggregate(count,f,a):
@cpdean
cpdean / muradWeek8.py
Created September 23, 2011 17:41 — forked from msalahi/muradWeek8.py
MuradWeek8Solution
from time import time
from math import sqrt
def disproves(n):
i=2
while i*i*2<n:
diff = n-i*i*2
if not any(diff%j==0 for j in xrange(3,int(sqrt(diff))+1,2)) :return False
i+=1
return True
n=3
@cpdean
cpdean / Tracer.js
Created October 23, 2011 03:24
disecting that clipboard hijacker
Tynt = window.Tynt || [];
if (typeof Tynt.TIL == "undefined") {
(function() {
var Ja = function() {
var h = document,
l = h.body,
p = h.documentElement,
aa = eval("/*@cc_on!@*/false"),
ba = function(a, b) {
@cpdean
cpdean / convolve.py
Created March 5, 2012 04:54 — forked from msalahi/convolve.py
seam carvin'
from math import sqrt
import random,time
import profile
random.seed(1)
def bellman_ford(edges):
numRows = len(edges)
numCols = len(edges[0])
@cpdean
cpdean / solution.py
Created July 21, 2012 16:25
Trolling with phone math
sleuth = 7+1+5
target = 37
simple_sum = target - sleuth # 24
from itertools import combinations
seed = [2 for n in range(7)] + [3 for n in range(7)] + [5 for n in range(7)]
answers = set(a for a in combinations(seed,7) if sum(a) == simple_sum)