MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_180/sol1.py:88–170  ·  view source on GitHub ↗

Find the sum of the numerator and denominator of the sum of all s(x,y,z) for golden triples (x,y,z) of the given order. >>> solution(5) 296 >>> solution(10) 12519 >>> solution(20) 19408891927

(order: int = 35)

Source from the content-addressed store, hash-verified

86
87
88def solution(order: int = 35) -> int:
89 """
90 Find the sum of the numerator and denominator of the sum of all s(x,y,z) for
91 golden triples (x,y,z) of the given order.
92
93 >>> solution(5)
94 296
95 >>> solution(10)
96 12519
97 >>> solution(20)
98 19408891927
99 """
100 unique_s: set = set()
101 hcf: int
102 total: Fraction = Fraction(0)
103 fraction_sum: tuple[int, int]
104
105 for x_num in range(1, order + 1):
106 for x_den in range(x_num + 1, order + 1):
107 for y_num in range(1, order + 1):
108 for y_den in range(y_num + 1, order + 1):
109 # n=1
110 z_num = x_num * y_den + x_den * y_num
111 z_den = x_den * y_den
112 hcf = gcd(z_num, z_den)
113 z_num //= hcf
114 z_den //= hcf
115 if 0 < z_num < z_den <= order:
116 fraction_sum = add_three(
117 x_num, x_den, y_num, y_den, z_num, z_den
118 )
119 unique_s.add(fraction_sum)
120
121 # n=2
122 z_num = (
123 x_num * x_num * y_den * y_den + x_den * x_den * y_num * y_num
124 )
125 z_den = x_den * x_den * y_den * y_den
126 if is_sq(z_num) and is_sq(z_den):
127 z_num = int(sqrt(z_num))
128 z_den = int(sqrt(z_den))
129 hcf = gcd(z_num, z_den)
130 z_num //= hcf
131 z_den //= hcf
132 if 0 < z_num < z_den <= order:
133 fraction_sum = add_three(
134 x_num, x_den, y_num, y_den, z_num, z_den
135 )
136 unique_s.add(fraction_sum)
137
138 # n=-1
139 z_num = x_num * y_num
140 z_den = x_den * y_num + x_num * y_den
141 hcf = gcd(z_num, z_den)
142 z_num //= hcf
143 z_den //= hcf
144 if 0 < z_num < z_den <= order:
145 fraction_sum = add_three(

Callers 1

sol1.pyFile · 0.70

Calls 3

add_threeFunction · 0.85
is_sqFunction · 0.85
addMethod · 0.45

Tested by

no test coverage detected