Ad
Algorithms
Logic
Algebra
Mathematics

int foo(int n){
int sum=0;
for (int i =1; i <= n; i++)
{
if (i % 3 == 0 || i % 7 == 0)
{
sum+=i;
}
}
if (n==1){
return 1;
}else
return sum;
}

Code
Diff
  • int foo(int n){
        int sum=0;
        for (int i =1; i <= n; i++)
        {
            if (i % 3 == 0 || i % 7 == 0)
            {
                sum+=i;
            }
        }
        if (n==1){
            return 1;
        }else
            return sum;
    }
    • const countMultiplesOfKUpToN = (N, k) => Math.floor(N/k),
    • sumLMultiplesOfN = (l, n) => 0.5*n*l*(l+1),
    • sumMultiplesOfAOrBUpToN = (a, b, N) => {
    • const lA = countMultiplesOfKUpToN(N, a),
    • lB = countMultiplesOfKUpToN(N, b),
    • lAB = countMultiplesOfKUpToN(N, a*b),
    • sA = sumLMultiplesOfN(lA, a),
    • sB = sumLMultiplesOfN(lB, b),
    • sAB = sumLMultiplesOfN(lAB, a*b)
    • return sA+sB-sAB
    • }
    • console.log(sumMultiplesOfAOrBUpToN(3,7,30))
    • console.log(sumMultiplesOfAOrBUpToN(3,7,500000000))
    • int foo(int n){
    • int sum=0;
    • for (int i =1; i <= n; i++)
    • {
    • if (i % 3 == 0 || i % 7 == 0)
    • {
    • sum+=i;
    • }
    • }
    • if (n==1){
    • return 1;
    • }else
    • return sum;
    • }