예제 #1
0
파일: e037.py 프로젝트: correl/euler
def is_truncatable(prime):
    if prime < 10:
        return False
    rtl = [int(str(prime)[:i]) for i in xrange(len(str(prime))) if i > 0 and i < len(str(prime))]
    ltr = [int(str(prime)[i:]) for i in xrange(len(str(prime))) if i > 0 and i < len(str(prime))]
    truncated = sorted(rtl + ltr, reverse=True)
    for n in truncated:
        if not is_prime(n):
            return False
    print 'OK', prime, rtl, ltr
    return True
예제 #2
0
파일: e041.py 프로젝트: correl/euler
def pandigital_prime_generator(n):
  if n > 7:
    raise Exception('Invalid pandigital prime length')
  for end in [3, 7, 9]:
    digits = range(1, n + 1)
    if end not in digits:
      continue
    digits.remove(end)
    for start in sorted(permutations(digits, len(digits)), reverse=True):
      number = int(''.join([str(i) for i in start] + [str(end)]))
      if is_prime(number):
        yield number
예제 #3
0
def main():
    size = 1
    diagonals = 0
    primes = 0
    for corners in spiral_corners_generator():
        diagonals = diagonals + len(corners)
        primes = primes + len([c for c in corners if is_prime(c)])
        pct = primes / float(diagonals) * 100
        if size > 7 and pct < 10.0:
            break
        size = size + 2
        
    print 'Side Length: {0}, Percentage: {1}'.format(size, pct)
예제 #4
0
파일: e021.py 프로젝트: correl/euler
def main():
    MIN = 2
    MAX = 10000
    
    sums = {}
    amicable = []
    i = MIN
    while i < MAX:
        if not is_prime(i):
            s = sum(proper_divisors(i))
            sums[i] = s
            if s in sums and i == sums[s] and i != s:
                print i, s, sums[s]
                amicable.append(i)
                amicable.append(s)
        i = i + 1
    print 'Sum of amicable numbers less than {0}: {1}'.format(MAX, sum(amicable))
예제 #5
0
def main():
    MIN = 2
    MAX = 10000

    sums = {}
    amicable = []
    i = MIN
    while i < MAX:
        if not is_prime(i):
            s = sum(proper_divisors(i))
            sums[i] = s
            if s in sums and i == sums[s] and i != s:
                print i, s, sums[s]
                amicable.append(i)
                amicable.append(s)
        i = i + 1
    print 'Sum of amicable numbers less than {0}: {1}'.format(
        MAX, sum(amicable))
예제 #6
0
def main():
    MAX = 1000000
    circular_primes = []
    print 'Searching for circular primes for p < {0}...'.format(MAX)
    for prime in prime_generator():
        if prime >= MAX:
            break
        try:
            # Ensure the prime *can* be circular
            if prime > 9:
                for c in [n for n in str(prime) if n not in ['1', '3', '7', '9']]:
                    raise NotCircular()
            # Check all permutations
            for rotation in cyclic_rotation(prime):
                if not is_prime(rotation):
                    raise NotCircular()
            circular_primes.append(prime)
        except NotCircular:
            pass
        # Clear all permutations from the list?
    print 'Circular Primes ({0}): {1}'.format(len(circular_primes), circular_primes)