-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path35_gcd_problem.py
More file actions
73 lines (60 loc) · 2.05 KB
/
35_gcd_problem.py
File metadata and controls
73 lines (60 loc) · 2.05 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Solution:
def gcd(self, a, b):
while b != 0:
a, b = b, a % b
return abs(a)
def gcd_of_array(self, arr):
if not arr:
return 0
result = arr[0]
for num in arr[1:]:
result = self.gcd(result, num)
return result
def gcd_of_array_smallest_and_largest(self, nums) -> int:
if not nums:
return 0
smallest = min(nums)
largest = max(nums)
return self.gcd(smallest, largest)
def pairwise_gcd(self, arr):
n = len(arr)
result = []
for i in range(n):
for j in range(i + 1, n):
result.append(((arr[i], arr[j]), self.gcd(arr[i], arr[j])))
return result
# Infinite generator — note: no 'self' because it doesn't depend on the class
@staticmethod
def infinite_numbers(start=12, step=6):
i = start
while True:
yield i
i += step
# Optional: compute GCD for first N numbers of infinite generator
def gcd_stream(self, generator, limit=10):
result = next(generator)
count = 1
for num in generator:
result = self.gcd(result, num)
count += 1
if count >= limit:
break
return result
if __name__ == "__main__":
solution = Solution()
# Example 1: GCD of two numbers
print("GCD(48, 18):", solution.gcd(48, 18))
# Example 2: GCD of an array
nums = [12, 18, 24]
print("Array:", nums)
print("GCD of array:", solution.gcd_of_array(nums))
print("GCD of array smallest and larget:", solution.gcd_of_array_smallest_and_largest(nums))
# Example 3: Pairwise GCDs
pairs = solution.pairwise_gcd(nums)
print("\nPairwise GCDs:")
for pair, value in pairs:
print(f"GCD{pair} = {value}")
# Example 4: Infinite generator demo (first 10 numbers)
print("\nGCD of first 10 numbers from infinite generator:")
gen = Solution.infinite_numbers()
print(solution.gcd_stream(gen, limit=10))