Div Count

Div Count

In Div Count problem we are given a sequence of number incrementing by one each time and we need to find the number that multiples that exist in the sequence of a particular factor.

Example:
console.log(CountDiv(6, 11 ,2)) -> 3
console.log(CountDiv(4, 17, 3)) -> 4

function CountDiv(start, end, divisor) {
  let nStart = Math.ceil(start / divisor);
  let nEnd = Math.floor(end / divisor);
  return nEnd - nStart + 1
}

console.log(CountDiv(6,11,2)) -> 3
console.log(CountDiv(4,17,3)) -> 4


We create a function called CountDiv with parameters called start, end, divisor

We create variable nStart to be equal to Math.ceil which is build in JavaScript method that returns the smallest integer greater than or equal to the given number. And we use inside Math.ceil(start / divisor)
nStart = start / divisor => 6 / 2 = 3

We create variable nEnd to be equal to Math.floor which is build in JavaScript method that returns a number representing the largest integer less than or equal to the specified number. And we use inside Math.floor(end / divisor)
nEnd = end / divisor => 11 / 2 = 5

In the end we return nEnd - nStart + 1
5 - 3 = 2 + 1 = 3

comments powered by Disqus