An algorithm which returns the number of integers divisible by N in an sequenced interval [Y..Z].

Following the solution.

public int algo(int Y, int Z, int N) {
    if ( Z==0 ) {
       return 1;
    }
    if (Z != 0) {
       return Z/N - ((Y-1)/N);
    } else {
       return Z/N +1;
    }
}

Following the math explanation.

The quantity of integers divisible by N in the range [1 .. Z] is Z/N. Then in the range  [Z .. Y], the result is =Y/N – ((Z – 1)/N). Also, zero is divisible by any number which answer is 0. 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s