# this file available at: https://arachnoid.com/files/python01/ import math # pythagorean triple: # https://mathworld.wolfram.com/PythagoreanTriple.html # https://en.wikipedia.org/wiki/Pythagorean_triple # define the Triangle class class Triangle: # create an instance of the class def __init__(self, a:int, b:int, c:int): self.a,self.b,self.c = a,b,c def perimeter(self) -> int: return self.a + self.b + self.c def area(self) -> float: return (self.a * self.b) / 2.0 def sum_of_squares(self) -> float: return self.a**2+self.b**2 def hypotenuse(self) -> float: return self.sum_of_squares()**0.5 def coprime(self) -> bool: return math.gcd(self.a,math.gcd(self.b,self.c)) == 1 # test: is this instance a primitive Pythagorean triple? def triple_test(self) -> bool: # are a, b and c coprime? if not self.coprime(): return False # is the calculated hypotenuse equal to c? if self.hypotenuse() != self.c: return False else: return True def triangle_tests(): label_length = 32 for values in ((3,4,5),(6,8,10),(5,12,13),(7,24,25),(13,14,19)): # create an instance of the Triangle class triangle_instance = Triangle(*values) print(f'for Triangle class instance values a,b,c = {values}:') print(f'\t{"Perimeter length":{label_length}} = {triangle_instance.perimeter():5.1f}') print(f'\t{"Area":{label_length}} = {triangle_instance.area():5.1f}') print(f'\t{"Sum of squares : a**2 + b**2":{label_length}} = {triangle_instance.sum_of_squares():5.1f}') print(f'\t{"Hypotenuse : sqrt(a**2+b**2)":{label_length}} = {triangle_instance.hypotenuse():5.1f}') print(f'\t{"Primitive Pythagorean triple:":{label_length}} = {triangle_instance.triple_test()}') print() if __name__ == "__main__": triangle_tests()