Sum of all the multiples of 3 and 7 below N
Last Updated :
10 Apr, 2023
Given a number N, the task is to find the sum of all the multiples of 3 and 7 below N.
Note: A number must not repeat itself in the sum.
Examples:
Input: N = 10
Output: 25
3 + 6 + 7 + 9 = 25
Input: N = 24
Output: 105
3 + 6 + 7 + 9 + 12 + 14 + 15 + 18 + 21 = 105
Brute Force Approach:
A brute force approach to solve this problem would be to iterate through all the numbers from 1 to N-1, and check if each number is a multiple of 3 or 7. If it is, add it to the sum. However, we need to make sure that a number is not added to the sum multiple times. To do this, we can use a set to keep track of the numbers that have already been added.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long sumMultiples( long long n)
{
long long sum = 0;
set< long long > multiples;
for ( long long i = 1; i < n; i++) {
if (i % 3 == 0 || i % 7 == 0) {
if (multiples.find(i) == multiples.end()) {
sum += i;
multiples.insert(i);
}
}
}
return sum;
}
int main()
{
long long n = 24;
cout << sumMultiples(n);
return 0;
}
|
Java
import java.util.HashSet;
import java.util.Set;
public class Main {
static long sumMultiples( long n) {
long sum = 0 ;
Set<Long> multiples = new HashSet<>();
for ( long i = 1 ; i < n; i++) {
if (i % 3 == 0 || i % 7 == 0 ) {
if (!multiples.contains(i)) {
sum += i;
multiples.add(i);
}
}
}
return sum;
}
public static void main(String[] args) {
long n = 24 ;
System.out.println(sumMultiples(n));
}
}
|
Python3
def sumMultiples(n):
sum = 0
multiples = set ()
for i in range ( 1 , n):
if i % 3 = = 0 or i % 7 = = 0 :
if i not in multiples:
sum + = i
multiples.add(i)
return sum
n = 24
print (sumMultiples(n))
|
C#
using System;
using System.Collections.Generic;
class MainClass {
static long SumMultiples( long n)
{
long sum = 0;
HashSet< long > multiples = new HashSet< long >();
for ( long i = 1; i < n; i++) {
if (i % 3 == 0 || i % 7 == 0) {
if (!multiples.Contains(i)) {
sum += i;
multiples.Add(i);
}
}
}
return sum;
}
static void Main()
{
long n = 24;
Console.WriteLine(SumMultiples(n));
}
}
|
Javascript
function sumMultiples(n) {
let sum = 0;
let multiples = new Set();
for (let i = 1; i < n; i++) {
if (i % 3 === 0 || i % 7 === 0) {
if (!multiples.has(i)) {
sum += i;
multiples.add(i);
}
}
}
return sum;
}
let n = 24;
console.log(sumMultiples(n));
|
Time Complexity: O(N), since we need to iterate through all the numbers from 1 to N-1.
Auxiliary Space: O(N), since the set can potentially store all the multiples.
Approach:
- We know that multiples of 3 form an AP as S3 = 3 + 6 + 9 + 12 + 15 + 18 + 21 + …
- And the multiples of 7 form an AP as S7 = 7 + 14 + 21 + 28 + …
- Now, Sum = S3 + S7 i.e. 3 + 6 + 7 + 9 + 12 + 14 + 15 + 18 + 21 + 21 + …
- From the previous step, 21 is repeated twice. In fact, all of the multiples of 21 (or 3*7) will be repeated as they are counted twice, once in the series S3 and again in the series S7. So, the multiples of 21 need to be discarded from the result.
- So, the final result will be S3 + S7 – S21
The formula for the sum of an AP series is :
n * ( a + l ) / 2
Where n is the number of terms, a is the starting term, and l is the last term.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long sumAP( long long n, long long d)
{
n /= d;
return (n) * (1 + n) * d / 2;
}
long long sumMultiples( long long n)
{
n--;
return sumAP(n, 3) + sumAP(n, 7) - sumAP(n, 21);
}
int main()
{
long long n = 24;
cout << sumMultiples(n);
return 0;
}
|
Java
import java.util.*;
class solution
{
static long sumAP( long n, long d)
{
n /= d;
return (n) * ( 1 + n) * d / 2 ;
}
static long sumMultiples( long n)
{
n--;
return sumAP(n, 3 ) + sumAP(n, 7 ) - sumAP(n, 21 );
}
public static void main(String args[])
{
long n = 24 ;
System.out.println(sumMultiples(n));
}
}
|
Python3
def sumAP(n, d):
n = int (n / d);
return (n) * ( 1 + n) * (d / 2 );
def sumMultiples(n):
n - = 1 ;
return int (sumAP(n, 3 ) +
sumAP(n, 7 ) -
sumAP(n, 21 ));
n = 24 ;
print (sumMultiples(n));
|
C#
using System;
class GFG
{
static long sumAP( long n, long d)
{
n /= d;
return (n) * (1 + n) * d / 2;
}
static long sumMultiples( long n)
{
n--;
return sumAP(n, 3) + sumAP(n, 7) -
sumAP(n, 21);
}
static public void Main(String []args)
{
long n = 24;
Console.WriteLine(sumMultiples(n));
}
}
|
PHP
<?php
function sumAP( $n , $d )
{
$n = (int)( $n / $d );
return ( $n ) * (1 + $n ) * ( $d / 2);
}
function sumMultiples( $n )
{
$n --;
return sumAP( $n , 3) +
sumAP( $n , 7) - sumAP( $n , 21);
}
$n = 24;
echo sumMultiples( $n );
?>
|
Javascript
<script>
function sumAP(n, d)
{
n = parseInt(n / d);
return (n) * (1 + n) * (d / 2);
}
function sumMultiples(n)
{
n--;
return sumAP(n, 3) +
sumAP(n, 7) -
sumAP(n, 21);
}
let n = 24;
document.write(sumMultiples(n));
</script>
|
Time complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...