Example #1
0
def prependPrimerDigit(num):
  result = set() 
  for i in range(1,10):
    candidate = str(i)+str(num)
    if myMath.isPrime(int(candidate)):
      result.add(int(candidate))
  return result
Example #2
0
#!/usr/bin/python

import myMath

myMath.initPrimeSieve(1000000)

primes = [x for x in xrange(1000000) if myMath.isPrime(x)]

answer = 0
mostConsecutive = 0

for i in xrange(len(primes)):
  j = 1
  cursum = primes[i]
  while i+j < len(primes) and cursum < 1000000:
    cursum+=primes[i+j]
    if myMath.isPrime(cursum) and j > mostConsecutive:
      answer = cursum
      mostConsecutive = j
    j+=1

print answer
Example #3
0
	for m in range(2, top+1):
		for n in range(m, top//m+1):
			p = m*n
			if p<=top: seive[p-2] = 0
	primes = []
	for i in range(top-1):
		if seive[i] != 0: primes.append(seive[i])
	return primes

p_max = 10000 # These have to be high enough for the program to produce the correct answer
s_max = 1000 # => 10000, 100

start_tot = time()

prime_list = primes(p_max)[1:] # Remove the number 2
can_be_written = []

# Get a large list of composites that we can write in such a way
for p in prime_list:
	for s in range(1,s_max+1):
		n = p + 2*s**2
		if not isPrime(n): can_be_written.append(n)

# Get large list of odd composites and check whether each element is in the "can_be_written" list
max_comp = p_max+2*s_max**2
for n in [n for n in range(1,max_comp,2) if not isPrime(n)]: # The [...] generates odd composites
	if n not in can_be_written:
		print n
		break
print "Time taken:", time()-start_tot
Example #4
0
myMath.initPrimeSieve(1000000)
#Euler published the remarkable quadratic formula:
#
#  n^2 + n + 41
#
#  It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 40^2 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n = 41, 41^2 + 41 + 41 is clearly divisible by 41.
#
#  Using computers, the incredible formula  n^2  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^2 + 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.

maxPrimes=(0,0,0)

for a in range(-999,1000):
  for b in range(-999,1000):
    n=0
    while myMath.isPrime(abs(n*n+a*n+b)):
      n+=1
    if n > maxPrimes[0]:
      maxPrimes = (n,a,b)
      print "%d: a=%d b=%d" % maxPrimes

print "%d: a=%d b=%d" % maxPrimes
print "Answer: " + str(maxPrimes[1] * maxPrimes[2])
Example #5
0
#!/usr/bin/python

#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?

import myMath

poss = [x for x in range(1,8)]
largest = 0

while len(poss) > 2:
  perms = myMath.getAllPermutations(poss)
  for perm in perms:
    if perm > largest and myMath.isPrime(perm):
      largest = perm
  poss = poss[:-1]

print largest
Example #6
0
from myMath import isPrime

ndiag = 0
nprime = 0
l = 0 # starting layer
n = 1 # starting number is the center

ratio = 1.
while ratio >= 0.1:
	if ndiag%4 == 0: l += 1 # move to next layer every four diagonal numbers
	n += 2*l # move to next diagonal number
	ndiag += 1 # increase number of diagonals traversed
	if isPrime(n): nprime += 1 # if prime, increase their count
	if ndiag%4 == 0: # run only when a loop is complete
		ratio = nprime*1./(ndiag+1) # update ratio, include the starting "1" in the diagonal count
		print 'Last number:', n, '   Layer:', l, '   Side length:', 2*l+1, '   Ratio:', ratio
Example #7
0
maxN = 0
maxA = 0
maxB = 0

a = 0
b = 0

range = 999

def qSum(n,a,b):
	return n*n+a*n+b


if __name__ == '__main__':
	for a in xrange(-range,range):
		for b in xrange(-range,range):
			n = 0
			sum = qSum(n,a,b)
			while (sum>0) and (myMath.isPrime(sum)):
				if n > maxN:
					maxN = n
					maxA = a
					maxB = b
				n += 1
				sum = qSum(n,a,b)
				print a,b,n,sum
				

	print maxN, maxA, maxB, maxA*maxB
	
Example #8
0
#27 = 19 + 222
#33 = 31 + 212
#
#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?

import myMath

primeSieve = myMath.initPrimeSieve(1000000)
goldbachSieve = [False] * 1000000
goldbachSieve[0] = goldbachSieve[1] = True

#weeding out all prime numbers (since it must be composite) and even numbers (since it must be odd)
for x in xrange(len(goldbachSieve)):
  if myMath.isPrime(x) or x % 2 == 0:
    goldbachSieve[x] = True


def getNextPrime():
  global primeSieve
  primeSieveLen = len(primeSieve)
  i = 1 
  while i < primeSieveLen:
    if primeSieve[i]:
      yield i
    i += 1

lastPrime = 1
for prime in getNextPrime():
  x = 0
Example #9
0
def rightToLeftTest(strnum):
  for x in xrange(len(strnum)-1):
    if myMath.isPrime(int(strnum[:-x-1])) == False:
      return False
  return True
Example #10
0
import myMath

myMath.initPrimeSieve(10000)

def digits(num):
  return [int(dig) for dig in str(num)] 

def findArithmeticSequence(numSet):
  if len(numSet) >= 3:
    numSet = sorted(numSet)
    for x in numSet:
      numSetCopy = numSet[:]
      numSetCopy.remove(x)
      for y in numSetCopy:
        diff = abs(y-x)
        if y + diff in numSetCopy: 
          return (x,y,y+diff)
  return ()

answers = []

for i in xrange(1001,10000):
  perms = list(set(myMath.getAllPermutations(digits(i))))
  perms = [x for x in perms if myMath.isPrime(x) and x > 1000]
  result = findArithmeticSequence(perms)
  if result != () and result not in answers:
    print result
    answers.append(result)

Example #11
0
#coding=utf-8
# main
import myMath as mm

num = input("请输入一个整数")
mm.mSqrt(num)
mm.mAbs(num)
mm.isPrime(num)
Example #12
0
def valid_pair(x,y):
    if isPrime(int(x + y)) and isPrime(int(y + x)): return True
    return False
Example #13
0
import myMath
myMath.initPrimeSieve(1000000)

resultSieve = bytearray(1000001)

def rotateDigits(num):
  carried = num%10
  return int(str(carried) + str(int(num/10)))


for i in range(2,len(resultSieve)):
  if resultSieve[i] != 0 or any([x == '0' for x in str(i)]):
    continue

  variations = [i]
  success = myMath.isPrime(i)
  rotated = i
  for x in xrange(len(str(i))-1):
    rotated = rotateDigits(rotated)
    variations.append(rotated)
    success = success and myMath.isPrime(variations[x+1])

  for x in variations:
    resultSieve[x] = 2 if success else 1
  
answer = 0

for x in resultSieve:
  if x == 2:
    answer+=1