Skip to main content
The National Cipher Challenge

Reply To: Programming

#98022
Astralica
Participant

Question 2:

Solution: 5040
Sum of 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229.

I just checked all the 4-digit sums of 42 consecutive primes.


import math

def generate_primes(n): # Sieve of Eratosthenes
  nums = [2] + [i for i in range(3, n + 1, 2)]
  _primes = set()
  
  for i in range(3, int(n**0.5)):
    for j in range(i**2, n + 1, i):
      _primes.add(j)
  
  return list(filter(lambda x: x not in _primes, nums))

def primeSlicer(): # Finds all sums of 42 consecutive primes
    primes = generate_primes(355) # Stops at the last 4-digit num
    for i in range(len(primes) - 41):
        yield sum(primes[i: i+42])
        
def divisorCounter(num):
    counter = 0
    for i in range(1, int(num**0.5) + 1):
        if (num % i == 0): # Sorts out square numbers here.
            counter += 1 if i == num // i else 2

    return counter
    
for v in primeSlicer(): # Checks each prime number for the number of divisors
    if divisorCounter(v) == 60:
        print(v)

Report a problem