fork download
  1. #include<stdio.h>
  2.  
  3. /* Function to find the sum of the multiples
  4. of a OR b between 1 and N, excluding N */
  5.  
  6. int findSum(int a, int b, int N){
  7. N--;
  8. int a_n = (N/a)*a, b_n = (N/b)*b, ab_n = (N/(a*b))*a*b;
  9. int sum_a = ((N/a) * (a + a_n))/2;
  10. int sum_b = ((N/b) * (b + b_n))/2;
  11. int sum_ab = ((N/(a*b)) * (a*b + ab_n))/2;
  12. return sum_a + sum_b - sum_ab;
  13. }
  14.  
  15. int main(){
  16. int a = 3, b = 5, N = 1000;
  17. printf("%d", findSum(a, b, N));
  18. }
Runtime error #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
233168