-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathsolution_1.py
More file actions
45 lines (32 loc) · 918 Bytes
/
solution_1.py
File metadata and controls
45 lines (32 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
# coding=utf-8
# Python Script
#
# Copyleft © Manoel Vilela
#
#
from __future__ import print_function
"""
Special Pythagorean triplet
Problem 9
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a² + b² = c²
For example, 3² + 4² = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
"""
def decompSum(n):
from itertools import combinations
m = (x for x in range(1, n // 2))
div = [3, 4, 5]
comb = combinations((x for x in m if any(d for d in div if not x % d)), 3)
for a, b, c in comb:
if a + b + c == n and a != b != c:
yield sorted((a, b, c))
def pythagorean(a, b, c):
return (a ** 2 + b ** 2) == c ** 2
def problem9(n):
for a, b, c in decompSum(n):
if pythagorean(a, b, c):
return a * b * c
print(problem9(1000))