
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Sum of all subsets of a set formed by first n natural numbers
A Set is a collection of data elements. Subset of a set is a set formed by only the elements after 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 found by first n natural numbers. this means I need to find all subsets that can be formed and then adding them. Let's take an example,
N = 3
Set = {1,2,3}
subsets formed = { {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3,} }
Sum = 1+1+2+1+3+2+2+3+3+1+2+3 = 24
Lets rearrange the sum, 1+1+1+1+2+2+2+2+3+3+3 = 4(1+2+3) = 24
There exists a mathematical formula for this type of series, General formula of the series is 2^n*(n^2 + n + 2) – 1.
Example
#include <stdio.h> #define mod (int)(1e9 + 7) int power(int x, int y) { int res = 1; x = x % mod; while (y > 0) { if (y & 1) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } int main() { int n = 45; n--; int ans = n * n; if (ans >= mod) ans %= mod; ans += n + 2; if (ans >= mod) ans %= mod; ans = (power(2, n) % mod * ans % mod) % mod; ans = (ans - 1 + mod) % mod; printf("The sum of the series is %d
", ans); return 0; }
Output
The sim of the series is 2815
- Related Articles
- Sum of sum of first n natural numbers in C++
- Sum of square-sums of first n natural numbers
- Sum of first n natural numbers in C Program
- Find the sum of first $n$ odd natural numbers.
- Sum of first N natural numbers which are divisible by X or Y
- Python Program for cube sum of first n natural numbers
- C++ Program for cube sum of first n natural numbers?
- C Program for cube sum of first n natural numbers?
- Java Program to cube sum of first n natural numbers
- Finding sum of first n natural numbers in PL/SQL
- Python Program for Sum of squares of first n natural numbers
- Sum of squares of first n natural numbers in C Program?
- C++ Program for Sum of squares of first n natural numbers?
- List all the subsets of a set {m , n}
- Java Program to Display Numbers and Sum of First N Natural Numbers

Advertisements