Sum of numbers from 1 to N which are divisible by 3 or 4
Last Updated :
24 Jun, 2024
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));
}
}
Python
# 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
JavaScript
<script>
// Javascript 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)
{
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;
}
// Driver code
var N = 20;
document.write( sum(12));
</script>
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
?>
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 <iostream>
using namespace std;
int main()
{
int n = 100; // assuming n is 100 for example purposes
int sum = 0;
// Loop through all numbers from 1 to n
for (int i = 1; i <= n; i++) {
// Check if the current number is divisible by 3 or
// 4
if (i % 3 == 0 || i % 4 == 0) {
// If the current number is divisible by 3 or 4,
// add it to the sum
sum += i;
}
}
// Print the sum of all numbers divisible by 3 or 4
cout << "Sum of numbers from 1 to " << n
<< " which are divisible by 3 or 4 is " << sum
<< endl;
return 0;
}
// This code is contributed by sarojmcy2e
C
#include <stdio.h>
int main() {
int n = 100; // assuming n is 100 for example purposes
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
// Java program for the above approach
public class Main {
public static void main(String[] args) {
int n = 100; // assuming n is 100 for example purposes
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);
}
}
// Contributed by adityasha4x71
Python
# Python program for the above approach
n = 100 # assuming n is 100 for example purposes
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#
using System;
public class MainClass {
public static void Main() {
int n = 100; // assuming n is 100 for example purposes
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; // assuming n is 100 for example purposes
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}`);
OutputSum 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.
Similar Reads
Sum of first K numbers which are not divisible by N
Given two numbers N and K, the task is to find the sum of first K numbers which are not divisible by N.Examples: Input: N = 5, K = 10 Output: 63 Explanation: Sum of { 1, 2, 3, 4, 6, 7, 8, 9, 11, 12 } is 63. Input: N = 3, k = 13 Output: 127 Explanation: Sum of { 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16,
9 min read
Sum of first N natural numbers which are divisible by 2 and 7
Given a number N. The task is to find the sum of all those numbers from 1 to N that are divisible by 2 or by 7.Examples: Input : N = 7Output : 19sum = 2 + 4 + 6 + 7Input : N = 14 Output : 63sum = 2 + 4 + 6 + 7 + 8 + 10 + 12 + 14 Brute Force Approach: A brute force approach to solve this problem woul
8 min read
Sum of the numbers upto N that are divisible by 2 or 5
Given a number n. The task is to find the sum of numbers up to n, that are divisible by 2 or 5.Examples: Input: n = 2 Output: 2 Input: n = 5 Output: 11 A naive approach is to just iterate over the numbers up to n and check if it divisible by 2 or 5. If it is divisible then just add this number to ou
7 min read
Sum of all numbers in the given range which are divisible by M
Given three numbers A, B and M such that A < B, the task is to find the sum of numbers divisible by M in the range [A, B]. Examples: Input: A = 25, B = 100, M = 30 Output: 180 Explanation: In the given range [25, 100] 30, 60 and 90 are the numbers which are divisible by M = 30 Therefore, sum of t
15 min read
Count numbers which are divisible by all the numbers from 2 to 10
Given an integer N, the task is to find the count of numbers from 1 to N which are divisible by all the numbers from 2 to 10. Examples: Input: N = 3000 Output: 1 2520 is the only number below 3000 which is divisible by all the numbers from 2 to 10. Input: N = 2000 Output: 0 Approach: Let's factorize
3 min read
Maximize the number of sum pairs which are divisible by K
Given an array of N integers and an integer K. The task is to print the maximum number of pairs(a[i]+a[j]) possible which are divisible by K. Note: A particular index number cannot be considered in more than one pair. Examples: Input: a[] = {1, 2, 2, 3, 2, 4, 10}, k =2 Output: 3 The pairs are: (1, 2
9 min read
Split a number as sum of K numbers which are not divisible by K
Given two numbers N and K, the task is to split this number into K positive integers such that their sum is equal to N and none of these K integers is a multiple of K. Note: N>=2 Examples: Input: N = 10, K = 3 Output: 1, 1, 8Input: N = 18, K = 3 Output:1, 1, 16 Approach: To split N into K numbers
5 min read
Count pairs of numbers from 1 to N with Product divisible by their Sum
Given a number N . The task is to count pairs (x, y) such that x*y is divisible by (x+y) and the condition 1 <= x < y < N holds true.Examples: Input : N = 6 Output : 1 Explanation: The only pair is (3, 6) which satisfies all of the given condition, 3<6 and 18%9=0. Input : N = 15 Output :
4 min read
Count of pairs from 1 to a and 1 to b whose sum is divisible by N
Given three integers a, b and N. Find the total number of distinct pairs which can be formed by selecting one integer from 1 to a and other from 1 to b, such that their sum is divisible by N. Examples: Input : a = 4, b = 4, N = 4 Output : 4 Input : a = 5, b = 13, N = 3 Output : 22 Basic Approach: Fo
15+ min read
Sum of all numbers divisible by 6 in a given range
Given a range L-R, find the sum of all numbers divisible by 6 in range L-RL and R are very large.Examples: Input : 1 20 Output : 36 Explanation: 6 + 12 + 18 = 36 Input : 5 7 Output : 6 Explanation: 6 is the only divisible number in range 5-7 A naive approach is be to run a loop from L to R and sum u
6 min read