I find myself in a predicament where the issue at hand is defined as:
Harshad/Niven numbers are positive numbers that are divisible by the sum of their digits. All single-digit numbers are Harshad numbers.
For instance, 27 is a Harshad number as 2 + 7 = 9, and 9 is a divisor of 27.
Harshad numbers can exist in consecutive clusters. The numbers 1 through 10 are Harshad numbers. Numbers 132 and 133 are both Harshad numbers. Numbers 1014, 1015, 1016, 1017 are also Harshad numbers.
Develop a function that takes a number and yields an array of two elements. The first element being the length of the Harshad cluster to which the number belongs. The second element is its position within the cluster.
Examples harshad(5) ➞ [10, 5] // cluster = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] // The second element should be the layman order in the cluster, not the programming index. harshad(133) ➞ [2, 2] // cluster = [132, 133]
Therefore, I have devised a method to identify all the Harshad clusters greater than the number provided as an argument to the function, the code for which is outlined below
function track(num) {
let a = num.toString().split("");
let b = a.reduce((a, b) => +a + +b, 0);
if (num % b != 0) {return;}
console.log(num,num%b);
if (num % b == 0) {
num++;
track(num);
}
}
track(1015);
thus console.log() reveals to me 1015,1016 and 1017
cluster since they are greater than 1015 which is the input to the function. Yet, how can I determine the numbers smaller than 1015 since 1014 should also be a part of the cluster, but simply adding another IF statement does not seem logical
function track(num) {
let a = num.toString().split("");
let b = a.reduce((a, b) => +a + +b, 0);
if (num % b != 0) {return;}
console.log(num,num%b);
if (num % b == 0) {
num++;
track(num);
}
if(num%b==0){
num--
track(num)
}
}
track(1015);
since this approach seems to lack coherence