Given three numbers A, B and X. Print the summation of numbers between A and B inclusive that are divisible by X.
Example :
Input : 5 20 5
Output : 50
Explanation: The numbers [5, 10, 15, 20] are dividable by 5 then the result: 5 + 10 + 15 + 20 = 50.
This my Function :
long long int Divisability(long long int a, long long int b, long long int x) {
long long int sum = 0;
for (long long int i = a; i <= b; i++) {
if (i % x == 0) {
sum += i;
}
}
return sum;}
It works well with a small ranges but doesn't work with a big ranges like :
Input : 1 1000000000 1000000000.
my function causes a "Time limit exceeded".
I need another algorithm to solve this problem.