-
Notifications
You must be signed in to change notification settings - Fork 1
/
elementary.js
47 lines (45 loc) · 919 Bytes
/
elementary.js
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
function multiply(a, b) {
let res;
if (a === 0 || b === 0) {
res = 0;
} else {
if (b < 0) {
a = -a;
b = -b;
}
res = a + multiply(a, b - 1);
}
return res;
}
function divide(a, b) {
let res;
if (a === 0) {
res = 0;
} else if (b === 1) {
res = a;
} else if (b === -1) {
res = -a;
} else if (b === 0) {
return Infinity;
} else {
let neg;
if (a < 0) {
a = Math.abs(a);
neg = true;
}
if (b < 0) {
b = Math.abs(b);
neg ? (neg = false) : (neg = true);
}
let count = 0;
while (a >= b) {
a -= b;
count++;
}
res = neg ? -count : count;
}
return res;
}
function modulo(num, divisor) {
return num - multiply(divisor, divide(num, divisor));
}