Sum of numbers from 1 to N which are divisible by 3 or 4
Last Updated :
06 Apr, 2023
Given a number N. The task is to find the sum of all those numbers from 1 to N that are divisible by 3 or by 4.
Examples:
Input : N = 5
Output : 7
sum = 3 + 4
Input : N = 12
Output : 42
sum = 3 + 4 + 6 + 8 + 9 + 12
Approach: To solve the problem, follow the below steps:
- Find the sum of numbers that are divisible by 3 upto N. Denote it by S1.
- Find the sum of numbers that are divisible by 4 upto N. Denote it by S2.
- Find the sum of numbers that are divisible by 12(3*4) upto N. Denote it by S3.
- The final answer will be S1 + S2 – S3.
In order to find the sum, we can use the general formula of A.P. which is:
Sn = (n/2) * {2*a + (n-1)*d}
Where,
n -> total number of terms
a -> first term
d -> common difference
For S1: The total numbers that will be divisible by 3 upto N will be N/3 and the series will be 3, 6, 9, 12, ….
Hence,
S1 = ((N/3)/2) * (2 * 3 + (N/3 - 1) * 3)
For S2: The total numbers that will be divisible by 4 up to N will be N/4 and the series will be 4, 8, 12, 16, …...
Hence,
S2 = ((N/4)/2) * (2 * 4 + (N/4 - 1) * 4)
For S3: The total numbers that will be divisible by 12 upto N will be N/12.
Hence,
S3 = ((N/12)/2) * (2 * 12 + (N/12 - 1) * 12)
Therefore, the result will be:
S = S1 + S2 - S3
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int sum( int N)
{
int S1, S2, S3;
S1 = ((N / 3)) * (2 * 3 + (N / 3 - 1) * 3) / 2;
S2 = ((N / 4)) * (2 * 4 + (N / 4 - 1) * 4) / 2;
S3 = ((N / 12)) * (2 * 12 + (N / 12 - 1) * 12) / 2;
return S1 + S2 - S3;
}
int main()
{
int N = 20;
cout << sum(12);
return 0;
}
|
Java
class GFG{
static int sum( int N)
{
int S1, S2, S3;
S1 = ((N / 3 )) * ( 2 * 3 + (N / 3 - 1 ) * 3 ) / 2 ;
S2 = ((N / 4 )) * ( 2 * 4 + (N / 4 - 1 ) * 4 ) / 2 ;
S3 = ((N / 12 )) * ( 2 * 12 + (N / 12 - 1 ) * 12 ) / 2 ;
return S1 + S2 - S3;
}
public static void main (String[] args) {
int N = 20 ;
System.out.print(sum( 12 ));
}
}
|
Python3
def sum (N):
global S1,S2,S3
S1 = (((N / / 3 )) *
( 2 * 3 + (N / / 3 - 1 ) * 3 ) / / 2 )
S2 = (((N / / 4 )) *
( 2 * 4 + (N / / 4 - 1 ) * 4 ) / / 2 )
S3 = (((N / / 12 )) *
( 2 * 12 + (N / / 12 - 1 ) * 12 ) / / 2 )
return int (S1 + S2 - S3)
if __name__ = = '__main__' :
N = 12
print ( sum (N))
|
C#
using System;
class GFG
{
static int sum( int N)
{
int S1, S2, S3;
S1 = ((N / 3)) * (2 * 3 +
(N / 3 - 1) * 3) / 2;
S2 = ((N / 4)) * (2 * 4 +
(N / 4 - 1) * 4) / 2;
S3 = ((N / 12)) * (2 * 12 +
(N / 12 - 1) * 12) / 2;
return S1 + S2 - S3;
}
public static void Main ()
{
int N = 20;
Console.WriteLine(sum(12));
}
}
|
PHP
<?php
function sum( $N )
{
$S1 ; $S2 ; $S3 ;
$S1 = (( $N / 3)) * (2 * 3 +
( $N / 3 - 1) * 3) / 2;
$S2 = (( $N / 4)) * (2 * 4 +
( $N / 4 - 1) * 4) / 2;
$S3 = (( $N / 12)) * (2 * 12 +
( $N / 12 - 1) * 12) / 2;
return $S1 + $S2 - $S3 ;
}
$N = 20;
echo sum(12);
?>
|
Javascript
<script>
function sum(N)
{
var S1, S2, S3;
S1 = ((N / 3)) * (2 * 3 + (N / 3 - 1) * 3) / 2;
S2 = ((N / 4)) * (2 * 4 + (N / 4 - 1) * 4) / 2;
S3 = ((N / 12)) * (2 * 12 + (N / 12 - 1) * 12) / 2;
return S1 + S2 - S3;
}
var N = 20;
document.write( sum(12));
</script>
|
Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.
Another Approach:
Declare two integer variables n and sum, and initialize n to the value 100 for the purpose of example.
Use a for loop to iterate from 1 to n, where the variable i is the loop counter.
For each iteration, check whether i is divisible by 3 or 4 using the modulo operator %. If the condition is true, add i to the variable sum.
After the loop has completed, print out the value of sum using printf.
Return 0 to indicate that the program has executed successfully.
C
#include <stdio.h>
int main() {
int n = 100;
int sum = 0;
for ( int i = 1; i <= n; i++) {
if (i % 3 == 0 || i % 4 == 0) {
sum += i;
}
}
printf ( "Sum of numbers from 1 to %d which are divisible by 3 or 4 is %d\n" , n, sum);
return 0;
}
|
Java
public class Main {
public static void main(String[] args) {
int n = 100 ;
int sum = 0 ;
for ( int i = 1 ; i <= n; i++) {
if (i % 3 == 0 || i % 4 == 0 ) {
sum += i;
}
}
System.out.printf( "Sum of numbers from 1 to %d which are divisible by 3 or 4 is %d\n" , n, sum);
}
}
|
Python3
n = 100
sum = 0
for i in range ( 1 , n + 1 ):
if i % 3 = = 0 or i % 4 = = 0 :
sum + = i
print (f "Sum of numbers from 1 to {n} which are divisible by 3 or 4 is {sum}" )
|
C++
#include <iostream>
using namespace std;
int main()
{
int n = 100;
int sum = 0;
for ( int i = 1; i <= n; i++) {
if (i % 3 == 0 || i % 4 == 0) {
sum += i;
}
}
cout << "Sum of numbers from 1 to " << n
<< " which are divisible by 3 or 4 is " << sum
<< endl;
return 0;
}
|
C#
using System;
public class MainClass {
public static void Main() {
int n = 100;
int sum = 0;
for ( int i = 1; i <= n; i++) {
if (i % 3 == 0 || i % 4 == 0) {
sum += i;
}
}
Console.WriteLine($ "Sum of numbers from 1 to {n} which are divisible by 3 or 4 is {sum}" );
}
}
|
Javascript
const n = 100;
let sum = 0;
for (let i = 1; i <= n; i++) {
if (i % 3 === 0 || i % 4 === 0) {
sum += i;
}
}
console.log(`Sum of numbers from 1 to ${n} which are divisible by 3 or 4 is ${sum}`);
|
Output
Sum of numbers from 1 to 100 which are divisible by 3 or 4 is 2551
The time complexity of this program is O(n), where n is the upper limit of the range of numbers to be considered.
The space complexity is O(1), as the memory usage is constant regardless of the value of n.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...