#!/usr/bin/python # BSD Zero Clause License # # Copyright (c) 2022 Marek Krzywdziński # # Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT # SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. import sys from primes import primes from semiprimes import semiprimes def calculate_parameters(s): scope = 0 strategy = int(s) print('Calculating strategy %s' % strategy) prime_iterator = iter(primes) prime = next(prime_iterator) left_prime = None right_prime = None allowed_primes = [] fields = {} found_all_semiprimes = False while not found_all_semiprimes: allowed_primes.append(prime) for left_prime in allowed_primes: for right_prime in [prime]: fields.setdefault(left_prime, {}) fields[left_prime][right_prime] = {'primes': (right_prime, left_prime)} for left_prime in allowed_primes: for right_prime in allowed_primes: if left_prime <= right_prime: fields[left_prime][right_prime].update({'semiprime': fields[left_prime][right_prime]['primes'][0] * fields[left_prime][right_prime]['primes'][1]}) if fields[left_prime][right_prime]['semiprime'] <= strategy: fields[left_prime][right_prime].update({'allowed': True}) else: fields[left_prime][right_prime].update({'allowed': False}) semiprime_iterator = iter(semiprimes) semiprime = next(semiprime_iterator) found_all_semiprimes = True while semiprime <= strategy: found_semiprime = False for left_prime in allowed_primes: for right_prime in allowed_primes: if left_prime <= right_prime: if fields[left_prime][right_prime]['allowed'] and fields[left_prime][right_prime]['semiprime'] == semiprime: found_semiprime = True break if not found_semiprime: print('Progress: %s%%' % str(round((semiprime / strategy) * 100, 2))) found_all_semiprimes = False break semiprime = next(semiprime_iterator) prime = next(prime_iterator) scope += 1 step_for_semiprimes = len(str(fields[left_prime][right_prime]['semiprime'])) pattern = '' initial_semiprimes = '1' previous_disallowed = None occurrences_in_a_row = 0 last_three_maximum_occurrences = [] for left_prime in allowed_primes: for right_prime in allowed_primes: if left_prime <= right_prime: initial_semiprimes += str(fields[left_prime][right_prime]['semiprime']).zfill(step_for_semiprimes) if fields[left_prime][right_prime]['allowed']: pattern += '1' if occurrences_in_a_row > 0 and occurrences_in_a_row > max(last_three_maximum_occurrences, default=0): if len(last_three_maximum_occurrences) == 3: last_three_maximum_occurrences.pop(0) last_three_maximum_occurrences.append(occurrences_in_a_row) occurrences_in_a_row = 0 previous_disallowed = False else: pattern += '0' if previous_disallowed: occurrences_in_a_row += 1 if len(last_three_maximum_occurrences) == 3 and occurrences_in_a_row > sum(last_three_maximum_occurrences): break previous_disallowed = True else: continue break pattern = pattern.rstrip('0') print('scope: %s' % hex(scope)) print('initial_semiprimes: %s' % hex(int(initial_semiprimes))) print('step for semiprimes: %s' % hex(int(step_for_semiprimes))) print('pattern: %s' % hex(int(str(pattern), 2))) calculate_parameters(sys.argv[1])