p091.py 793 Bytes
 1 2 ``````# # Solution to Project Euler problem 91 `````` Nayuki committed Dec 27, 2016 3 ``````# Copyright (c) Project Nayuki. All rights reserved. `````` 4 ``````# `````` Nayuki Minase committed Feb 05, 2016 5 ``````# https://www.nayuki.io/page/project-euler-solutions `````` 6 7 8 9 10 11 ``````# https://github.com/nayuki/Project-Euler-solutions # def compute(): LIMIT = 51 `````` Nayuki Minase committed Feb 27, 2016 12 13 14 15 16 17 18 `````` ans = sum(1 for x1 in range(LIMIT) for y1 in range(LIMIT) for x2 in range(LIMIT) for y2 in range(LIMIT) # For uniqueness, ensure that (x1,y1) has a larger angle than (x2,y2) if y2 * x1 < y1 * x2 and is_right_triangle(x1, y1, x2, y2)) `````` 19 20 21 `````` return str(ans) `````` Nayuki committed Jul 04, 2017 22 ``````# Tests whether the three points {(0,0), (x1,y1), (x2,y2)} form a right triangle. `````` 23 ``````def is_right_triangle(x1, y1, x2, y2): `````` Nayuki committed Jul 04, 2017 24 25 26 27 `````` a = x1**2 + y1**2 b = x2**2 + y2**2 c = (x2 - x1)**2 + (y2 - y1)**2 return (a + b == c) or (b + c == a) or (c + a == b) `````` 28 29 30 31 `````` if __name__ == "__main__": print(compute())``````