Sum of sum of all subsets of a set formed by first N natural numbers

Given N, and ff(N) = f(1) + f(2) + …… + f(N), where f(k) is the sum of all subsets of a set formed by first k natural numbers. The task is to find ff(N) modulo 1000000007.

Examples:

Input: 2
Output: 7
f(1) + f(2)
f(1) = 1 = 1
f(2) = 1 + 2 + {1 + 2} = 6

Input: 3
Output: 31
f(1) + f(2) + f(3)
f(1) = 1 = 1
f(2) = 1 + 2 + {1 + 2} = 6
f(3) = 1 + 2 + 3 + {1 + 2} + {2 + 3} + {1 + 3} + {1 + 2 + 3} = 24



Approach: Find a pattern of the sequence that will form. The values of f(1), f(2), f(3) are 1, 6 and 31 respectively. Let’s find f(4).

f(4) =  1 + 2 + 3 + 4 + {1 + 2} + {1 + 3} + {1 + 4} 
        + {2 + 3} + {2 + 4} + {3 + 4} + {1 + 2 + 3} + {1 + 2 + 4}
        + {1 + 3 + 4} + {2 + 3 + 4} + {1 + 2 + 3 + 4} = 80.

Hence ff(N) will be

ff(1) = f(1) = 1
ff(2) = f(1) + f(2) = 7
ff(3) = f(1) + f(2) + f(3) = 31
ff(4) = f(1) + f(2) + f(3) + f(4) = 111
.
.
.

The series formed is 1, 7, 31, 111… There exists a formula for it which is 2^n*(n^2 + n + 2) – 1. where, N is starting from zero.

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find Sum of all
// subsets of a set formed by
// first N natural numbers | Set-2
#include <bits/stdc++.h>
using namespace std;
  
// modulo value
#define mod (int)(1e9 + 7)
  
