Count ways to express even number ‘n’ as sum of even integers
Last Updated :
27 Apr, 2021
Given an positive even integer ‘n’. Count total number of ways to express ‘n’ as sum of even positive integers. Output the answer in modulo 109 + 7
Examples:
Input: 6
Output: 4
Explanation
There are only four ways to write 6
as sum of even integers:
1. 2 + 2 + 2
2. 2 + 4
3. 4 + 2
4. 6
Input: 8
Output: 8
Approach is to find pattern or recursive function whichever is possible. The approach would be the same as already discussed in “Count ways to express ‘n’ as sum of odd integers“. Here the given number is even that means even sum can only be achieved by adding the (n-2)th number as two times. We can notice that (by taking some examples) adding a 2 to a number doubles the count. Let the total number of ways to write ‘n’ be ways(n). The value of ‘ways(n)’ can be written by formula as follows:
ways(n) = ways(n-2) + ways(n-2)
ways(n) = 2 * ways(n-2)
ways(2) = 1 = 20
ways(4) = 2 = 21
ways(6) = 4 = 22
ways(8) = 8 = 23
''
''
''
ways(2 * n) = 2n-1
Replace n by (m / 2)
=> ways(m) = 2m/2 - 1
C++
#include<iostream>
using namespace std;
const int MOD = 1e9 + 7;
int power( int x, unsigned int y, int p)
{
int res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (1LL * res * x) % p;
y = y>>1;
x = (1LL * x * x) % p;
}
return res;
}
int countEvenWays( int n)
{
return power(2, n/2 - 1, MOD);
}
int main()
{
int n = 6;
cout << countEvenWays(n) << "\n" ;
n = 8;
cout << countEvenWays(n);
return 0;
}
|
Java
class GFG {
static int MOD = 1000000007 ;
static int power( int x, int y, int p)
{
int res = 1 ;
x = x % p;
while (y > 0 )
{
if (y % 2 == 1 )
res = ( 1 * res * x) % p;
y = y >> 1 ;
x = ( 1 * x * x) % p;
}
return res;
}
static int countEvenWays( int n)
{
return power( 2 , n/ 2 - 1 , MOD);
}
public static void main(String args[])
{
int n = 6 ;
System.out.println(countEvenWays(n));
n = 8 ;
System.out.println(countEvenWays(n));
}
}
|
Python
MOD = 1e9 + 7
def power(x, y, p) :
res = 1
x = x % p
while (y > 0 ) :
if (y & 1 ) :
res = ( 1 * res * x) % p
y = y >> 1
x = ( 1 * x * x) % p
return res
def countEvenWays(n) :
return power( 2 , n / 2 - 1 , MOD)
n = 6
print ( int (countEvenWays(n)))
n = 8
print ( int (countEvenWays(n)))
|
C#
using System;
class GFG {
static int MOD = 1000000007;
static int power( int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0)
{
if (y % 2 == 1)
res = (1 * res * x) % p;
y = y >> 1;
x = (1 * x * x) % p;
}
return res;
}
static int countEvenWays( int n)
{
return power(2, n/2 - 1, MOD);
}
public static void Main()
{
int n = 6;
Console.WriteLine(countEvenWays(n));
n = 8;
Console.WriteLine(countEvenWays(n));
}
}
|
PHP
<?php
$MOD = 1000000000.0;
function power( $x , $y , $p )
{
$res = 1;
$x = $x % $p ;
while ( $y > 0)
{
if ( $y & 1)
$res = (1 * $res *
$x ) % $p ;
$y = $y >> 1;
$x = (1 * $x *
$x ) % $p ;
}
return $res ;
}
function countEvenWays( $n )
{
global $MOD ;
return power(2, $n /
2 - 1, $MOD );
}
$n = 6;
echo countEvenWays( $n ), "\n" ;
$n = 8;
echo countEvenWays( $n );
?>
|
Javascript
<script>
let MOD = 1000000007;
function power(x, y, p)
{
let res = 1;
x = x % p;
while (y > 0)
{
if (y % 2 == 1)
res = (1 * res * x) % p;
y = y >> 1;
x = (1 * x * x) % p;
}
return res;
}
function countEvenWays(n)
{
return power(2, n/2 - 1, MOD);
}
let n = 6;
document.write(countEvenWays(n) + "<br/>" );
n = 8;
document.write(countEvenWays(n));
</script>
|
Output:
4
8
Time complexity: O(Log(n))
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...