I created an algorithm for finding the number of numbers inside a range that are divisible by a third number k. I got this to work but in polynomial time instead of linear time
function divisibleCount(x, y, k) {
var count = 0;
for (var i = x; i <= y; i++) {
if (i % k === 0) {
count++;
}
return count;
}
The arguments are as follows
x: Start of range
y: End of range
K: Number is divisible by
The problem is definitely the for loop which makes this polynomial time.
I attempted to use
for (var i = x; i <= k; i += k)
but got the wrong answer.
Is there any way I can improve this?