Created
December 30, 2012 19:54
-
-
Save zackmdavis/4414714 to your computer and use it in GitHub Desktop.
looking for tuples of n consecutive years with exactly n prime factors which are all distinct (turns out n>=4 is impossible because out of every four consecutive numbers, one must have 2**2 in its factorization, but I didn't realize this until the code was already written)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from subprocess import check_output | |
def factorize(n): | |
if n == 0 or n == 1: | |
return {} | |
output = check_output(["factor", str(n)]).decode('utf-8') | |
factors = list(map(int, output.split(": ")[1].split(' '))) | |
factorization = {(f, factors.count(f)) for f in set(factors)} | |
return factorization | |
def factors_all_distinct(fctn): | |
for p in fctn: | |
if p[1] != 1: | |
return False | |
return len(fctn) | |
successes = {} | |
previous_fact = None | |
year_fact = None | |
consecutive = 1 | |
for year in range(1000, 3000): | |
previous_fact = year_fact | |
year_fact = factors_all_distinct(factorize(year)) | |
if year_fact: | |
if year_fact == previous_fact: | |
consecutive += 1 | |
else: | |
consecutive = 1 | |
if year_fact >= 3 and consecutive == year_fact: | |
run = tuple(y for y in range(year - year_fact + 1, year + 1)) | |
print(run) | |
if year_fact in successes: | |
successes[year_fact].append(run) | |
else: | |
successes[year_fact] = [run] | |
else: | |
consecutive = 1 | |
print(successes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment