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
Finding out the Harshad number JavaScript
Harshad numbers are those numbers which are exactly divisible by the sum of their digits. Like the number 126, it is completely divisible by 1+2+6 = 9.
-
All single digit numbers are harshad numbers.
-
Harshad numbers often exist in consecutive clusters like [1,2,3,4,5,6,7,8,9,10], [110,111,112], [1010, 1011, 1012].
Our job is to write a function that takes in a Number as input checks whether it's a harshad number or not, if not then returns -1 otherwise it returns the length of streak of consecutive harshad cluster.
For example ?
harshadNum(1014) = harshadNum(1015) = harshadNum(1016) = harshadNum(1017) = 4 harshadNum(1) = 10 harshadNum(12) = 1 harshadNum(23) = -1
Understanding the Problem
Let's break this problem into two major functions:
-
isHarshad() ? takes in a number num and returns a boolean depending upon whether the number is harshad or not.
-
harshadNum() ? main function that takes in the actual number, makes call to isHarshad() at various points and returns the length of streak.
The isHarshad() Function
First, we need a helper function to check if a number is Harshad:
const isHarshad = (num) => {
let sum = 0, temp = num;
while(temp){
sum += temp % 10;
temp = Math.floor(temp/10);
}
return num % sum === 0;
}
// Test the function
console.log(isHarshad(126)); // 1+2+6 = 9, 126 % 9 = 0
console.log(isHarshad(23)); // 2+3 = 5, 23 % 5 = 3 (not 0)
true false
The harshadNum() Function
Now let's code the main function that finds the length of consecutive Harshad numbers:
const isHarshad = (num) => {
let sum = 0, temp = num;
while(temp){
sum += temp % 10;
temp = Math.floor(temp/10);
}
return num % sum === 0;
}
const harshadNum = (number) => {
//if the input is not harshad return -1
if(!isHarshad(number)){
return -1;
}
let streak = 1, prev = number-1, next = number+1;
//check preceding streak
while(isHarshad(prev) && prev > 0){
streak++;
prev--;
}
//check succeeding streak
while(isHarshad(next)){
streak++;
next++;
}
return streak;
};
// Test with different examples
console.log(harshadNum(1014)); // Should return 4
console.log(harshadNum(1)); // Should return 10
console.log(harshadNum(12)); // Should return 1
console.log(harshadNum(23)); // Should return -1
4 10 1 -1
How It Works
The algorithm works as follows:
- Check whether the input number is a Harshad number. If not, return -1.
- Initialize streak counter to 1 (counting the input number itself).
- Run a loop backwards from the input number while we keep getting Harshad numbers, incrementing the streak counter.
- Run a loop forwards from the input number while we keep getting Harshad numbers, incrementing the streak counter.
- Return the total streak length.
Complete Example
const isHarshad = (num) => {
let sum = 0, temp = num;
while(temp){
sum += temp % 10;
temp = Math.floor(temp/10);
}
return num % sum === 0;
}
const harshadNum = (number) => {
if(!isHarshad(number)){
return -1;
}
let streak = 1, prev = number-1, next = number+1;
while(isHarshad(prev) && prev > 0){
streak++;
prev--;
}
while(isHarshad(next)){
streak++;
next++;
}
return streak;
};
// Demonstrating the cluster [1014, 1015, 1016, 1017]
console.log("Testing cluster starting at 1014:");
for(let i = 1014; i
Testing cluster starting at 1014:
1014: Harshad
1015: Harshad
1016: Harshad
1017: Harshad
Streak length: 4
Conclusion
This solution efficiently finds consecutive Harshad number clusters by checking both directions from a given number. The algorithm returns the total streak length or -1 if the input isn't a Harshad number.
