-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_pythagorean_tuples.py
120 lines (98 loc) · 5.77 KB
/
test_pythagorean_tuples.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import math
import unittest
from random import randint
from pythagorean_tuples import pythagorean_triples
def is_pythagorean_triple(t):
return t[0] ** 2 + t[1] ** 2 == t[2] ** 2
def is_primitive(t):
return math.gcd(t[0], t[1]) == 1 and math.gcd(t[1], t[2]) == 1 and math.gcd(t[2], t[0]) == 1
class TestPythagoreanTriples(unittest.TestCase):
def test_not_integer(self):
self.assertRaises(TypeError, pythagorean_triples, 3.14, "TypeError has not been raised")
def test_not_positive(self):
self.assertRaises(ValueError, pythagorean_triples, 0, "ValueError has not been raised")
def test_number_AP(self):
self.assertEqual(set(), pythagorean_triples(2, True))
self.assertEqual({(4, 3, 5)}, pythagorean_triples(4, True))
self.assertEqual({(32, 255, 257)}, pythagorean_triples(32, True))
self.assertEqual({(512, 65535, 65537)}, pythagorean_triples(512, True))
def test_number_A(self):
self.assertEqual(set(), pythagorean_triples(2))
self.assertEqual({(4, 3, 5)}, pythagorean_triples(4))
self.assertEqual({(32, 24, 40), (32, 60, 68), (32, 126, 130), (32, 255, 257)}, pythagorean_triples(32))
self.assertEqual({(512, 384, 640), (512, 960, 1088), (512, 2016, 2080), (512, 4080, 4112), (512, 8184, 8200),
(512, 16380, 16388), (512, 32766, 32770), (512, 65535, 65537)}, pythagorean_triples(512))
def test_random_AP(self):
for _ in range(10):
a = 2 ** randint(15, 50)
primitive_triples = pythagorean_triples(a, True)
for t in primitive_triples:
self.assertTrue(is_pythagorean_triple(t), f"{t} is not Pythagorean triple")
self.assertTrue(is_primitive(t), f'{t} is not primitive')
def test_random_A(self):
for _ in range(10):
a = 2 ** randint(15, 50)
triples = pythagorean_triples(a)
for t in triples:
self.assertTrue(is_pythagorean_triple(t), f"{t} is not Pythagorean triple")
def test_number_BP(self):
self.assertEqual({(3, 4, 5)}, pythagorean_triples(3, True))
self.assertEqual({(13, 84, 85)}, pythagorean_triples(13, True))
self.assertEqual({(271, 36720, 36721)}, pythagorean_triples(271, True))
self.assertEqual({(121, 7320, 7321)}, pythagorean_triples(121, True))
self.assertEqual({(153, 104, 185), (153, 11704, 11705)}, pythagorean_triples(153, True))
self.assertEqual({(235, 1092, 1117), (235, 27612, 27613)}, pythagorean_triples(235, True))
def test_number_B(self):
self.assertEqual({(3, 4, 5)}, pythagorean_triples(3))
self.assertEqual({(13, 84, 85)}, pythagorean_triples(13))
self.assertEqual({(271, 36720, 36721)}, pythagorean_triples(271))
self.assertEqual({(121, 660, 671), (121, 7320, 7321)}, pythagorean_triples(121))
self.assertEqual({(153, 104, 185), (153, 204, 255), (153, 420, 447), (153, 680, 697), (153, 1296, 1305),
(153, 3900, 3903), (153, 11704, 11705)}, pythagorean_triples(153))
self.assertEqual({(235, 564, 611), (235, 1092, 1117), (235, 5520, 5525), (235, 27612, 27613)},
pythagorean_triples(235))
def test_random_BP(self):
for _ in range(10):
a = randint(10_000_000, 100_000_000) * 2 + 1
primitive_triples = pythagorean_triples(a, True)
for t in primitive_triples:
self.assertTrue(is_pythagorean_triple(t), f"{t} is not Pythagorean triple")
self.assertTrue(is_primitive(t), f'{t} is not primitive')
def test_random_B(self):
for _ in range(10):
a = randint(10_000_000, 100_000_000) * 2 + 1
triples = pythagorean_triples(a)
for t in triples:
self.assertTrue(is_pythagorean_triple(t), f"{t} is not Pythagorean triple")
def test_number_CP(self):
self.assertEqual({(12, 5, 13), (12, 35, 37)}, pythagorean_triples(12, True))
self.assertEqual(set(), pythagorean_triples(14, True))
self.assertEqual(set(), pythagorean_triples(34, True))
self.assertEqual({(56, 33, 65), (56, 783, 785)}, pythagorean_triples(56, True))
self.assertEqual({(68, 285, 293), (68, 1155, 1157)}, pythagorean_triples(68, True))
self.assertEqual(set(), pythagorean_triples(126, True))
def test_number_C(self):
self.assertEqual({(12, 5, 13), (12, 35, 37), (12, 9, 15), (12, 16, 20)}, pythagorean_triples(12))
self.assertEqual({(14, 48, 50)}, pythagorean_triples(14))
self.assertEqual({(34, 288, 290)}, pythagorean_triples(34))
self.assertEqual({(56, 33, 65), (56, 42, 70), (56, 90, 106), (56, 105, 119), (56, 192, 200), (56, 390, 394),
(56, 783, 785)}, pythagorean_triples(56))
self.assertEqual({(68, 51, 85), (68, 285, 293), (68, 576, 580), (68, 1155, 1157)},
pythagorean_triples(68))
self.assertEqual({(126, 32, 130), (126, 120, 174), (126, 168, 210), (126, 432, 450), (126, 560, 574),
(126, 1320, 1326), (126, 3968, 3970)}, pythagorean_triples(126))
def test_random_CP(self):
for _ in range(10):
a = randint(10_000_000, 100_000_000) * 2
primitive_triples = pythagorean_triples(a, True)
for t in primitive_triples:
self.assertTrue(is_pythagorean_triple(t), f"{t} is not Pythagorean triple")
self.assertTrue(is_primitive(t), f'{t} is not primitive')
def test_random_C(self):
for _ in range(10):
a = randint(10_000_000, 100_000_000) * 2
triples = pythagorean_triples(a)
for t in triples:
self.assertTrue(is_pythagorean_triple(t), f"{t} is not Pythagorean triple")
if __name__ == '__main__':
unittest.main()