# Count the divisible numbers in Java

## The challenge

Complete the function that takes 3 numbers `x, y and k` (where `x ≤ y`), and returns the number of integers within the range `[x..y]` (both ends included) that are divisible by `k`.

More scientifically: `{ i : x ≤ i ≤ y, i mod k = 0 }`

### Example

Given `x = 6, y = 11, k = 2` the function should return `3`, because there are three numbers divisible by `2` between `6` and `11``6, 8, 10`

• Note: The test cases are very large. You will need a O(log n) solution or better to pass. (A constant time solution is possible.)

## The solution in Java code

Option 1:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```class Solution {
static long divisibleCount(long x, long y, long k) {
return y / k - x / k + (x % k > 0 ? 0 : 1);
}
}
```Code language: Java (java)```

Option 2:

``````public class Solution {

public static long divisibleCount(long x, long y, long k) {
return Math.floorDiv(y, k) - Math.floorDiv(x - 1, k);
}

}
```Code language: Java (java)```

Option 3:

``````import java.util.stream.LongStream;
public class Solution {

public static long divisibleCount(long x, long y, long k) {
while (x % k != 0)
x++;
while (y % k != 0)
y--;
return (y - x) / k + 1;  }

}
```Code language: Java (java)```

## Test cases to validate our solution

``````import java.util.Random;
import java.util.function.LongBinaryOperator;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class SolutionTest {
@Test
public void fixedTests() {
assertEquals( 3, Solution.divisibleCount( 6,  11,  2));
assertEquals(20, Solution.divisibleCount(11, 345, 17));
assertEquals( 1, Solution.divisibleCount( 0,   1,  7));
assertEquals( 1, Solution.divisibleCount(20,  20,  2));
assertEquals( 0, Solution.divisibleCount(20,  20,  8));
assertEquals( 1, Solution.divisibleCount(19,  20,  2));
assertEquals(11, Solution.divisibleCount( 0,  10,  1));
assertEquals( 2, Solution.divisibleCount(11,  14,  2));
assertEquals(838488366986797791L, Solution.divisibleCount(101, Long.MAX_VALUE, 11));
assertEquals(84618092081236466L, Solution.divisibleCount(1005, Long.MAX_VALUE, 109));
}

@Test
public void randomTests() {
Random rnd = new Random();
LongBinaryOperator nextLong = (a,b)->a+(long)(rnd.nextDouble()*(b-a));
for(int i=0; i<100; ++i) {
long a = nextLong.applyAsLong(10000L, Long.MAX_VALUE - nextLong.applyAsLong(45000L, 150000L));
long b = nextLong.applyAsLong(a + 1, Long.MAX_VALUE);
long k = nextLong.applyAsLong(2L, 5000L);
assertEquals(Math.floorDiv(b,k)-Math.floorDiv(a-1,k), Solution.divisibleCount(a, b, k));
}
}
}
```Code language: Java (java)```
Tags:
Subscribe
Notify of