from lib_prime import primes from math import sqrt def pascal_numbers_till(row): def pascal_next(xs): r = [xs[0]] for i in range(len(xs) - 1): r.append(xs[i] + xs[i + 1]) r.append(xs[-1]) return r all = set([1]) xs = [1] for _ in range(row - 1): xs = pascal_next(xs) all |= set(xs) return sorted(list(set(all))) def is_square_free(ps, n): for p in ps: if n % p == 0: return False return True def solve(n): r = 0 pascals = pascal_numbers_till(n) ps = [p * p for p in primes(int(sqrt(max(pascals))))] for n in pascals: if is_square_free(ps, n): r += n return r def test(): assert pascal_numbers_till(1) == [1] assert pascal_numbers_till(2) == [1] assert pascal_numbers_till(3) == [1, 2] assert pascal_numbers_till(8) == [1, 2, 3, 4, 5, 6, 7, 10, 15, 20, 21, 35] assert solve(8) == 105 def euler_203(): test() return solve(51) if __name__ == "__main__": solution = euler_203() print("e203.py: " + str(solution)) assert solution == 34029210557338