-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNumber To Binary.js
More file actions
35 lines (26 loc) · 772 Bytes
/
Number To Binary.js
File metadata and controls
35 lines (26 loc) · 772 Bytes
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
function NumberToBinary(n) {
// Handle special case when input is 0
if (n === 0) return "0";
// Initialize result string
let binaryCode = "";
// Continue until quotient is 1
while (n !== 1) {
// Append remainder to result
binaryCode = Math.abs(n % -2).toString() + binaryCode;
// Update quotient for the next iteration
n = Math.ceil(n / -2);
}
// Append the final '1' to the result
binaryCode = "1" + binaryCode;
return binaryCode;
}
function baseNeg2(n) {
// base case for special inputs
if (n === 0) return "0";
if (n === 1) return "1";
let ans = baseNeg2(Math.ceil(n / -2));
let reminder = Math.abs(n % -2).toString();
reminder = ans + "" + reminder;
return reminder;
}
console.log(baseNeg2(3));