Exemple #1
0
def solve():
    even_char = re.compile("0|2|4|6|8")
    sum_all = 0
    count = 0
    for i in primes_in_range(9,1000000,2):
        s = str(i)
        if '0' in s:
            continue
#       if even_char.search(s):
#           continue
#       else:
        flag = True
        for sub in truncl(s):
            if not isprime(int(sub)):
                flag = False
                break
        if flag:
            for sub in truncr(s):
                if not isprime(int(sub)):
                    flag = False
                    break
        if flag: 
            count += 1
            sum_all += i
            print '*** PING ***   ', i
    print "Answer: first %i primes add up to %i" % (count, sum_all)
Exemple #2
0
def solve_twodigit():
    for n in xrange(99*99, 1, -1):
##        if n < 9000:
##            break
        if ispalindrome(n) and not isprime(n):
#            print 'checking',n
            for x in factors(n):
                if x <= 99 and x >= 10 and n / x > 10 and n / x < 99:
                    print n, ':', x, '*', n / x
                    break
Exemple #3
0
def solve():
    c = 0
    for n in primes_in_range(2, 1000000, 1):
        flag = True
        for r in rots(n):
            if not isprime(r):
                flag = False
                break
        if flag:
            c += 1
            print "%i (%i)" % (n, c)
Exemple #4
0
def solve():
    n = 1
    s = 0
    start = time()
    while n < 1000000:
#        if n % 10000 == 0: print n,
        if n%2 == 0 or n%3 == 0:
            n+=1
            continue
        if isprime(n):
            print n
            s += n
        n+=1
    print s
    print 'finished in %f seconds' % (time() - start)
Exemple #5
0
"""
2007-12-16
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we
can see that the 6th prime is 13.

What is the 10001st prime number?
"""
import psyco
psyco.full()
from fast_prime import isprime

c = 0
n = 1
while c < 10001:
    if isprime(n):
        c += 1
    n += 1
print n-1
Exemple #6
0
    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.
"""
import psyco; psyco.full()

from fast_prime import isprime

##for n in range(80):
##  res = n**2 + (-1000 * n) - 999
##  print n, res, isprime(abs(res))

result = ((0, 0),0)
rng = 1000
for a in range(-rng,rng):
  for b in range(-rng,rng):
    n = 0
    while isprime(abs(n**2 + (a * n) + b)):
      n += 1
    if n > result[1]:
      result = (a, b), n
  if a % 50 == 0: print a, result
print
print '==== FINAL ===='
print result

##  ==== FINAL ====
##  ((-61, 971), 71)
Exemple #7
0
def check(n):
  if not isprime(n): return 1
  c = 1
  while (((10 ** c) - 1) % n) != 0 and c < n:
    c += 1
  return c
Exemple #8
0
"""
2007-12-14

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 317584931803?

    >>> for x in factors(13195):
    ...     if isprime(x):
    ...         print '%i,'%x , 

    5, 7, 13, 29,

"""

from fast_prime import factors, isprime

for x in factors(317584931803):
    if isprime(x):
        print "%i," % x,
# 1, 67, 829, 1459, 3919