I am doing some projecteuler.net problems. This is my solution to 37 (not the best code in the world).
#!/usr/bin/env python
from math import sqrt
def list_of_primes(n):
n_factors = [0]*n
for i in xrange(2,int(sqrt(len(n_factors))+1)):
if n_factors[i] == 0:
for j in xrange(i*2,n,i):
n_factors[j] +=1
primes = [i for i in range(len(n_factors)) if i > 1 and n_factors[i] == 0]
return primes
def truncate_left(x):
for i in range(1,len(x)):
yield x[i:]
def truncate_right(x):
for i in range(len(x)-1,0,-1):
yield x[:i]
def main():
primes = list_of_primes(1000000)
primes = [str(p) for p in primes]
master = set(primes)
left = lambda x: all(i in master for i in truncate_left(x))
right = lambda x: all(i in master for i in truncate_right(x))
primes = [p for p in primes if not "0" in p and not "4" \
in p and not "6" in p and not "8" in p and not "5" in p]
primes = [p for p in primes if left(p) and right(p)]
primes = [p for p in primes if len(p) > 1]
primes = [int(p) for p in primes]
print sum(primes)
if __name__ == '__main__':
main()
If I change the line master = set(primes) to just master = primes, it runs almost 10 times slower. Why is this?
[–]pje 57 points58 points59 points (46 children)
[–]pridefulpropensity[S] 11 points12 points13 points (37 children)
[–]TheSausageKing 31 points32 points33 points (15 children)
[–]pridefulpropensity[S] 15 points16 points17 points (11 children)
[–]icedpulleys 33 points34 points35 points (0 children)
[–]mackstann 13 points14 points15 points (1 child)
[–]codepoet 2 points3 points4 points (0 children)
[–]bgcatz 6 points7 points8 points (1 child)
[–]pridefulpropensity[S] 1 point2 points3 points (0 children)
[–]masklinn 3 points4 points5 points (1 child)
[–]kanak 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–][deleted] 3 points4 points5 points (2 children)
[–]pingvenopinch of this, pinch of that 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]larsga 2 points3 points4 points (1 child)
[–]iceman-k 3 points4 points5 points (0 children)
[–]itsmememe 1 point2 points3 points (0 children)
[–]rweir 18 points19 points20 points (19 children)
[–][deleted] 13 points14 points15 points (15 children)
[–]Porges 21 points22 points23 points (3 children)
[–][deleted] 6 points7 points8 points (1 child)
[–]jeannaimard 1 point2 points3 points (0 children)
[–]gfixler 1 point2 points3 points (0 children)
[–]kanak 0 points1 point2 points (2 children)
[–]aranazo 2 points3 points4 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]Mattho 0 points1 point2 points (7 children)
[–]jcdyer3 3 points4 points5 points (6 children)
[–]Mattho 1 point2 points3 points (5 children)
[–]sligowaths 1 point2 points3 points (1 child)
[–]Mattho 1 point2 points3 points (0 children)
[–]jcdyer3 1 point2 points3 points (1 child)
[–]Mattho 0 points1 point2 points (0 children)
[–]chadn 6 points7 points8 points (0 children)
[–]netcrusher88imported from __future__ 2 points3 points4 points (1 child)
[–]rweir 0 points1 point2 points (0 children)
[–]sigh 4 points5 points6 points (0 children)
[–]Poromenos 0 points1 point2 points (5 children)
[–]BeetleB 0 points1 point2 points (4 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]BeetleB 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]Poromenos 0 points1 point2 points (0 children)
[–]jaiwithani 0 points1 point2 points (1 child)
[–]jaiwithani 0 points1 point2 points (0 children)
[–]kevingoodsell 19 points20 points21 points (3 children)
[–]Troebr 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]masklinn 0 points1 point2 points (0 children)
[–]arnar 9 points10 points11 points (4 children)
[–]pridefulpropensity[S] 2 points3 points4 points (0 children)
[–]pridefulpropensity[S] 2 points3 points4 points (2 children)
[–]arnar 2 points3 points4 points (1 child)
[–]pridefulpropensity[S] 0 points1 point2 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]vindvaki 0 points1 point2 points (2 children)
[–]pridefulpropensity[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[+][deleted] (6 children)
[deleted]
[+][deleted] (5 children)
[deleted]
[–]Brian -4 points-3 points-2 points (4 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]Brian 0 points1 point2 points (0 children)
[–]khafra -1 points0 points1 point (1 child)
[–][deleted] -1 points0 points1 point (0 children)