Created
September 14, 2011 17:51
-
-
Save cpdean/1217241 to your computer and use it in GitHub Desktop.
another speedup challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
for i in xrange(2,int(math.ceil(math.sqrt(n)))+1): | |
if n%i==0: | |
div += [i,n/i] | |
return div | |
def factors_for_loop_extend_operator(n): | |
# bug, feed this a perfect square and the square root | |
# will be counted twice | |
div = [1] | |
for i in xrange(2,int(math.ceil(math.sqrt(n)))+1): | |
if n%i==0: | |
div.extend([i,n/i]) | |
return div | |
def factors_generators_and_for_loop(n): | |
div = [1] | |
possible_factors = xrange(2,int(math.ceil(math.sqrt(n)))+1) | |
factor_pairs = ((i, n/i) for i in possible_factors if n%i == 0) | |
# [x for b in a for x in b] | |
# takes every list in a, and extracts every element in them | |
div = div + [factor for pair in factor_pairs for factor in pair] | |
#for pair in factor_pairs: | |
# div += pair | |
return div | |
def factors_uglymess(n): | |
div = [1] | |
# [x for b in a for x in b] | |
# takes every list in a, and extracts every element in them | |
div = div + [factor for pair in ((i, n/i) for i in xrange(2,int(math.ceil(math.sqrt(n)))+1) if n%i == 0) for factor in pair] | |
#for pair in factor_pairs: | |
# div += pair | |
return div | |
def factors_uglymess_generator(n): | |
# could not make a real generator. exploiting | |
# the sum() function found in the higher level code | |
div = 1 + sum(factor for pair in ((i, n/i) for i in xrange(2,int(math.ceil(math.sqrt(n)))+1) if n%i == 0) for factor in pair) | |
return [div] | |
from time import time | |
def test(n,f): | |
start = time() | |
a = [isAbundant(i,f) for i in range(n)] | |
return (time()-start,f.__name__) | |
def pc_isAbundant(n,f): #make this run fast | |
div_sum = f(n) | |
return True if div_sum>n else False | |
def pc_factors_for_loop_plus_operator(n): | |
# bug, feed this a perfect square and the square root | |
# will be counted twice | |
div = [1] | |
for i in xrange(2,int(math.ceil(math.sqrt(n)))+1): | |
if n%i==0: | |
div += [i,n/i] | |
return sum(div) | |
def pc_factors_for_loop_extend_operator(n): | |
# bug, feed this a perfect square and the square root | |
# will be counted twice | |
div = [1] | |
for i in xrange(2,int(math.ceil(math.sqrt(n)))+1): | |
if n%i==0: | |
div.extend([i,n/i]) | |
return sum(div) | |
def pc_factors_generators_and_for_loop(n): | |
div = [1] | |
possible_factors = xrange(2,int(math.ceil(math.sqrt(n)))+1) | |
factor_pairs = ((i, n/i) for i in possible_factors if n%i == 0) | |
# [x for b in a for x in b] | |
# takes every list in a, and extracts every element in them | |
div = div + [factor for pair in factor_pairs for factor in pair] | |
#for pair in factor_pairs: | |
# div += pair | |
return sum(div) | |
def pc_factors_uglymess(n): | |
div = [1] | |
# [x for b in a for x in b] | |
# takes every list in a, and extracts every element in them | |
div = div + [factor for pair in ((i, n/i) for i in xrange(2,int(math.ceil(math.sqrt(n)))+1) if n%i == 0) for factor in pair] | |
#for pair in factor_pairs: | |
# div += pair | |
return sum(div) | |
def pc_factors_uglymess_generator(n): | |
# could not make a real generator. exploiting | |
# the sum() function found in the higher level code | |
div = 1 + sum(factor for pair in ((i, n/i) for i in xrange(2,int(math.ceil(math.sqrt(n)))+1) if n%i == 0) for factor in pair) | |
return div | |
def pc_straight_sum(n): | |
# bug, feed this a perfect square and the square root | |
# will be counted twice | |
div_sum = 1 | |
for i in xrange(2,int(math.ceil(math.sqrt(n)))+1): | |
if n%i==0: | |
div_sum += i + n/i | |
return div_sum | |
def pc_test(n,f): | |
start = time() | |
a = [pc_isAbundant(i,f) for i in range(n)] | |
return (time()-start,f.__name__) | |
scale = 2**13 | |
#""" | |
print "%f for %s"%test(scale,factors_for_loop_plus_operator) | |
print "%f for %s"%test(scale,factors_for_loop_extend_operator) | |
print "%f for %s"%test(scale,factors_generators_and_for_loop) | |
print "%f for %s"%test(scale,factors_uglymess) | |
print "%f for %s"%test(scale,factors_uglymess_generator) | |
#""" | |
#""" | |
print "precalc results" | |
print "%f for %s"%pc_test(scale,pc_factors_for_loop_plus_operator) | |
print "%f for %s"%pc_test(scale,pc_factors_for_loop_extend_operator) | |
print "%f for %s"%pc_test(scale,pc_factors_generators_and_for_loop) | |
print "%f for %s"%pc_test(scale,pc_factors_uglymess) | |
print "%f for %s"%pc_test(scale,pc_factors_uglymess_generator) | |
print "%f for %s"%pc_test(scale,pc_straight_sum) | |
#""" | |
n = 123 | |
""" | |
print factors_for_loop_plus_operator(n) | |
print factors_for_loop_extend_operator(n) | |
print factors_generators_and_for_loop(n) | |
print factors_uglymess(n) | |
print factors_uglymess_generator(n) | |
#""" | |
""" | |
print sum(factors_for_loop_plus_operator(n)) | |
print sum(factors_uglymess(n)) | |
print sum(factors_uglymess_generator(n)) | |
#""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment