Answers for "the sum the multiple"

1

find sum of multiples of a number

// C# program to find sum of multiples
// of a number up to N efficiently
using System;
 
class GFG {
 
    // Function for calculating sum
    // of multiples of a upto N
    static int calculate_sum(int a, int N)
    {
 
        // Number of multiples
        int m = N / a;
 
        // sum of first m natural numbers
        int sum = m * (m + 1) / 2;
 
        // sum of multiples
        int ans = a * sum;
 
        return ans;
    }
 
    // Driver code
    public static void Main()
    {
 
        int a = 7, N = 49;
        Console.WriteLine("Sum of multiples of " + a +
         " up to " + N + " = " + calculate_sum(a, N));
    }
}
Posted by: Guest on January-04-2022
-1

Sum of all the multiples of 3 or 5

const findSum = n => {
  let countArr = []
  
  for(let i = 0; i <= n; i++) if(i % 3 === 0 || i % 5 === 0) countArr.push(i) 
  return countArr.reduce((acc , curr) => acc + curr)
}
console.log(findSum(5))
Posted by: Guest on June-18-2020

Code answers related to "the sum the multiple"

Python Answers by Framework

Browse Popular Code Answers by Language