Example #1
0
class LongestSum:
    primes      = get_primes(1000000)
    max_value   = 1000000

    def __init__(self, index):
        self.index          = index
        self.current_sum    = 0
        self.largest_sum    = 0
        self.count          = 0
        self.largest_counts = []

    def determine_sum(self):
        added = None
        while self.current_sum < 1000000:
            self.current_sum += LongestSum.primes[self.index]
            self.count += 1
            if self.current_sum in LongestSum.primes:
                self.largest_sum = self.current_sum
                self.largest_counts.append(self.count)
                added = True
            else:
                added = False
            self.index += 1

        # Because python doesn't support a do-while loop, this loop will run 1
        # loop over the maximum value. If during that last loop, the resulting
        # current_sum is in fact a prime number, we need to remove it because
        # it's an invalid answer.
        if added: _remove_last_entries(self)

    def largest_count(self):
        return max(self.largest_counts)

    # Trying to signify a private method
    def _remove_last_entries(self):
        self.largest_sum -= LongestSum.primes[self.index - 1]
        self.largest_count.pop()
Example #2
0
 def __init__(self, count):
     self.primes = get_primes(count)
     self.factors = []
     self.index = 0
     self.final_prime = True
     self.number = LargestPrime.number
Example #3
0
 def __init__(self, count):
     self.primes         = get_primes(count)
     self.factors        = []
     self.index          = 0
     self.final_prime    = True
     self.number         = LargestPrime.number
Example #4
0
start = max(
    map(lambda path: int(path.split('-')[1].split('.')[0]),
        glob('../data/*.*prime')))
print('Loading primes...')
small_primes = read_compressed_prime_file('../data/0-1000000000',
                                          step_size)[1:]
print('INIT VALUES:', time() - loading_primes_time, end='\n\n')

for calc_round in range(1, 101):
    print('ROUND %s\n--------------------------------' % calc_round)
    start_time = time()

    part_time = start_time
    sieve = np.zeros(offset, dtype=np.bool)
    calculate_sieve[blocks, block_size](start, offset, sieve, small_primes)
    print('CALC PRIMES:', time() - part_time)

    part_time = time()
    final_primes = get_primes(start, sieve)
    print('READ PRIMES:', time() - part_time)

    part_time = time()
    # save_prime_file(start, offset, final_primes)
    save_and_compress_prime_file(start, offset, step_size, final_primes)
    print('SAVING FILE:', time() - part_time)

    print('--------------------------------\nROUND TIME : %s' %
          (time() - start_time),
          end='\n\n')
    start += offset
Example #5
0
# The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
#
# Find the sum of all the primes below two million.

from prime_generator import get_primes

print(sum(get_primes(2000000)))
Example #6
0
from prime_generator import get_primes

with open('wordlist.txt', 'r', encoding="iso-8859-1") as file:
    wordlist = [line.rstrip() for line in file]

lower_alphabet = "abcdefghijklmnopqrstuvwxyz'èóüäçéáåöñëíâôûøêîïàùú"
full_alphabet = lower_alphabet + lower_alphabet.upper()
char_to_prime_map = dict(zip(full_alphabet, get_primes(2)))


def hash(word):
    total = 1
    for char in word:
        total = total * char_to_prime_map[char]
    return total


def build_hash_map():
    hash_map = {}
    for word in wordlist:
        try:
            hash_map[hash(word)].append(word)
        except KeyError:
            hash_map[hash(word)] = [word]
    return hash_map


def create_anagram_list():
    anagrams = []
    hash_map = build_hash_map()
    for key in hash_map.keys():