Skip to content

Instantly share code, notes, and snippets.

@msalahi
msalahi / collatz.py
Created September 9, 2011 14:53
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(n):
if n not in mem: mem[n]= 1 + collatzCount(n/2 if (n%2==0) else 3*n+1) if (n>1) else 1
return mem[n]
@msalahi
msalahi / perfect.py
Created September 14, 2011 22:19
oheyandy2
import math
import time
import itertools
def isAbundant(n):
div = [1]
for i in range(2,int(math.sqrt(n))+1):
if n%i==0:
div += [i] if i==n/i else [i,n/i]
return True if sum(div)>n else False
@msalahi
msalahi / BROuler23.py
Created September 15, 2011 14:22
euler23
import math
import time
def isAbundant(n):
div = [1]
for i in range(2,int(math.sqrt(n))+1):
if n%i==0:
div += [i] if i==n/i else [i,n/i]
return True if sum(div)>n else False
@msalahi
msalahi / week7.py
Created September 16, 2011 16:57
MuradWeek7Solution
import math
def isAbundant(n):
div = 1
for i in range(2,int(math.sqrt(n))+1):
if n%i==0:
div += i if i==n/i else i+(n/i)
return True if div>n else False
def sumOfUniqueAbundantPairSums():
@msalahi
msalahi / muradWeek8.py
Created September 23, 2011 15:39
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
@msalahi
msalahi / landon.py
Created October 21, 2011 04:34
gettin' some chains
def landon(array,k,threshold):
sections=[]
start,end=0,0
for i in range(0,len(array)):
#print i,array[i],"start: ",start,"end: ",end
if array[i]<threshold:
start= start if arr[start]<threshold else i
end=i
else:
@msalahi
msalahi / integral.py
Created February 10, 2012 00:48
WHY GODDDD
import sys,time,random
from multiprocessing import Pool
def integral_helper((f,start,end,num_samples)):
return sum( (f((end-start)*random.random()+start) for i in xrange(num_samples)))
def integral(f,start,end,num_processes):
WORK_ITEMS = 100;
pool = Pool(processes=num_processes)
args = (f,start,end,WORK_ITEMS/num_processes)
@msalahi
msalahi / convolve.py
Created March 5, 2012 04:51
seam carvin'
from math import sqrt
import random,time
import profile
def bellman_ford(edges):
numRows = len(edges)
numCols = len(edges[0])
OPT = dict(( (len(edges)-1,x),edges[-1][x]) for x in xrange(len(edges[-1])))
opt = dict(( (len(edges)-1,x),None) for x in xrange(len(edges[-1])))
@msalahi
msalahi / monte_carlo.py
Created July 7, 2013 09:25
parallelized monte carlo integration using python 3
import concurrent.futures
import random
def f(x):
return x**2
def integrate(func, start, end, num_samples=1000):
"""uses random sampling to integrate func from 'start' to 'end'"""
with concurrent.futures.ProcessPoolExecutor() as executor:
@msalahi
msalahi / monte_carlo_test.py
Created July 7, 2013 18:23
performance fix for ProcessPoolExecutor
import concurrent.futures
import multiprocessing
import random
from fast import FastProcessPoolExecutor
import time
from functools import partial
def fn2(fn, chunk):
return [fn(*args) for args in zip(*chunk)]
class FastProcessPoolExecutor(concurrent.futures.ProcessPoolExecutor):