Ejemplo n.º 1
0
 def __init__(self, m,  k = 5):      # k = num de hash functions, rel relação entre numero de palavras e tamanho do filtro
     self.k = k
     self.m = m
     self.primes = list()
     self.storage = [ 0 ] * m
     self.primes = generate_primes(self.k << 1, start = 3)
     random.shuffle(self.primes)
Ejemplo n.º 2
0
#!/usr/bin/env python3 -tt

import naive_prime_test as t
import prime_generator as pg
import collections

prime_sets = collections.defaultdict(lambda: list())
get_prime = pg.generate_primes()

primes = set()


def is_prime(x):
    if x in primes:
        return True
    elif t.is_prime(x):
        return True
    return False


def is_concatenable(y, x):
    return is_prime(int(str(y) + str(x))) and is_prime(int(str(x) + str(y)))


def is_pairwise_concatenable(set_of_length_n, prime):
    return all(is_concatenable(prime, prime2) for prime2 in set_of_length_n)


size = 5
while len(prime_sets[size]) == 0:
    next_prime = next(get_prime)
import math

# Imports time and start counting
import time
start = time.time()
print("Starting...")

# Brute force intuition:
# - Generate a list of primes using the sieve
# - Check first two elements a,b, add c if a,b is a prime pair. Add d if a,b,c are pairs, etc.
# - Make sure a < b < c < d < e to speed up
# - Calculate sum for pairs, set as max, and continue
# - On completion, max is the correct answer
# - I do assume this will be slow

list_of_primes = generate_primes(10000)
list_of_primes.remove(2)
list_of_primes.remove(5)

num_combinations = 5
checked_pairs = dict()


def concat(x, y):
    return int("%d%d" % (x, y))  # Fastest (so far)
    #return int(str(x)+str(y)) # Faster
    #return int(x*10**(1+math.floor(math.log10(y)))+y) # Slow


def check_prime(n):
    if n in list_of_primes:
# 10 	1,3,7,9 	        4 	2.5
#
# It can be seen that n=6 produces a maximum n/f(n) for n = 10.
#
# Find the value of n = 1,000,000 for which n/f(n) is a maximum.

# Imports
import math
from prime_generator import generate_primes

# Imports time and start counting
import time
start = time.time()
print("Starting...")

limit = 10**6
number = 1
# Generating primes until sqrt(limit)
primes = generate_primes(int(math.sqrt(limit)))

# Finds the number of primes that can be multiplied before the number is above limit
for prime in primes:
    if number * prime > limit:
        break
    number *= prime

print(number)

# Prints the time to complete
print("Done in " + str(time.time() - start) + " seconds.")
# Similarly we can work from right to left: 3797, 379, 37, and 3.
#
# Find the sum of the only eleven primes that are both truncatable
# from left to right and right to left.
#
# NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.

# Import primes generator
from prime_generator import generate_primes

# Imports time and start counting
import time
start = time.time()
print("Starting...")

list_of_primes = generate_primes(1000000)  # I assume it is less than a million

primes = []

for prime in list_of_primes:
    if prime > 30:
        if '0' in str(prime) or '2' in str(prime):
            pass
        elif '4' in str(prime) or '6' in str(prime) or '8' in str(prime):
            pass
        else:
            primes.append(prime)
    else:
        primes.append(prime)

primes.sort()
# We only need to search for a 7 digit (7654321) and below pandigital, as:
# Given a 9-digit pandigital number, the digits add to 45, meaning the number is divisible by 9;
# Given a 8-digit pandigital number, the digits add to 36, meaning the number is divisible by 9;
# thus, the number cannot be prime.
# ( I have no idea where that rule is from, and how it works.. only that it's there )

# Imports
from prime_generator import generate_primes

# Imports time and start counting
import time
start = time.time()
print("Starting...")

# Imports the primes
list_of_primes = generate_primes(7654321)

pandigital = [1, 2, 3, 4, 5, 6, 7, 8, 9]


def to_list(number):
    # Takes a number and returns a list of digits
    tmp_list = []
    string = str(number)
    for a in string:
        tmp_list.append(int(a))
    return tmp_list


primes = []
for x in list_of_primes: