Exemple #1
0
def run():
	pr = primes.populatePrimes( 3162 ) #3162 ~= sqrt of (10000000)
	def prime(n, primes):
		isPrime = True
		index = 0
		p = primes[index]
		limit = int(math.sqrt(n)) + 1
		while p < limit:
			if n % p == 0:
				isPrime = False
				break
			else:
				index += 1
				p = primes[ index ]
		return isPrime
	panPrimes = []
	for i in list(itertools.permutations('1234567')):
		s = ''
		for letter in i:
			s += letter
		num = int(s)
		if num%2 == 0:
			continue
		else:
			if prime(num, pr):
				panPrimes.append(num)
	return max(panPrimes)
Exemple #2
0
def run():
    p = primes.populatePrimes(1000000)
    pSet = set(p)

    # returns true if first digit is 2, 3 or 7
    def firstDigit(n):
        first = int(str(n)[0])
        return first == 2 or first == 3 or first == 7 or first == 5

    # returns true if last digit is 3 or 7
    def lastDigit(n):
        return n % 10 == 3 or n % 10 == 7

    # return true if its prime for all "right truncations"
    def rightPrime(n):
        if n / 10 == 0 and n in pSet:
            return True
        else:
            trunc = n / 10
            return trunc in pSet and rightPrime(trunc)

    # return true if its prime for all "left truncations"
    def leftPrime(n):
        if n / 10 == 0 and n in pSet:
            return True
        else:
            trunc = int(str(n)[1:])
            return trunc in pSet and leftPrime(trunc)

    count = 0
    answers = []
    exempt = set([2, 3, 5, 7])
    for p in pSet:
        if not firstDigit(p) and not lastDigit(p):
            continue
        else:
            if rightPrime(p) and leftPrime(p) and p not in exempt:
                count += 1
                answers.append(p)
    print answers
    print sum(answers)
Exemple #3
0
import primes

p4 = [ p for p in primes.populatePrimes(10000) if p/1000 > 1 ]

map = {}

for p in p4:
	key = list(str(p))
	key.sort()
	dkey = reduce(lambda x, y: x + y, key)
	if dkey in map:
		map[dkey].append(p)
	else:
		map[dkey] = [p]
		
count = 0
for k, v in map.iteritems():
	v.sort()
	
for k,v in map.iteritems():
	if len(v) >= 3:
		if len(v) == 3:
			if 2*v[1] == v[0] + v[2]:
				print "THE ANSWER!!!!!!!!!!!!!! is %d, %d, %d" %(v[0], v[1], v[2])
		else:
			found = False
			s = {}
			for p1 in v:
				for p2 in v:	
					if p1-p2 in s and p1 != p2 and p1 == s[p1-p2][1]:
						print "%d, %d. And %d, %d" %(s[p1-p2][0], s[p1-p2][1], p1, p2)
Exemple #4
0
import primes

p = primes.populatePrimes(1000)[3:]
max = (0, 0)
for prime in p:
    count = 1
    divis = False
    while not divis:
        #print "We are on prime %d and count %d" %(prime, count)
        if ((10**count - 1) % prime) == 0:
            divis = True
        else:
            count += 1
    if count > max[1]:
        max = (prime, count)

print max
Exemple #5
0
import primes

p4 = [p for p in primes.populatePrimes(10000) if p / 1000 > 1]

map = {}

for p in p4:
    key = list(str(p))
    key.sort()
    dkey = reduce(lambda x, y: x + y, key)
    if dkey in map:
        map[dkey].append(p)
    else:
        map[dkey] = [p]

count = 0
for k, v in map.iteritems():
    v.sort()

for k, v in map.iteritems():
    if len(v) >= 3:
        if len(v) == 3:
            if 2 * v[1] == v[0] + v[2]:
                print "THE ANSWER!!!!!!!!!!!!!! is %d, %d, %d" % (v[0], v[1],
                                                                  v[2])
        else:
            found = False
            s = {}
            for p1 in v:
                for p2 in v:
                    if p1 - p2 in s and p1 != p2 and p1 == s[p1 - p2][1]:
Exemple #6
0
import primes

# find a*b for which n^2 + an + b generates the most consecutive primes
# b is a prime
primesB = primes.populatePrimes(1000)
primes = set(primes.populatePrimes(10000))

aList = [n for n in range(-999, 1000) if n % 2 != 0]
answers = {}  #key = number of consec primes generated, val = a*b
for a in aList:
    for b in primesB:
        quad = lambda x: x**2 + a * x + b
        composite = False
        num = 0
        while not composite:
            if quad(num) not in primes:
                composite = True
            else:
                num += 1
        #answers[a*b] = num
        answers[num] = (a, b)

print max(answers)
print answers[max(answers)]
Exemple #7
0
import primes

p = primes.populatePrimes(1000)[3:]
max = (0, 0)
for prime in p:
	count = 1
	divis = False
	while not divis:
		#print "We are on prime %d and count %d" %(prime, count)
		if ((10**count - 1) % prime) == 0:
			divis = True
		else:
			count += 1
	if count > max[1]:
		max = (prime, count)
		
print max