Sum of numbers from 1 to N which are divisible by 3 or 4
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++
// C++ program to find sum of numbers from 1 to N // which are divisible by 3 or 4 #include <bits/stdc++.h> using namespace std; // Function to calculate the sum // of numbers divisible by 3 or 4 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; } // Driver code int main() { int N = 20; cout << sum(12); return 0; } |
Java
// Java program to find sum of numbers from 1 to N // which are divisible by 3 or 4 class GFG{ // Function to calculate the sum // of numbers divisible by 3 or 4 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; } // Driver code public static void main (String[] args) { int N = 20; System.out.print(sum(12)); } } |
Python3
# Python3 program to find sum of numbers # from 1 to N # which are divisible by 3 or 4 # Function to calculate the sum # of numbers divisible by 3 or 4 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)) # This code is contributed by Shrikant13 |
C#
// C# program to find sum of // numbers from 1 to N which // are divisible by 3 or 4 using System; class GFG { // Function to calculate the sum // of numbers divisible by 3 or 4 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; } // Driver code public static void Main () { int N = 20; Console.WriteLine(sum(12)); } } // This code is contributed // by inder_verma |
PHP
<?php // PHP program to find sum of // numbers from 1 to N which // are divisible by 3 or 4 // Function to calculate the sum // of numbers divisible by 3 or 4 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; } // Driver Code $N = 20; echo sum(12); // This code is contributed // by inder_verma ?> |
42
Recommended Posts:
- Count numbers which are divisible by all the numbers from 2 to 10
- Sum of first N natural numbers which are divisible by 2 and 7
- Sum of first N natural numbers which are divisible by X or Y
- Divisible by 37 for large numbers
- Sum of the numbers upto N that are divisible by 2 or 5
- Sum of all numbers divisible by 6 in a given range
- Count the numbers divisible by 'M' in a given range
- Smallest number divisible by first n numbers
- Count numbers in range 1 to N which are divisible by X but not by Y
- Numbers that are not divisible by any number in the range [2, 10]
- Print k numbers where all pairs are divisible by m
- Sum of n digit numbers divisible by a given number
- Largest N digit number divisible by given three numbers
- Smallest n digit number divisible by given three numbers
- Number of pairs from the first N natural numbers whose sum is divisible by K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

