예제 #1
0
def composite(limit=10 ** 3):
    numbers = set([_ for _ in range(1, limit) if _ % 2 == 1])

    primes = set(primes_sieve(limit))

    for _ in range(0, limit):
        for p in primes:
            n = int(p + 2 * _ ** 2)
            if n in numbers:
                numbers.remove(n)

    return numbers
예제 #2
0
def composite(limit=10**3):
    numbers = set([_ for _ in range(1, limit) if _ % 2 == 1])

    primes = set(primes_sieve(limit))

    for _ in range(0, limit):
        for p in primes:
            n = int(p + 2 * _**2)
            if n in numbers:
                numbers.remove(n)

    return numbers
예제 #3
0
def consecutive_primes(count=2):

  memo = list()
  primes = set(primes_sieve())
  uniq_primes = list()

  for n in range(0, 10**6):
      if len(memo) == count:
          return memo

      factors = Counter(prime_factors(n))

      if len(set(factors)) == count and factors not in uniq_primes:
          memo.append(n)
          uniq_primes.append(factors)
      else:
          memo = list()
          uniq_primes = [factors]
예제 #4
0
def truncate_primes(limit=10**6):
    primes = set(primes_sieve(limit))
    memo = list()

    for prime in primes:

        prime_string = str(prime)
        front = prime_string
        back = prime_string
        if len(prime_string) < 2:
            continue

        for _ in range(len(prime_string) - 1):
            back = back[:-1]
            front = front[1:]

            if int(back) not in primes or int(front) not in primes:
                break
        else:
            memo.append(prime)

    return memo
예제 #5
0
def truncate_primes(limit=10**6):
    primes = set(primes_sieve(limit))
    memo = list()

    for prime in primes:

        prime_string = str(prime)
        front = prime_string
        back = prime_string
        if len(prime_string) < 2:
            continue

        for _ in range(len(prime_string)-1):
            back = back[:-1]
            front = front[1:]

            if int(back) not in primes or int(front) not in primes:
                break
        else:
           memo.append(prime)

    return memo
예제 #6
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 utils.divisors import primes_sieve, is_prime
from collections import Counter

def pandigital_primes(primes):
    memo = list()

    print('starting')

    for prime in primes:
        counter = Counter(str(prime))
        v = Counter([str(_) for _ in range(1, len(str(prime))+1)])
        if counter == v:
            memo.append(prime)

    return memo


if __name__ == "__main__":
    answer = pandigital_primes(primes_sieve(10**7))
    print(answer[-1])
예제 #7
0
What 12-digit number do you form by concatenating the three terms in this sequence?
'''

from utils.divisors import primes_sieve, is_prime
from collections import defaultdict, Counter

def same_permutation(a, b):
    d = defaultdict(int)
    for x in a:
        d[x] += 1
    for x in b:
        d[x] -= 1
    return not any(d.values())

if __name__ == "__main__":
    primes = [ p for p in primes_sieve(10000) if len(str(p)) == 4]

    memo = dict()
    for p in primes:
        for item, value in memo.items():
            if same_permutation(str(p), str(item)):
                memo[item] = memo[item] + [p]
        else:
            memo[p] = [p]

    primes = [sorted(vs, reverse=True) for vs in memo.values()]

    c = primes

    for p in primes:
      memo = defaultdict(int)
예제 #8
0
'''
The prime 41, can be written as the sum of six consecutive primes:

  41 = 2 + 3 + 5 + 7 + 11 + 13
  This is the longest sum of consecutive primes that adds to a prime below one-hundred.

  The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.

  Which prime, below one-million, can be written as the sum of the most consecutive primes?
'''

from utils.divisors import primes_sieve, is_prime
from collections import defaultdict, Counter

if __name__ == "__main__":
    primes = primes_sieve()
    prime_set = set(primes)
    largest_prime_possible = 0

    prime_sums = [0]

    for p in primes:
        prime_sums.append(prime_sums[-1] + p)
        if prime_sums[-1] > 10**6:
            break

    count = 0

    for i in range(len(prime_sums)):
        for i_2 in range(len(prime_sums) - 1, i + count, -1):
            n = prime_sums[i_2] - prime_sums[i]
예제 #9
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 utils.divisors import primes_sieve, is_prime
from collections import Counter


def pandigital_primes(primes):
    memo = list()

    print('starting')

    for prime in primes:
        counter = Counter(str(prime))
        v = Counter([str(_) for _ in range(1, len(str(prime)) + 1)])
        if counter == v:
            memo.append(prime)

    return memo


if __name__ == "__main__":
    answer = pandigital_primes(primes_sieve(10**7))
    print(answer[-1])