Count Divisors  

by ne on 2021-09-22 under Algo

Write a program that will return the number of integers which are divisibly by K, in a given range of A..B, both inclusive.

For example, if A=3, B=6 and K=2, then it should return 2. SInce, there are 2 integers, 4 and 6, which are divisible by 2 (k).

Solution:


package com.codility;

/**
 * Created by nsthethunderbolt on 9/7/17.
 */
public class SolutionCountDiv {
    public int solution(int A, int B, int K){
        int ret=B/K -A/K;
        if(A%K==0)
            ret++;
        return ret;
    }
    public static void main(String[] arg) {
        int ret = new SolutionCountDiv().solution(11,345,17);
        System.out.println(ret);      
    }
}