Finding Number of Integers Divisible by Divisor Between Two Bounds

For an assignment, I need to write code that takes 3 inputs: a lower bound, an upper bound, and a divisor. The program is then supposed to output the number of integers between the lower and upper bounds that are evenly divisible by the divisor.

It sounds easy enough, but here's the catch: I'm forbidden from using loops of any kind (only sequential commands), no functions outside of main(), and the only libraries I can use are iostream, iomanip, and fstream, which (as far as I know) are irrelevant to the mathematical aspect of this problem.

The hint we were given was that we need to find the number of integers evenly divisible by the divisor below the upper bound, and then the number of integers evenly divisible by the divisor below the lower bound, and then subtract the second result from the first.

That part is easy enough to understand, and this would be simple if loops were allowed. But does anyone know how I can solve this using solely sequential commands?

Any help would be greatly appreciated.

Edit: In case it helps at all, here're some example values:
Lower bound: 10
Upper bound: 20
Divisor: 2
Output 6
Last edited on
interesting handicapped problem!
if upperBound is even, upperBound++;
if lowerBound is even, lowerBound--;

now use the hint you were given!
it is for problems like these that loops exist. loops have made life easy and i dont ever think we will ever live without them.

Why do teachers then test "ability of living without them "?
anup30: That was actually very helpful, however I'm not allowed to use 'if' statements, either...

Edit: shadowCODE, we're not supposed to use them because the class I'm taking hasn't gotten to them yet. We lose marks for using code that hasn't been discussed in class.

Edit #2: I think I've figured it out, using anup30's advice. This formula seems to work:

numberOfDivisibleIntegers = ((upperBound + 1) / divisor) - ((lowerBound - 1) / divisor);

(Where all variables are integers.)

Do you guys see any problems with this?

Edit #3: I get correct outputs for everything from 1 to 10, except for 7. It outputs 2 when it should be 1 (using the bounds 10 -> 20)...

Edit #4: Okay, this is working so far:

numberOfDivisibleIntegers = (upperBound / divisor) - ((lowerBound - 1) / divisor);

It works for all values I've tested including 7.

Once again: Do you guys see any problems?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main() {
    int n1, n2, d, res=0;
	cin >> n1 >>n2 >> d;
	res = (n1-n2 + (n2%d) )/d;
	if(n2%d==0) res++;
	cout << endl << "result "<< res <<endl;
    return 0;
}

i didn't notice about "if", to remove it have to think more :)
@anup30 You can simply remove the if with something like this (if your goal is to increment by one) res += n2%d == 0;
sorry it does not work.

e.g

range [70,199] , d = 50, we expect n to be = 2, (100, 150).

(199 + 1)/50 = 4,
(70 - 1)/50 = 1,

n = 4 - 1 = 3. ==> WRONG.

try
n = upper/d - lower/d
@shadowCODE, ^3 code will work!
I would do something like

1
2
lower = lower + lower % divisior ? (divisor - lower % divisor) : 0; //only if lower % divisior > 0
result = (upper - lower + 2) / divisor;


I haven't tested it but with shadowCODE's example of [70, 199] with d = 50

I get

lower = 70 + 70 % 50 != 0 ? (50 - 70 % 50) : 0;
lower = 70 + 20 != 0 ? (50 - 20) : 0
lower = 70 + 20 != 0 ? (30) : 0
lower = 70 + 30
lower = 100

result = (199 - 100 + 2) / 50;
result = (101) / 50
result = 2

A few other cases [4, 10] d = 2 (should be 4: 4, 6, 8, 10)
lower = 4
result = (10 - 4 + 2) / 2 == 8 / 2 == 4

[3, 14] d = 4 (should be 3: 4, 8, 12)
lower = 4
result = (14 - 4 + 2) / 4 == 12 / 4 = 3

IF you can't use ternary operators there are ways to replicate without but might need some hackery.
Last edited on
shadowCODE, check out the second formula I provided:

numberOfDivisibleIntegers = (upperBound / divisor) - ((lowerBound - 1) / divisor);

This one works with the range/divisor you provided (when all values are of the integer datatype). I don't think there are any problems with it. Can anyone prove that it doesn't work?

The suggestion you provided doesn't work (for the range [10,20], d = 2, it outputs 5 instead of 6).

giblit: I'm sorry to say that I'm not allowed to use propositions, either. I apologize for not mentioning that earlier.
Last edited on
It's fine. Also your second solution appears to work with these 4 cases:

1) upper - lower < divisor && upper % divisor != 0 && lower % divisor != 0 //you have this taken care of
2) upper % divisor == 0 && lower % divisor != 0 //you have this taken care of
3) upper % divisor != 0 && lower % divisor == 0 //you have this taken care of
4) upper % divisor == 0 && lower % divisor == 0 //you have this taken care of

You can test it with something like:

[4, 14] d = 4 (4, 8, 12 -> 3 is result)
[5, 16] d = 4(8, 12, 16 ->3 is result)
[4, 12] d = 4(4, 8, 12 ->3 is result)
[5, 7] d = 4 ( ->0 is result)


With your formula I get:
result = (14/4) - ((4-1)/4) = 3 - (3/4) = 3 - 0 = 3 <--correct
result = (16/4) - ((5-1)/4) = 4 - (4/4) = 4 - 1 = 3 <--correct
result = (12/4) - ((4-1)/4) = 3 - (3/4) = 3 - 0 = 0 <--correct
result = (7/4) - ((5-1)/4) = 1 - (4/4) = 1 - 1 = 0 <--correct

There is also I guess a 5th case

5) upper - lower > divisor && upper % divisor != 0 && lower % divisor != 0
[5, 9] d = 4 (8 ->1 is result)
result = (9/4) - ((5-1)/4) = 2 - (4/4) = 2- 1 = 1 <--correct
Last edited on
so its complete now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main() {
    int n1, n2, d, res=0;	
	
		cin >> n1 >>n2 >> d;
		res = (n1-n2 + n2%d )/d;
		//if(n2%d==0) res++;
		res+= n2%d == 0; //remove if as giblit
		cout << "result "<< res << endl;	
	
    return 0;
}
Squidy's final equation works well for a couple of test cases

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main() {
    int n1, n2, d;

    cout<<"Lower: ";
    cin>>n1;
    cout<<"Upper: ";
    cin>>n2;
    cout<<"div: ";
    cin>>d;

    cout<<(n2/d - (n1 - 1)/d)<<endl;
    return 0;
}


Nice team work!!
I submitted the assignment (using the final equation I posted), and the functionality portion of the grade (which is processed automatically) is 100%.

Thanks a lot for all your help, guys. I really appreciate it.
welcome
U of L? Was googling around for help on this assignment, found this, seems the same.
Topic archived. No new replies allowed.