-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path37. Find LCM of two numbers .js
More file actions
181 lines (153 loc) · 4.29 KB
/
37. Find LCM of two numbers .js
File metadata and controls
181 lines (153 loc) · 4.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// find LCM of two numbers :
// --------------------------------------------------------------------------------------------------------------::
const findLCM1 = (a, b) => {
const findGCD = (x, y) => {
while (y !== 0) {
let temp = y;
y = x % y;
x = temp;
}
return x;
};
return (a * b) / findGCD(a, b);
};
// Terminal Input Handling
if (process.argv.length > 2) {
const num1 = parseInt(process.argv[2]);
const num2 = parseInt(process.argv[3]);
console.log(`Finding LCM of ${num1} and ${num2}:`);
console.log(`(GCD Formula) : ${findLCM1(num1, num2)}`);
} else {
// Test cases
console.log("LCM of 12 and 18:", findLCM1(12, 18));
}
/*
EXPLANATION:
Using Mathematical Formula (LCM * GCD = a * b)
=======================================================
const findLCM1 = (a, b) => {
const findGCD = (x, y) => {
while (y !== 0) {
let temp = y;
y = x % y;
x = temp;
}
return x;
};
return (a * b) / findGCD(a, b);
};
HOW IT WORKS:
- Uses mathematical relationship: LCM(a,b) × GCD(a,b) = a × b
- First calculates GCD using Euclidean algorithm
- Then applies formula: LCM = (a × b) / GCD
EXAMPLE: LCM(12, 18)
Step 1: Find GCD(12, 18)
- 18 % 12 = 6, so GCD(12, 6)
- 12 % 6 = 0, so GCD = 6
Step 2: LCM = (12 × 18) / 6 = 216 / 6 = 36
*/
// Brute Force Approach
// ------------------------------------------------------------------------------------
const findLCM2 = (a, b) => {
let lcm = a > b ? a : b;
while (true) {
if (lcm % a === 0 && lcm % b === 0) {
return lcm;
}
lcm++;
}
};
// Using Step Increment
// ------------------------------------------------------------------------------------
const findLCM3 = (a, b) => {
let max = a > b ? a : b;
let min = a < b ? a : b;
let lcm = max;
while (lcm % min !== 0) {
lcm += max;
}
return lcm;
};
// Recursive Approach with GCD
// ------------------------------------------------------------------------------------
const findLCM4 = (a, b) => {
const gcd = (x, y) => {
if (y === 0) return x;
return gcd(y, x % y);
};
return (a * b) / gcd(a, b);
};
// Terminal Input Handling
if (process.argv.length > 2) {
const num1 = parseInt(process.argv[2]);
const num2 = parseInt(process.argv[3]);
console.log(`Finding LCM of ${num1} and ${num2}:`);
console.log(`Method 2 (Brute Force): ${findLCM2(num1, num2)}`);
console.log(`Method 3 (Step Increment): ${findLCM3(num1, num2)}`);
console.log(`Method 4 (Recursive GCD): ${findLCM4(num1, num2)}`);
} else {
// Test cases
console.log("LCM of 12 and 18:", findLCM2(12, 18));
console.log("LCM of 15 and 25:", findLCM3(15, 25));
}
/*
EXPLANATION:
Brute Force Approach
==============================
const findLCM2 = (a, b) => {
let lcm = a > b ? a : b;
while (true) {
if (lcm % a === 0 && lcm % b === 0) {
return lcm;
}
lcm++;
}
};
HOW IT WORKS:
- Starts from the larger number
- Increments by 1 until finding a number divisible by both
- First such number is the LCM
EXAMPLE: LCM(12, 18)
- Start with 18
- 18 % 12 = 6 (not 0), continue
- 19 % 12 = 7, 19 % 18 = 1, continue
- ...continue until 36
- 36 % 12 = 0, 36 % 18 = 0 ✓
Using Step Increment
==============================
const findLCM3 = (a, b) => {
let max = a > b ? a : b;
let min = a < b ? a : b;
let lcm = max;
while (lcm % min !== 0) {
lcm += max;
}
return lcm;
};
HOW IT WORKS:
- Starts with the larger number
- Increments by the larger number (not by 1)
- More efficient than brute force
EXAMPLE: LCM(12, 18)
- max = 18, min = 12, lcm = 18
- 18 % 12 = 6 (not 0), lcm = 18 + 18 = 36
- 36 % 12 = 0 ✓
TIME COMPLEXITY: O(LCM/max(a,b))
SPACE COMPLEXITY: O(1)
Recursive Approach with GCD
====================================
const findLCM4 = (a, b) => {
const gcd = (x, y) => {
if (y === 0) return x;
return gcd(y, x % y);
};
return (a * b) / gcd(a, b);
};
HOW IT WORKS:
- Same mathematical formula as Method 1
- Uses recursive GCD instead of iterative
- Cleaner code but uses function call stack
EXAMPLE: LCM(12, 18)
- gcd(12, 18) → gcd(18, 12) → gcd(12, 6) → gcd(6, 0) → 6
- LCM = (12 × 18) / 6 = 36
*/