-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion 32.py
More file actions
38 lines (31 loc) · 1.29 KB
/
Question 32.py
File metadata and controls
38 lines (31 loc) · 1.29 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
# Concept name : Number Problems
# Question : LCM And GCD
# Given two integers a and b, You have to compute their LCM and GCD and return an array containing their LCM and GCD.
# Examples:
# Input: a = 5 , b = 10
# Output: [10, 5]
# Explanation: LCM of 5 and 10 is 10, while their GCD is 5.
# Solution :
class Solution:
def lcmAndGcd(self, a : int, b : int) -> List[int]:
oa=a
ob=b
while a%b!=0:
rem=a%b
a=b
b=rem
gcd=b
lcm=(oa*ob)//gcd
return [lcm, gcd]
# Explanation : Preserve original values:
# Store a and b in oa and ob for later use in LCM calculation.
# Compute GCD using the Euclidean algorithm:
# Repeatedly perform a % b and update a and b:
# while a % b != 0:
# rem = a % b
# a = b
# b = rem
# Once the remainder is zero, b holds the GCD.
# Calculate LCM using the formula:
# LCM = (original_a * original_b) // GCD
# Return [LCM, GCD]