Ad
Algorithms
Logic
Algebra
Mathematics

Arithmetic progression, int itself an integer number of divisions by 3, 7, 21;

Code
Diff
  • int foo(int n){
        
          return
            ((int)n/3 *(3 + n - n % 3)+
            (int)n/7 *(7 + n- n % 7) -
            (int)n/21 *(21 + n - n % 21))/2;
    }
    • int foo(int n){
    • if (n < 0) {
    • return 0;
    • }
    • int count_3 = n / 3;
    • int count_7 = n / 7;
    • int count_21 = n / 21;
    • return
    • count_3 *(3 + n - n % 3) /2 +
    • count_7 *(7 + n- n % 7) / 2 -
    • count_21 *(21 + n - n % 21) /2;
    • ((int)n/3 *(3 + n - n % 3)+
    • (int)n/7 *(7 + n- n % 7) -
    • (int)n/21 *(21 + n - n % 21))/2;
    • }