class PrimeFinder: def __init__(self): self.iteration = 0 self.primes_list = [] def calc_primes(self, n): for i in range(n - len(self.primes_list) + 1): self.next_prime() def next_prime(self): self.iteration += 1 if self.iteration < 3: self.primes_list.append(self.iteration + 1) return self.iteration + 1 while True: number = int((self.iteration - 1)/2) * 6 + ((self.iteration - 1) % 2 * 2 - 1) if self.is_prime(number): self.primes_list.append(number) return number self.iteration += 1 def is_prime(self, number): return self.long_prime_check(number) def long_prime_check(self, number): if number == 1: return False if number == 2: return True i = 0 if len(self.primes_list) == 0: self.next_prime() while self.primes_list[i] * self.primes_list[i] <= number: if number % self.primes_list[i] == 0: return False i += 1 if len(self.primes_list) <= i: self.next_prime() return True class SnowflakesFinder: def __init__(self): self.prime_finder = PrimeFinder() self.prime_finder.calc_primes(1) self.search_level = 1 self.last = 0 self.current = 0 def next_snowflake(self): is_last_prime = False is_current_prime = False is_snowflake = False gap = 1 power = 1 for i in range(self.search_level): gap *= self.prime_finder.primes_list[i] self.last -= self.last % gap self.current = self.last + gap prime_adder_list = range(gap) for i in range(self.search_level): prime_adder_list = filter(lambda x: x % self.prime_finder.primes_list[i] != 0, prime_adder_list) while not is_snowflake: prime_adder = 1 self.last = self.current self.current += gap is_last_prime = is_current_prime is_current_prime = self.prime_finder.is_prime(self.current - 1) if is_last_prime and is_current_prime: is_snowflake = True t = 0 while prime_adder_list[t] < gap - 1: if self.prime_finder.is_prime(self.last + prime_adder): is_snowflake = False break t += 1 prime_adder = prime_adder_list[t] self.search_level += 1 return self.last - 1, self.current - 1 s = SnowflakesFinder() for i in range(10): print(s.next_snowflake())