p091.py 793 Bytes
Newer Older
1 2
# 
# Solution to Project Euler problem 91
3
# Copyright (c) Project Nayuki. All rights reserved.
4
# 
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
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)


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):
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())