-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcd.js
More file actions
148 lines (119 loc) · 3.12 KB
/
Copy pathgcd.js
File metadata and controls
148 lines (119 loc) · 3.12 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
// Program to Find GCD or HCF of Two Numbers
function gcd(a, b) {
// find minimum of a and b
let result = Math.min(a, b);
while (result > 0) {
if (a % result == 0 && b % result == 0) {
break;
}
result--;
}
// return gsd of a and b
return result;
}
console.log(gcd(98, 56));
/*
Check if result divides both a and b without a remainder:
Start with result = 56:
98 % 56 = 42 (≠ 0) → Not a divisor
Decrement result to 55:
98 % 55 = 43 (≠ 0) → Not a divisor
Continue decrementing until result = 14:
98 % 14 = 0
56 % 14 = 0
Factors of 98: 1, 2, 7, 14, 49, 98
Factors of 56: 1, 2, 4, 7, 8, 14, 28, 56
Common factors: 1, 2, 7, 14
Greatest common divisor (GCD): 14
*/
console.log(gcd(60, 36));
/*
Check if result divides both a and b without a remainder:
Start with result = 36:
60 % 36 = 24 (≠ 0) → Not a divisor
Decrement result to 35:
60 % 35 = 23 (≠ 0) → Not a divisor
Continue decrementing until result = 12:
60 % 14 = 0
36 % 14 = 0
Factors of 60: 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60
Factors of 36: 1, 2, 3, 4, 6, 6, 9, 12, 18, 36
Common factors: 1, 2, 4, 6, 12
Greatest common divisor (GCD): 12
*/
// Euclidean Algorithm - Subtraction
function gcd1(a, b) {
if (a == 0) return b;
if (b == 0) return a;
// base case
if (a == b) return a;
if (a > b) return gcd1(a - b, b); // 42 , 56 ; 28 , 14 ; 14, 14 => a === b => return a = 14
return gcd1(a, b - a); // 42, 14
}
let aa = 98,
bb = 56;
console.log("GCD of " + aa + " and " + bb + " is " + gcd1(aa, bb));
// Optimized Subtraction Euclidean by Checking Divisibility First
function gcd(a, b) {
if (a === 0) {
return b;
}
if (b === 0) {
return a;
}
if (a === b) {
return a;
}
if (a > b) {
// 98 > 56; 42 < 56; 42 > 14
if (a % b === 0) {
// 42 % 14 === 0
return b; // 14
}
return gcd(a - b, b); // 42 , 56
}
if (b % a === 0) {
return a;
}
return gcd(a, b - a); // 42, 14
}
let a1 = 98,
b1 = 56;
console.log(`GCD of ${a1} & ${b1} is ${gcd(a1, b1)}`);
// Optimized Division Based Euclidean - recursive function
function gcd(a, b) {
if (b === 0) {
return a;
}
return gcd(b, a % b);
}
console.log(gcd(60, 36));
/* gcd(60, 36)
gcd(60, 36) → gcd(36, 60 % 36) = gcd(36, 24)
gcd(36, 24) → gcd(24, 36 % 24) = gcd(24, 12)
gcd(24, 12) → gcd(12, 24 % 12) = gcd(12, 0)
gcd(12, 0) → b === 0 => return 12. */
// Euclidean Algorithm - while looping statement
function gcd(a, b) {
while (b !== 0) {
let temp = b; // 56; 42; 14
b = a % b; // 98 % 56 = 42; 56 % 42 = 14 ; 42 % 14 = 0 => here b === 0 --> stop execution
a = temp; // 56; 42; 14
}
return a;
}
console.log(gcd(98, 56));
console.log(gcd(56, 98));
console.log(gcd(12, 16));
/* Compute a % b:
56 % 98 = 56 (since 98 > 56, the remainder is 56).
=> (98, 56)
12 % 16 = 12 (since 16 > 12, the remainder is 12).
=> (16, 12)
*/
/*
gcd(98, 56)
98 % 56 = 42 → gcd(56, 42)
56 % 42 = 14 → gcd(42, 14)
42 % 14 = 0 → GCD = 14
*/