Sum of all subsets of a set formed by first n natural numbers

A set is a collection of data elements. A subset of a set is a set formed by selecting some or all elements from the parent set. For example, B is a subset of A if all elements of B exist in A.

Here we need to find the sum of all subsets of a set formed by first n natural numbers. This means we need to find all possible subsets and then add up all their elements.

Understanding the Problem

Let's take an example with N = 3:

Set = {1, 2, 3}

All possible subsets = {{}, {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}}

Sum of elements in all subsets = 0 + 1 + 2 + 3 + (1+2) + (1+3) + (2+3) + (1+2+3) = 0 + 1 + 2 + 3 + 3 + 4 + 5 + 6 = 24

If we rearrange the sum: 1+1+1+1 + 2+2+2+2 + 3+3+3+3 = 4(1+2+3) = 4×6 = 24

Mathematical Formula

For a set of first n natural numbers, each element appears in exactly 2^(n-1) subsets. Therefore, the total sum is:

Formula: 2^(n-1) × (sum of first n natural numbers) = 2^(n-1) × n(n+1)/2

Syntax

int subsetSum = power(2, n-1) * n * (n+1) / 2;

Example 1: Simple Approach

#include <stdio.h>

int power(int base, int exp) {
    int result = 1;
    for (int i = 0; i < exp; i++) {
        result *= base;
    }
    return result;
}

int main() {
    int n = 3;
    
    /* Sum of first n natural numbers */
    int sum = n * (n + 1) / 2;
    
    /* Each element appears in 2^(n-1) subsets */
    int total = power(2, n - 1) * sum;
    
    printf("For n = %d:<br>", n);
    printf("Sum of all subsets = %d<br>", total);
    
    return 0;
}
For n = 3:
Sum of all subsets = 24

Example 2: Handling Large Numbers with Modular Arithmetic

#include <stdio.h>
#define MOD 1000000007

long long power(long long base, long long exp, long long mod) {
    long long result = 1;
    base = base % mod;
    while (exp > 0) {
        if (exp & 1) {
            result = (result * base) % mod;
        }
        exp = exp >> 1;
        base = (base * base) % mod;
    }
    return result;
}

int main() {
    int n = 10;
    
    /* Calculate 2^(n-1) mod MOD */
    long long pow2 = power(2, n - 1, MOD);
    
    /* Calculate n * (n + 1) / 2 mod MOD */
    long long sum = ((long long)n * (n + 1) / 2) % MOD;
    
    /* Final result */
    long long result = (pow2 * sum) % MOD;
    
    printf("For n = %d:<br>", n);
    printf("Sum of all subsets = %lld<br>", result);
    
    return 0;
}
For n = 10:
Sum of all subsets = 28160

How It Works

  • For n natural numbers {1, 2, 3, ..., n}, there are 2^n total subsets
  • Each number appears in exactly 2^(n-1) subsets
  • The sum of all elements across all subsets is 2^(n-1) × (1 + 2 + ... + n)
  • Using the formula for sum of first n natural numbers: n(n+1)/2

Conclusion

The sum of all subsets of first n natural numbers can be efficiently calculated using the formula 2^(n-1) × n(n+1)/2. For large values of n, modular arithmetic prevents integer overflow while maintaining accuracy.

Updated on: 2026-03-15T11:34:05+05:30

316 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements