Given three positive integers N, A and B (A < B < N),
find the sum of all positive integers less than N, which are
divisible by either A or B.
For example, when N = 20, A =
4 and B = 7, the possible values are 4, 7, 8, 12, 14, and 16.
Their sum is 61.
Method 1: (Brute Force)
Method 1: (Brute Force)
- Starting from i = 1, loop till N-1
- For every i, check ether A divides i or B divides i and add it to sum , if it does
- Print the sum
#include <stdio.h> int main() { int N,A,B,sum=0,i; scanf("%d %d %d",&N,&A,&B); for(i=1;i<N;i++) { if( (i%A)==0 || (i%B)==0 ) sum=sum+i; } printf("%d",sum); return 0; }
Input Format:
N, A and B.
Output Format:
Sum
Sample Input
20 4 7
Sample Output
61