Beispiel #1
0
import timing
import eulerutil
import itertools
import copy
'''
Circular primes

Problem 35

The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?
'''
def rotations(n):
	results = []
	num = [str(i) for i in str(n)]
	for i in range(len(num)):
		num.insert(0,num.pop())
		results.append(''.join(list(num)))
	return results

max = 1000000
count = 0
for n in range(2,max):
	if eulerutil.isprime(n):
		if all([eulerutil.isprime(int(i)) for i in rotations(n)]):
			count += 1
		if n%10000==0: print(n)
print(count)
Beispiel #2
0
The incredible formula  n² − 79n + 1601 was discovered, which produces 80 primes for the consecutive values n = 0 to 79. The product of the coefficients, −79 and 1601, is −126479.

Considering quadratics of the form:

n² + an + b, where |a| < 1000 and |b| < 1000

where |n| is the modulus/absolute value of n
e.g. |11| = 11 and |−4| = 4
Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0.
'''

def f(n,a,b):
	return (n**2)+(a*n)+b

max = 0
for a in range(-999,1000):
	for b in range(-999,1000):
		n = 0
		while True:
			if eulerutil.isprime(f(n,a,b)):
				n+=1
			else:
				break
		if n > max:
			max = n
			prod = a*b
print(max,prod)

		
Beispiel #3
0
The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.

Find the sum of the only eleven primes that are both truncatable from left to right and right to left.

NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.

'''

def truncate(n):
	num = str(n)
	result = [num]
	for i in range(1,len(num)):
		result.append(num[:i])
		result.append(num[i:])
	return [int(x) for x in result]

count, i = 0, 11
truncatable_primes = []
while True:
	if count>=11: break
	if isprime(i):
		if all([isprime(x) for x in truncate(i)]):
			count += 1
			truncatable_primes.append(i)
	i+=1
	if i%100000==0: print(i,count)

print(truncatable_primes)
print(sum(truncatable_primes))

Beispiel #4
0
import timing

from eulerutil import isprime
from itertools import permutations

'''
Pandigital prime
Problem 41

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?

'''
digits = "123456789"
p = []
for n in range(3,10):
	for i in [''.join(list(x)) for x in permutations(digits[:n],n)]:
		if isprime(int(i)):
			p+=[i]

print(max(p))