-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp091.py
More file actions
executable file
·52 lines (40 loc) · 1.41 KB
/
p091.py
File metadata and controls
executable file
·52 lines (40 loc) · 1.41 KB
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
46
47
48
49
50
51
# -*- coding: UTF-8 -*-
#The points P (x_(1), y_(1)) and Q (x_(2), y_(2)) are plotted at integer co-ordinates and are joined to the origin, O(0,0), to form ΔOPQ.
#
#There are exactly fourteen triangles containing a right angle that can be formed when each co-ordinate lies between 0 and 2 inclusive; that is,
#0 ≤ x_(1), y_(1), x_(2), y_(2) ≤ 2.
#
#Given that 0 ≤ x_(1), y_(1), x_(2), y_(2) ≤ 50, how many right triangles can be formed?
import logging
logger = logging.getLogger('p091')
class Vector:
'''2D vector'''
def __init__(self, x, y):
self.x = x
self.y = y
def minus(self, v):
return Vector(self.x-v.x, self.y-v.y)
def dot(self, v):
return self.x*v.x + self.y*v.y
def val(self):
return (self.x, self.y)
def IsRat(v1, v2):
'''does v1, v2 form a right angle triangle'''
if (v1.dot(v2) == 0): return 1
v3 = v1.minus(v2)
if (v1.dot(v3) == 0): return 1
if (v2.dot(v3) == 0): return 1
return 0
def main(args):
n = 0
m = 50
for x1 in range(m):
for y1 in range(m+1):
if (x1 == 0 and y1 == 0): continue
v1 = Vector(x1, y1)
for x2 in range(x1, m+1):
for y2 in range(m+1):
if (x1 == x2 and y2 <= y1): continue
v2 = Vector(x2, y2)
n += IsRat(v1, v2)
logger.info("answer: {}".format(n+m))