RSS
 

Posts Tagged ‘projecteuler’

ProjectEuler Problem 10

07 Dec

Find the sum of all the primes below two million.

my code:

#!/usr/bin/env python
firstPrime = 5
topCandidate = 2000000

primeList = []
candidate = 5
inc = 2
while(candidate <= topCandidate):
    position = 1
    thisPrime = firstPrime
    prime = 1
    while(thisPrime * thisPrime <= candidate):
	if(candidate % thisPrime == 0):
	    prime = 0
	    break
	thisPrime = primeList[position]
	position += 1
    if(prime):
	primeList.append(candidate)
    candidate = candidate + inc
    if(inc == 2):
	inc = 4
    else:
	inc = 2
print sum(primeList) + 5

素数是真锻炼人阿..

 
No Comments

Posted in scripts

 

ProjectEuler Problem 8

07 Dec

PE LINK

and.. my code:

#!/usr/bin/env python
f = open('bignumber', 'r')
bignumber = f.read().strip()
li = []
product = []
productNumber = 1
for i in range(len(bignumber)):
    for j in range(5):
	if bignumber[j + i] == '0':
	    break
	li.append(bignumber[j + i])
    for x in li:
	productNumber = productNumber * int(x)
    product.append(productNumber)
    productNumber = 1
    li = []

print 'product = ', max(product)

很多天没有去 PE 了。。自己还是差太多阿.. 唉……

 
No Comments

Posted in scripts