Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Next multiple of 5 and binary concatenation in JavaScript
We need to write a JavaScript function that takes a number and finds the next higher multiple of 5 by concatenating the shortest possible binary string to the number's binary representation.
Problem
Given a number n, we want to:
- Convert n to binary representation
- Append the shortest binary string to make it divisible by 5
- Return the resulting decimal number
How It Works
The algorithm generates all possible binary combinations of increasing length until it finds one that, when concatenated to the original number's binary form, creates a number divisible by 5.
Example
For number 8 (binary: 1000), we need to find the shortest binary string to append:
const generateAll = (num = 1) => {
const res = [];
let max = parseInt("1".repeat(num), 2);
for(let i = 0; i <= max; i++){
res.push(i.toString(2).padStart(num, '0'));
};
return res;
};
const smallestMultiple = (num = 1) => {
const numBinary = num.toString(2);
let i = 1;
while(true){
const perm = generateAll(i);
const required = perm.find(binary => {
return parseInt(numBinary + binary, 2) % 5 === 0;
});
if(required){
return parseInt(numBinary + required, 2);
};
i++;
};
};
console.log(smallestMultiple(8));
console.log("Binary of 8:", (8).toString(2));
console.log("Result binary:", (35).toString(2));
35 Binary of 8: 1000 Result binary: 100011
Step-by-Step Breakdown
Let's trace through the example with number 8:
// Step 1: Convert 8 to binary
let num = 8;
console.log("Original number:", num);
console.log("Binary representation:", num.toString(2));
// Step 2: Try different binary suffixes
console.log("\nTrying different suffixes:");
console.log("1000" + "0" + " =", parseInt("10000", 2), "% 5 =", parseInt("10000", 2) % 5);
console.log("1000" + "1" + " =", parseInt("10001", 2), "% 5 =", parseInt("10001", 2) % 5);
console.log("1000" + "00" + " =", parseInt("100000", 2), "% 5 =", parseInt("100000", 2) % 5);
console.log("1000" + "01" + " =", parseInt("100001", 2), "% 5 =", parseInt("100001", 2) % 5);
console.log("1000" + "10" + " =", parseInt("100010", 2), "% 5 =", parseInt("100010", 2) % 5);
console.log("1000" + "11" + " =", parseInt("100011", 2), "% 5 =", parseInt("100011", 2) % 5);
Original number: 8 Binary representation: 1000 Trying different suffixes: 1000 + 0 = 16 % 5 = 1 1000 + 1 = 17 % 5 = 2 1000 + 00 = 32 % 5 = 2 1000 + 01 = 33 % 5 = 3 1000 + 10 = 34 % 5 = 4 1000 + 11 = 35 % 5 = 0
Optimized Version
Here's a more efficient approach that doesn't generate all combinations upfront:
const findNextMultipleOf5 = (num) => {
const numBinary = num.toString(2);
for(let length = 1; length <= 10; length++) {
for(let suffix = 0; suffix < (1 << length); suffix++) {
const binarySuffix = suffix.toString(2).padStart(length, '0');
const combined = parseInt(numBinary + binarySuffix, 2);
if(combined % 5 === 0 && combined > num) {
return combined;
}
}
}
return -1; // Not found within reasonable range
};
// Test with multiple numbers
console.log("Next multiple of 5 for 8:", findNextMultipleOf5(8));
console.log("Next multiple of 5 for 3:", findNextMultipleOf5(3));
console.log("Next multiple of 5 for 1:", findNextMultipleOf5(1));
Next multiple of 5 for 8: 35 Next multiple of 5 for 3: 5 Next multiple of 5 for 1: 5
Key Points
- The algorithm systematically tries binary suffixes of increasing length
- It stops at the first suffix that makes the result divisible by 5
- Binary concatenation is achieved using string operations followed by parseInt
- The modulo operator (%) checks divisibility by 5
Conclusion
This algorithm efficiently finds the next multiple of 5 by binary concatenation. The key insight is systematically trying shorter suffixes first to ensure the shortest possible binary addition.
