-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36. Find GCD of two numbers .js
More file actions
196 lines (163 loc) · 5.6 KB
/
36. Find GCD of two numbers .js
File metadata and controls
196 lines (163 loc) · 5.6 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// find GCD of two numbers :
// --------------------------------------------------------------------------------------------------------------::
let a = 48;
let b = 18;
let gcd = 1;
for (let i = 1; i <= a && i <= b; i++) {
if (a % i === 0 && b % i === 0) {
gcd = i;
}
}
console.log("GCD of " + a + " and " + b + " is: " + gcd);
/*
GCD (Greatest Common Divisor) is the largest number that divides both numbers evenly.
EXPLANATION FOR BASIC METHOD:
let a = 48; // First number
let b = 18; // Second number
let gcd = 1; // Initialize GCD to 1 (minimum possible GCD)
// Check all numbers from 1 to minimum of a and b
for (let i = 1; i <= a && i <= b; i++) { // Loop from 1 to smaller number
if (a % i === 0 && b % i === 0) { // If i divides both numbers evenly
gcd = i; // Update GCD to current divisor
}
}
EXECUTION for GCD(48, 18):
i=1: 48%1=0, 18%1=0 → gcd=1
i=2: 48%2=0, 18%2=0 → gcd=2
i=3: 48%3=0, 18%3=0 → gcd=3
i=6: 48%6=0, 18%6=0 → gcd=6
i=9: 48%9≠0, skip
i=18: 48%18≠0, skip
Final: gcd=6
LOGIC: Check every number, keep updating GCD when we find a common divisor
*/
// ------------------------------------------------------------------------------------
function findGCDEuclidean(num1, num2) {
while (num2 !== 0) {
let temp = num2;
num2 = num1 % num2;
num1 = temp;
}
return num1;
}
console.log("Euclidean method - GCD(56, 98):", findGCDEuclidean(56, 98));
console.log("Euclidean method - GCD(48, 18):", findGCDEuclidean(48, 18));
/*
EXPLANATION FOR EUCLIDEAN METHOD:
function findGCDEuclidean(num1, num2) {
while (num2 !== 0) { // Continue until second number becomes 0
let temp = num2; // Store num2 temporarily
num2 = num1 % num2; // Replace num2 with remainder
num1 = temp; // Replace num1 with old num2
}
return num1; // When num2 becomes 0, num1 is the GCD
}
EUCLIDEAN ALGORITHM:
GCD(a, b) = GCD(b, a % b) until one number becomes 0
EXECUTION for GCD(48, 18):
Step 1: num1=48, num2=18 → temp=18, num2=48%18=12, num1=18
Step 2: num1=18, num2=12 → temp=12, num2=18%12=6, num1=12
Step 3: num1=12, num2=6 → temp=6, num2=12%6=0, num1=6
Step 4: num2=0, return num1=6
ADVANTAGE: Much faster than checking all numbers, O(log min(a,b)) complexity
*/
// ------------------------------------------------------------------------------------
let x = 24;
let y = 36;
let smaller = x < y ? x : y;
let result = 1;
for (let i = smaller; i >= 1; i--) {
if (x % i === 0 && y % i === 0) {
result = i;
break;
}
}
console.log("Optimized method - GCD(" + x + ", " + y + "):", result);
/*
EXPLANATION FOR OPTIMIZED BRUTE FORCE:
let x = 24; // First number
let y = 36; // Second number
let smaller = x < y ? x : y; // Find smaller of the two numbers
let result = 1; // Initialize result
// Start from smaller number and go down to 1
for (let i = smaller; i >= 1; i--) { // Count down from smaller number
if (x % i === 0 && y % i === 0) { // Check if i divides both numbers
result = i; // Found GCD
break; // Exit loop immediately (first match is largest)
}
}
EXECUTION for GCD(24, 36):
smaller = 24
i=24: 24%24=0, 36%24≠0 → not divisible
i=23: 24%23≠0 → not divisible
i=22: 24%22≠0 → not divisible
...
i=12: 24%12=0, 36%12=0 → found GCD=12, break
ADVANTAGE: Starts from largest possible, exits early when found
LOGIC: First common divisor found when counting down is the greatest
*/
// ------------------------------------------------------------------------------------
function gcdRecursive(a, b) {
if (b === 0) {
return a;
}
return gcdRecursive(b, a % b);
}
console.log("Recursive method - GCD(72, 48):", gcdRecursive(72, 48));
/*
EXPLANATION FOR RECURSIVE METHOD:
function gcdRecursive(a, b) {
if (b === 0) { // Base case: when second number is 0
return a; // Return first number as GCD
}
return gcdRecursive(b, a % b); // Recursive call with swapped and remainder
}
RECURSIVE EUCLIDEAN ALGORITHM:
Same logic as iterative but uses function calls instead of loops
EXECUTION for GCD(72, 48):
gcdRecursive(72, 48) → gcdRecursive(48, 72%48) → gcdRecursive(48, 24)
gcdRecursive(48, 24) → gcdRecursive(24, 48%24) → gcdRecursive(24, 0)
gcdRecursive(24, 0) → return 24
CALL STACK:
gcdRecursive(72, 48)
└─ gcdRecursive(48, 24)
└─ gcdRecursive(24, 0) → returns 24
ADVANTAGE: Clean, mathematical representation of Euclidean algorithm
*/
// ------------------------------------------------------------------------------------
function findGCDManual(num1, num2) {
let a = num1;
let b = num2;
while (a !== b) {
if (a > b) {
a = a - b;
} else {
b = b - a;
}
}
return a;
}
console.log("Manual subtraction - GCD(15, 25):", findGCDManual(15, 25));
/*
EXPLANATION FOR MANUAL SUBTRACTION METHOD:
function findGCDManual(num1, num2) {
let a = num1; // Copy first number
let b = num2; // Copy second number
while (a !== b) { // Continue until both numbers become equal
if (a > b) { // If first number is larger
a = a - b; // Subtract smaller from larger
} else { // If second number is larger
b = b - a; // Subtract smaller from larger
}
}
return a; // When equal, that's the GCD
}
EXECUTION for GCD(15, 25):
a=15, b=25 → b>a, so b=25-15=10
a=15, b=10 → a>b, so a=15-10=5
a=5, b=10 → b>a, so b=10-5=5
a=5, b=5 → equal, return 5
LOGIC: Keep subtracting smaller from larger until both become equal
ADVANTAGE: No modulo operator needed, pure subtraction
DISADVANTAGE: Slower than Euclidean for large numbers
*/