project-euler/203_squarefree_binomial.py

39 lines
888 B
Python

#!/usr/bin/env python3
from operator import mul
from functools import reduce
def factorize(bound):
sieve = [[] for i in range(bound)]
for n in range(2, bound):
if sieve[n] == []:
sieve[n] = [n]
for i in range(2, n+1):
if i*n >= bound:
break
sieve[i*n] = sieve[i] + sieve[n]
return sieve
def bin(row, col, fnums):
num = [f for a in fnums[row-col+1:row+1] for f in a]
den = [f for a in fnums[1:col+1] for f in a]
for f in set(num):
if num.count(f) - den.count(f) > 1:
return 0
return reduce(mul, num) // reduce(mul, den)
rn = 51
fnums = [[1] + i for i in factorize(rn)]
t = 0
dup = []
for row in range(2, rn):
for col in range(1, row//2+1):
n = bin(row, col, fnums)
if n in dup:
continue
t += n
dup.append(n)
print(t+1)