Exemple #1
0
def extendSquareSpiral(sideLength, primeCountInDiagonales):
	edgeNumber = sideLength ** 2 
	for _ in range(4):
		edgeNumber += sideLength + 1
		if isPrime(edgeNumber):
			primeCountInDiagonales += 1
	return sideLength + 2, primeCountInDiagonales
Exemple #2
0
def phi(n):
	result = n
	if isPrime(n):
		return n - 1
	for p in getFactorization(n):
		result *= (1 - 1 / p[0])
	return result	
Exemple #3
0
def consecutivePrimesFrom(primes, start, maxvalue):
    result = []
    for i in range(start + 1, len(primes)):
        partsum = sum(primes[start : i + 1])
        if isPrime(partsum) and partsum < maxvalue:
            result.append(primes[start : i + 1])
        if partsum > maxvalue:
            return result

    return result
Exemple #4
0
def foursHasProperty(candidate, targetNumber, digitCount):
	for t in candidate[1]:
		result = []
		for i in string.digits:
			pc = int(rep(candidate[0], t, i))
			if isPrime(pc) and len(str(pc)) == digitCount:
				result.append(int(rep(candidate[0], t, i)))
		if(len(result) >= targetNumber):
			print(result)
			return True
		
	return False
Exemple #5
0
'''
Created on Jun 2, 2015


 n² + an + b = prime, where |a| < 1000 and |b| < 1000
 
 b is prime 
'''
from utilities.divisors import getPrimes, isPrime

if __name__ == '__main__':
    maximum = (1, 1, 1)
    
    for b in getPrimes(1000):
        for a in range(-999, 1000, 2):
            res = b
            n = 0
            while isPrime(res):
                n += 1
                res = n ** 2 + a * n + b
            if(maximum[-1] < n):
                print("scooby doo ", maximum[-1], n)
                maximum = (a, b, n)
            print(a, b, n)
    print(maximum) 
    print(maximum[0] * maximum[1])
Exemple #6
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 utilities.divisors import getPrimes, isPrime



def allRotations(x):
    return [str(x)[i:] + str(x)[:i] for i in range(len(str(x)))]

if __name__ == '__main__':
    print(len([x for x in getPrimes(1000000) if all(isPrime(int(rot)) for rot in allRotations(x))]))



Exemple #7
0
'''

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? 

'''
from itertools import permutations
from string import digits

from utilities.divisors import isPrime


if __name__ == '__main__':
	for to in range(9, 1, -1):
		candidates = ([int("".join(p)) for p in list(permutations([i for i in digits[1:to]]))
				if isPrime(int("".join(p)))])
		print(max(candidates) if candidates else "empty")
Exemple #8
0
def foursHasProperty(x):
	s = str(x)
	return isPermutation(x + 3330, s) and isPermutation(x + 2 * 3330, s) \
		and isPrime(x) and isPrime(x + 3330) and isPrime(x + 2 * 3330)
Exemple #9
0
def allCombisArePrime(p1, p2):
	if(len(str(p1) + str(p2)) > 8):
		print(p1, p2)
		return False
	return isPrime(int(str(p1) + str(p2))) and isPrime(int(str(p2) + str(p1)))
Exemple #10
0
def foursHasProperty(p):
    return all(isPrime(i) for i in [int(p[i:]) for i in range(1, len(p))])\
        and all(isPrime(i) for i in [int(p[:-i]) for i in range(1, len(p))])
Exemple #11
0
def foursHasProperty(x):
    for i in range(1, ceil(sqrt(x / 2))):
        print(x, i, x - 2 * i ** 2)
        if isPrime(x - 2 * i ** 2):
            return True
    return False
Exemple #12
0
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 math import sqrt, ceil
from utilities.divisors import isPrime


def foursHasProperty(x):
    for i in range(1, ceil(sqrt(x / 2))):
        print(x, i, x - 2 * i ** 2)
        if isPrime(x - 2 * i ** 2):
            return True
    return False


if __name__ == "__main__":
    i = 1
    while True:
        i = i + 2
        if not isPrime(i) and not foursHasProperty(i):
            print(i)
            break
Exemple #13
0
 def testIsPrime(self):
     self.assertEqual(False, isPrime(4))
     self.assertEqual(True, isPrime(2))
     self.assertEqual(False, isPrime(1))
     self.assertEqual(False, isPrime(0))