Example #1
0
def main():
	n = 3
	while True:
		if isPrime(n-1): primes.append(n-1)
		ways = computeWays(n)
		if ways > 5000:
			print n
			break
		n += 1
Example #2
0
def getDiagonalNumbers(step):
	global diagonals, ratio
	rightbottom = (2 * step + 1) ** 2
	leftbottom = (2 * step + 1) ** 2 - (2 * step) 
	lefttop = (2 * step + 1) ** 2 - 2 * (2 * step)
	righttop = (2 * step + 1) ** 2 - 3 * (2 * step)

	newnumber = [rightbottom, leftbottom, lefttop, righttop]
	diagonals += newnumber

	for i in newnumber:
		if isPrime(i):
			ratio[0] += 1
	ratio[1] += 4	
Example #3
0
3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8

It can be seen that there are 21 elements in this set.

How many elements would be contained in the set of reduced proper 
fractions for d <= 1,000,000?
"""

# Euler totient function phi(n):
# counts the number of positive integers less than or equal 
# to n that are relatively prime to n

# phi(p) = p -1, for prime number p
# phi(n) = n * (1-1/p1) * (1- 1/p2) *...*(1-1/pk)
#			p1,p2,..pk are prime factors of n

# this problem equals to: find the sum of phi(2), phi(3), ...phi(1000000)

from Helper import isPrime
limit = 1000001
totients = range(limit) # [0,1,2,..,1000000]

for i in range(2, limit): # for (i=2; i<limit; i++)
	if isPrime(i):
		totients[i] = i - 1;
		for j in range(2*i, limit, i):
			totients[j] *= (1.0 -  1.0 / i)

print sum(totients) - 1

Example #4
0
def isPrimeOnCombination(a, b):
    return (isPrime(int(str(a)+str(b))) == True and \
    isPrime(int(str(b)+str(a))) == True)
Example #5
0
"""
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.
"""

from Helper import isPrime


top = 2000000
sum = 0

for i in range(2, top):
	if isPrime(i) == True:
		sum += i
print sum		
Example #6
0
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.
"""
from Helper import isTruncatablePrime, isPrime

n = 11
sums = 0
count = 0

while True:
	flag = True
	for i in (0,4,6,8):
		if str(n).count(str(i)) > 0:
			flag = False
			break

	if flag == True and isPrime(n) == True:		
		if isTruncatablePrime(n):
			count += 1
			sums += n
			if count == 11:
				break

	n += 2
print sums	

Example #7
0
    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.
"""
from Helper import isPrime

maxprimecount = 0
coefficients = 0
limit = 1000

for a in range(-limit + 1, limit):
	for b in range(-limit + 1, limit):
		n = 0
		while True:
			if n**2+a*n+b > 0 :
				if isPrime((n**2+a*n+b)):
					n += 1
				else:
					break	
			else:
				break
		if 	maxprimecount < n:
			maxprimecount = n
			coefficients = a * b

print coefficients
Example #8
0
9 = 7 + 2*1^2
15 = 7 + 2*2^2
21 = 3 + 2*3^2
25 = 7 + 2*3^2
27 = 19 + 2*2^2
33 = 31 + 2*1^2

It turns out that the conjecture was false.

What is the smallest odd composite that cannot be written 
as the sum of a prime and twice a square?
"""
from Helper import isPrime, isSquare

primes = [2]
n = 3

while True:
	if n % 2 == 1 and isPrime(n) == False:
		flag = False
		for i in primes:
			if isSquare((n-i)/2):
				flag = True
				break
		if flag == False:
			print n
			break
	if isPrime(n):
		primes.append(n) 

	n += 1
Example #9
0
"""
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?
"""

from Helper import isPrime

count = 1
n = 3
while n < 1000000:
	if isPrime(n):
		digits = str(n)

		flag = True
		for i in range(len(digits)):
			digits = digits[1:] + digits[0]
			if isPrime(int(digits)) == False:
				flag = False
				break
		if flag == True:
			count += 1		
	n += 2 # step over even number 	

print count
Example #10
0
"""

The arithmetic sequence, 1487, 4817, 8147, in which each of 
the terms increases by 3330, is unusual in two ways: 
(i) each of the three terms are prime, and, 
(ii) each of the 4-digit numbers are permutations of one another.

There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, 
exhibiting this property, but there is one other 4-digit increasing sequence.

What 12-digit number do you form by concatenating the three terms in this sequence?
"""

from Helper import isPrime, isPerm

n = 1489 	# must be odd
while True:
	b, c = n+3330, n+6660
	if isPrime(n) and isPrime(b) and isPrime(c) \
	and isPerm(n,b) and isPerm(b,c): break
	n += 2
 
print str(n)+str(b)+str(c)
Example #11
0
"""
The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?
"""
from Helper import isPrime

number = 600851475143
factor = 0

for i in xrange(2, number+1):
	if number%i == 0:
		if isPrime(number/i) == True:
			print number/i
			break
			
		
