2021-08-19 14:50:01 +05:30
|
|
|
/*
|
|
|
|
|
* Problem Statement: Given a positive number `num`, find it's binary equivalent using recursion
|
|
|
|
|
*
|
|
|
|
|
* What is Binary Equivalent?
|
|
|
|
|
* - In binary number system, a number is represented in terms of 0s and 1s,
|
2021-08-19 15:41:36 +05:30
|
|
|
* for example:
|
|
|
|
|
* - Binary Of 2 = 10
|
|
|
|
|
* - Binary of 3 = 11
|
|
|
|
|
* - Binary of 4 = 100
|
2021-08-19 14:50:01 +05:30
|
|
|
*
|
|
|
|
|
* Reference on how to find Binary Equivalent
|
|
|
|
|
* - https://byjus.com/maths/decimal-to-binary/
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-10 18:18:25 +02:00
|
|
|
export const binaryEquivalent = (num) => {
|
2021-08-19 14:50:01 +05:30
|
|
|
if (num === 0 || num === 1) {
|
|
|
|
|
return String(num)
|
|
|
|
|
}
|
|
|
|
|
return binaryEquivalent(Math.floor(num / 2)) + String(num % 2)
|
|
|
|
|
}
|