-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojeul_pr91.py
More file actions
50 lines (39 loc) · 1.76 KB
/
Copy pathprojeul_pr91.py
File metadata and controls
50 lines (39 loc) · 1.76 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
problem = """Problem 91: Right triangles with integer coordinates"""
explanation = """\
There are three distinct cases for a right triangle OPQ with non-negative
integer coordinates, where P = (p1, p2) and Q = (q1, q2) such that p1<=q1,
p2>=q2 and 0<=p1,p2,q1,q2<=n:
1. O == right angle, it is easy to calculate that there exist n**2
right triangles with these specifications.
2. P == right angle. Therefore we obtain
OP**2 + PQ**2 = OQ**2 <=> p1**2 + p2**2 - p1*q1 - p2*q2 = 0
We can count the triangles in this case by finding all the
solutions of this equation that respect the constraints.
3. Q == right angle, the number of these triangles equals the number
of right triangles in the second case because
of the x=y line symmetry.
"""
n = 50
trianglesWithOasRightAngle = n**2
trianglesWithPasRightAngle = 0
for q1 in range(0,n+1):
for p1 in range(0,q1+1):
for p2 in range(0, n+1):
for q2 in range(0, p2+1):
if p1**2 + p2**2 - p1*q1 - p2*q2 == 0:
if p1==q1 and p2==q2: continue #no triangle is formed
if p1==0 and p2==0: continue #no triangle is formed
if q1==0 and q2==0: continue #no triangle is formed
trianglesWithPasRightAngle += 1
trianglesWithQasRightAngle = trianglesWithPasRightAngle
print(problem, explanation, sep="\n\n")
input(
"Number of right triangles with non-negative integer coordinates<="
+ str(n)
+ ": "
+ str(
trianglesWithOasRightAngle
+trianglesWithPasRightAngle
+trianglesWithQasRightAngle
)
)