Esempio n. 1
0
def main(argv=sys.argv):
    n = 7
    while True:
        n += 2

        prime = False
        for p in gen_primes():
            if p == n:
                prime = True
                break
            elif p > n:
                break
        if prime:
            continue

        equal = False
        for p in gen_primes():
            if not equal and p > n:
                print "The answer is %d" % n
                return 0
            pw = 1
            while True:
                if (p + power(pw)) == n:
                    equal = True
                    break
                if (p + power(pw)) >= n:
                    break
                pw += 1
            if equal:
                break
Esempio n. 2
0
def factorize(n):
    if n == 1:
        return [1]
    factors = []
    while n > 1:
        for p in gen_primes():
            if (n % p) == 0:
                factors.append(p)
                n /= p
                break
    return factors
Esempio n. 3
0
def main(argv=sys.argv):
    truncatables = []
    for prime in gen_primes():
        prime = str(prime)
        if len(prime) == 1:
            continue
        truncatable = True
        for i in range(1, len(prime)):
            lrest, rrest = int(prime[:-i]), (prime[i:])
            if int(lrest) not in primes or int(rrest) not in primes:
                truncatable = False
                break
        if truncatable:
            truncatables.append(prime)
            print prime
            if len(truncatables) == 11:
                break
    print sum(truncatables)
    return 0
Esempio n. 4
0
def main(argv=sys.argv):
    _max = 0
    for a in xrange(-999, 1000):
        for b in xrange(-999, 1000):
            count = 0
            done = False
            for n in itertools.count():
                value = n**2 + a*n + b
                for p in gen_primes():
                    if p > value:
                        done = True
                        break
                    if p == value:
                        count += 1
                        break
                if done:
                    break
            if count > _max:
                print 'max on a, b (count):', a, b, count
                _max = count
                coefficients = {'a': a, 'b': b, 'count': count}
    print 'Product is %d for coefficients %s' % (
        coefficients['a'] * coefficients['b'], coefficients)
    return 0
Esempio n. 5
0
# -*- coding: utf-8 -*-

# Find all pandigital numbers 'abcdefghij' with a sub-string
# divisibility property:
#   bcd divisible by 2
#   cde divisible by 3
#       (5, 7, 11, 13...)
#   hij divisible by 17

import sys
from pe_3 import gen_primes

digits = '0123456789'
primes = []
for prime in gen_primes():
    if prime > 17:
        break
    primes.insert(0, prime)


# Recursive generator function to get 4-digits, divisible by two numbers
def rec_divby2(so_far, rest):
    if len(so_far) == 3:
        # This branch selects the last digit
        for last in '02468':
            if last not in so_far:
                yield so_far + last
    else:
        # This branch goes for digits 1 to 3
        for i in xrange(len(rest)):
            next = so_far + rest[i]
Esempio n. 6
0
def main(argv=sys.argv):
    for p in gen_primes():
        primes.append(p)
        if p >= HOW_MANY:
            break
    print len([p for p in primes if circular(p)])