// Iterative Function to calculate (x^y)%p in O(log y)
int power(int x, int y, int p)
{
    int res = 1; // Initialize result
  
    x = x % p; // Update x if it is more than or
    // equal to p
  
    while (y > 0) {
  
        // If y is odd, multiply x with the result
        if (y & 1)
            res = (res * x) % p;
  
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
  
// function to find ff(n)
int check(int n)
{
    // In formula n is starting from zero
    n--;
  
    // calculate answer using
    // formula 2^n*(n^2 + n + 2) - 1
    int ans = n * n;
  
    // whenever answer is greater than
    // or equals to mod then modulo it.
    if (ans >= mod)
        ans %= mod;
  
    ans += n + 2;
  
    if (ans >= mod)
        ans %= mod;
  
    ans = (power(2, n, mod) % mod * ans % mod) % mod;
  
    // adding modulo while substraction is very necessary
    // otherwise it will cause wrong answer
    ans = (ans - 1 + mod) % mod;
  
    return ans;
}
  
// Driver code
int main()
{
    int n = 4;
  
    // function call
    cout << check(n) << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find Sum of all
// subsets of a set formed by
// first N natural numbers | Set-2
   
class Geeks {
       
// Iterative Function to calculate
// (x^y)%p in O(log y)
static int power(int x, int y, int p)
{
      
    // Initialize result
    int res = 1
   
    // Update x if it is more 
    // than or equal to p
    x = x % p; 
   
    while (y > 0) {
   
        // If y is odd, multiply x
        // with the result
        if (y != 0)
            res = (res * x) % p;
   
        // y must be even now
        // y = y / 2
        y = y >> 1
        x = (x * x) % p;
    }
    return res;
}
   
// function to find ff(n)
static int check(int n)
{
       
    // modulo value
    int  mod = (int)(1e9 + 7);
   
    // In formula n is 
    // starting from zero
    n--;
   
    // calculate answer using
    // formula 2^n*(n^2 + n + 2) - 1
    int ans = n * n;
   
    // whenever answer is greater than
    // or equals to mod then modulo it.
    if (ans >= mod)
        ans %= mod;
   
    ans += n + 2;
   
    if (ans >= mod)
        ans %= mod;
   
    ans = (power(2, n, mod) % mod *
                  ans % mod) % mod;
   
    // adding modulo while substraction
    // is very necessary otherwise it 
    // will cause wrong answer
    ans = (ans - 1 + mod) % mod;
   
    return ans;
}
   
// Driver Code
public static void main(String args[])
{
    int n = 4;
   
    // function call
    System.out.println(check(n));
}
}
   
// This code is contributed by ankita_saini

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

#Python3 program to find Sum of all 
# subsets of a set formed by 
# first N natural numbers | Set-2 
  
  
  
# modulo value 
mod = (int)(1e9 + 7
  
# Iterative Function to calculate (x^y)%p in O(log y) 
def power(x,y,p): 
    res = 1 # Initialize result 
  
    x = x % p # Update x if it is more than or 
    # equal to p 
  
    while (y > 0):
  
        # If y is odd, multiply x with the result 
        if (y & 1): 
            res = (res * x) %
  
        # y must be even now 
        y = y >> 1 # y = y/2 
        x = (x * x) %
    return res 
  
# function to find ff(n) 
def check(n): 
    # In formula n is starting from zero 
    n=n-1
  
    # calculate answer using 
    # formula 2^n*(n^2 + n + 2) - 1 
    ans = n *
  
    # whenever answer is greater than 
    # or equals to mod then modulo it. 
    if (ans >= mod): 
        ans %= mod 
  
    ans += n + 2
  
    if (ans >= mod): 
        ans %= mod 
  
    ans = (pow(2, n, mod) % mod * ans % mod) % mod 
  
    # adding modulo while substraction is very necessary 
    # otherwise it will cause wrong answer 
    ans = (ans - 1 + mod) % mod 
  
    return ans 
  
#Driver code 
if __name__=='__main__':
    n = 4
  
# function call 
    print(check(n)) 
  
# This code is contributed by ash264

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find Sum 
// of all subsets of a set 
// formed by first N natural
// numbers | Set-2
using System;
  
class GFG
{
      
// Iterative Function 
// to calculate (x^y)%p 
// in O(log y)
static int power(int x, int y,
                 int p)
{
      
    // Initialize result
    int res = 1; 
  
    // Update x if it is more 
    // than or equal to p
    x = x % p; 
  
    while (y > 0) 
    {
  
        // If y is odd, multiply 
        // x with the result
        if (y != 0)
            res = (res * x) % p;
  
        // y must be even 
        // now y = y / 2
        y = y >> 1; 
        x = (x * x) % p;
    }
    return res;
}
  
// function to find ff(n)
static int check(int n)
{
      
    // modulo value
    int mod = (int)(1e9 + 7);
  
    // In formula n is 
    // starting from zero
    n--;
  
    // calculate answer 
    // using formula 
    // 2^n*(n^2 + n + 2) - 1
    int ans = n * n;
  
    // whenever answer is 
    // greater than or equals 
    // to mod then modulo it.
    if (ans >= mod)
        ans %= mod;
  
    ans += n + 2;
  
    if (ans >= mod)
        ans %= mod;
  
    ans = (power(2, n, mod) % mod *
                 ans % mod) % mod;
  
    // adding modulo while 
    // substraction is very 
    // necessary otherwise it 
    // will cause wrong answer
    ans = (ans - 1 + mod) % mod;
  
    return ans;
}
  
// Driver Code
public static void Main(String []args)
{
    int n = 4;
  
    // function call
    Console.WriteLine(check(n));
}
}
  
// This code is contributed
// by ankita_saini

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find Sum 
// of all subsets of a set 
// formed by first N natural 
// numbers | Set-2
  
// modulo value
  
// Iterative Function to
// calculate (x^y)%p in O(log y)
function power($x, $y, $p)
{
    $res = 1; // Initialize result
  
    $x = $x % $p; // Update x if it 
                  // is more than or
                  // equal to p
  
    while ($y > 0)
    {
  
        // If y is odd, multiply
        // x with the result
        if ($y & 1)
            $res = ($res * $x) % $p;
  
        // y must be even now
        $y = $y >> 1; // y = y/2
        $x = ($x * $x) % $p;
    }
    return $res;
}
  
// function to find ff(n)
function check($n)
{
    $mod = 1e9+7;
      
    // In formula n is 
    // starting from zero
    $n--;
  
    // calculate answer using
    // formula 2^n*(n^2 + n + 2) - 1
    $ans = $n * $n;
  
    // whenever answer is greater 
    // than or equals to mod then
    // modulo it.
    if ($ans >= $mod)
        $ans %= $mod;
  
    $ans += $n + 2;
  
    if ($ans >= $mod)
        $ans %= $mod;
  
    $ans = (power(2, $n, $mod) %
                   $mod * $ans
                   $mod) % $mod;
  
    // adding modulo while substraction 
    // is very necessary otherwise it
    // will cause wrong answer
    $ans = ($ans - 1 + $mod) % $mod;
  
    return $ans;
}
  
// Driver code
$n = 4;
  
// function call
echo check($n) ."\n";
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)
?>

chevron_right


Output:

111

Time complexity: O(log n).



My Personal Notes arrow_drop_up

pawanasipugmailcom

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.