Example #12
0
def isPrimeOnCombination(a, b):
    return (isPrime(int(str(a)+str(b))) == True and \
    isPrime(int(str(b)+str(a))) == True)
Example #13
0
#http://projecteuler.diandian.com/post/2013-04-25/40050564523
from Helper import isPrime


#check if two primes still prime after combination
def isPrimeOnCombination(a, b):
    return (isPrime(int(str(a)+str(b))) == True and \
    isPrime(int(str(b)+str(a))) == True)


# find primes under 100000
primes = []
for i in range(2, 10000):
    if isPrime(i):
        primes.append(i)
primes.remove(2)
primes.remove(5)
print 'found primes under 10000'

# Pre calculate the list of numbers for each prime
# which remains prime after combination. And the number
# in list must 1. be higher than give one, 2. is prime
# 3. remain prime after combination
primesCanCombine = {}
for i in primes:
    primesCanCombine[i] = []
    for j in primes[primes.index(i) + 1:]:
        if isPrimeOnCombination(i, j):
            primesCanCombine[i].append(j)
print 'finished the available combined list for each prime'
Example #14
0
def primeCount(x):
	count = 0
	for i in x:
		if isPrime(i):
			count += 1
	return count
Example #15
0
2 	1 					1 		2
3 	1,2 				2 		1.5
4 	1,3 				2 		2
5 	1,2,3,4 			4 		1.25
6 	1,5 				2 		3
7 	1,2,3,4,5,6 		6 		1.1666...
8 	1,3,5,7 			4 		2
9 	1,2,4,5,7,8 		6 		1.5
10 	1,3,7,9 			4 		2.5

It can be seen that n=6 produces a maximum n/phi(n) for n <= 10.

Find the value of n <= 1,000,000 for which n/phi(n) is a maximum.
"""

""" 
Calculation of Totient Function
http://mathworld.wolfram.com/TotientFunction.html 

"""
from Helper import isPrime
limit = 10 ** 6
primes = [i for i in range(2, 100) if isPrime(i)]
maxn = 1
for i in primes:
	maxn *= i
	if maxn > limit:
		maxn /= i
		break
print maxn			
Example #16
0
2 	1 					1 		2
3 	1,2 				2 		1.5
4 	1,3 				2 		2
5 	1,2,3,4 			4 		1.25
6 	1,5 				2 		3
7 	1,2,3,4,5,6 		6 		1.1666...
8 	1,3,5,7 			4 		2
9 	1,2,4,5,7,8 		6 		1.5
10 	1,3,7,9 			4 		2.5

It can be seen that n=6 produces a maximum n/phi(n) for n <= 10.

Find the value of n <= 1,000,000 for which n/phi(n) is a maximum.
"""

""" 
Calculation of Totient Function
http://mathworld.wolfram.com/TotientFunction.html 

"""
from Helper import isPrime
limit = 10 ** 6
primes = [i for i in range(2, 100) if isPrime(i)]
maxn = 1
for i in primes:
	maxn *= i
	if maxn > limit:
		maxn /= i
		break
print maxn			
Example #17
0
The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13

This is the longest sum of consecutive primes that adds to a prime below one-hundred.

The longest sum of consecutive primes below one-thousand that adds to a prime, 
contains 21 terms, and is equal to 953.

Which prime, below one-million, can be written as the sum of the most consecutive primes?

"""
from Helper import isPrime
limit = 1000000

primes = [i for i in range(2, limit) if isPrime(i)]
(countmax, primemax)  = (1, 2)

for i in range(len(primes)-1):
	if countmax*primes[i] > limit: continue
	sums = primes[i]
	count = 1
	for j in range(i+1, len(primes)):
		sums += primes[j]
		if j - i + 1 < countmax: continue
		if sums > limit: break
		if sums in primes: 
			count = j - i + 1
			sumtmp = sums

	if countmax < count : 
Example #18
0
What is the largest n-digit pandigital prime that exists?

This probelm bored, I wont waste time on it. So answers from 
http://blog.dreamshire.com/2009/03/26/project-euler-problem-41-solution/
Analysis

What is the value of n? We know it's between 4 and 9 
but that leaves a huge set of prime number candidates 
to search through. So let's eliminate some values of 
n by using the rule that any integer is divisible by 
3 or 9 whose sum of digits is divisible by 3 or 9; 
therefore composite and not prime.

9+8+7+6+5+4+3+2+1 = 45,
8+7+6+5+4+3+2+1 = 36,
6+5+4+3+2+1 = 21, and
5+4+3+2+1 = 15,
all of which are divisible by 3 and therefore could 
not yield a 1 to {5, 6, 8, 9} pandigital prime. So 
that leaves 4 and 7 digit prime numbers less than 
4321 and 7654321 respectively. Since we want the 
largest pandigital prime we'll check the 7 digit 
group first.

"""
from Helper import isPrime, isPandigital
 
n = 7654321
while not(isPrime(n) and isPandigital(n, 7)):
  n-=2
print "Answer to PE41 = ", n