Problem 91

completed January 23, 2012

Comments

This one felt really similar to the “find all the rectangles in this box” problem. I simply made an algorithm to make all possible triangles, used the Pythagorean Theorem to find which were right triangles, and weeded out duplicates and dumb ones like (0,0) and stuff like that. Took the script just 14 seconds to run!

Code

bound = 50

def isright((x1, y1), (x2, y2)):
	a = (x1**2 + y1**2)
	b = (x2**2 + y2**2)
	c = ((x1-x2)**2 + (y1-y2)**2)
	if (a + b == c) or (b + c == a) or (c + a == b):
		return True
	else:
		return False


count = 0
tested = set()
for x1 in range(bound+1):
	for y1 in range(bound+1):
		for x2 in range(bound+1):
			for y2 in range(bound+1):
				if (isright((x1, y1), (x2, y2)) == True) and (((x2, y2), (x1, y1)) not in tested) and ((x1, y1) != (0,0)) and ((x2, y2) != (0,0)) and ((x1, y1) != (x2, y2)):
					count += 1
					tested.add(((x1, y1), (x2, y2)))

print count