예제 #1
0
파일: euler47.py 프로젝트: stefsy/euler
# Find the first four consecutive integers to have four distinct prime factors. 
#What is the first of these numbers?

import math
from eulerhelpers import genprimes

def primeFactorize(num):
	"""generates an array of prime factors of num"""
	divisors = []
	for x in primes:
		if x > num: 
			break
		if num%x == 0:
			divisors.append(x)
	return divisors

primes = genprimes(1000)

for i in range(1000000):
	if len(primeFactorize(i)) == len(primeFactorize(i+1)) == len(primeFactorize(i+2)) == len(primeFactorize(i+3)) == 4: 
		print i, i+1, i+2, i+3
		break

예제 #2
0
파일: euler46.py 프로젝트: stefsy/euler
# composite numbers are postive non-prime numbers

from eulerhelpers import genprimes
import math

def breaks_bach(num):
	for t_prime in primes:	
		#list of primes has to be in ascending order
		if t_prime > num: 
			return True 
		t_num = num - t_prime 
		if math.sqrt( t_num/2 ) % 1 == 0:
			return False
	return True

if __name__ == "__main__":
	primes = sorted(genprimes(10000))
	n = 3
	while True: 
		if n in primes: 
			pass
		if breaks_bach(n) is True:
			print n , "breaks Goldbach's conjecture."
			break
		else: 
			n += 2
		#don't go into an infinite loop
		if n > 10000:
			print "max n"
			break
예제 #3
0
파일: euler49.py 프로젝트: stefsy/euler
import math
from eulerhelpers import genprimes, digitize
from itertools import permutations, combinations

# returns tuple of triples
def arithmetic(p):
	for combine in combinations(p,2):
		tempTerm = math.fabs(combine[0]-combine[1])
		if tempTerm != 0:
			if max(combine[0],combine[1]) + tempTerm in p:
				return combine[0], combine[1], max(combine[0],combine[1]) + tempTerm
			if min(combine[0],combine[1]) - tempTerm in p:
				return combine[0], combine[1], min(combine[0],combine[1]) - tempTerm 
	pass 

eligibleSet = set(genprimes(10000)) - set(genprimes(1000))
ineligible = set()
triple = []

for prime in eligibleSet:
	if prime in ineligible:
		pass
	else: 
		permuter = permutations(digitize(prime)) 
		tSeq = []
		for permute in permuter:
			temp = permute[0] * 10**3 + permute[1] * 10**2 + permute[2] * 10 + permute[3]
			if temp in eligibleSet:
				tSeq.append(temp)
		# if there exists an arithmetic sequence, put all the combinations into the ineligable set to avoid repeats
		if arithmetic(tSeq):
예제 #4
0
파일: euler41.py 프로젝트: stefsy/euler
# We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.

# What is the largest n-digit pandigital prime that exists?

# Digit sum trick! number is divisible by 3 if and only if the digit sum of the number is divisible by 3
# simplifies problem to either  4 digit or 7 digit prime

# generate all primes beneath 7654321
# generate all possible 7 digit pandigital combinations

# largest 4 digit prime is 4231

from eulerhelpers import genprimes, isPandigital
from itertools import permutations

if __name__ == "__main__":
    primes = genprimes(7654321)
    x = 7654321

    while True:
        if isPandigital(x) and x in primes:
            print x
            break
        x -= 1

        if x <= 1000000:
            break
예제 #5
0
파일: euler50.py 프로젝트: stefsy/euler
#Which prime, below one-million, can be written as the sum of the most consecutive primes?

from eulerhelpers import genprimes

def ssum(l):
	s = 0
	for x in l:
		s += x
	return s

upto = 1000000
primes = genprimes(upto)
streamsum = 0 
streamlen = 0 

for start in range(0,len(primes)):
	for stop in range(start+streamlen,len(primes)):
		temp = ssum(primes[start:stop])
		# print streamsum, start, stop, temp
		if temp in primes and len(primes[start:stop]) > streamlen:
			streamsum = temp
			streamlen = len(primes[start:stop])
		if temp > upto: 
			break
			
#704.9 seconds to run :(

print "prime", streamsum
print "length", streamlen