# Count ways to express even number ‘n’ as sum of even integers

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 **10 ^{9} + 7**

**Examples:**

Input: 6 Output: 4ExplanationThere 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 = 2^{0}ways(4) = 2 = 2^{1}ways(6) = 4 = 2^{2}ways(8) = 8 = 2^{3}'' '' '' ways(2 * n) = 2^{n-1}Replace n by (m / 2) =>ways(m) = 2^{m/2 - 1}

## C++

`// C++ program to count ways to write ` `// number as sum of even integers ` `#include<iostream> ` `using` `namespace` `std; ` ` ` `// Initialize mod variable as constant ` `const` `int` `MOD = 1e9 + 7; ` ` ` `/* Iterative Function to calculate (x^y)%p in O(log y) */` `int` `power(` `int` `x, unsigned ` `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 result ` ` ` `if` `(y & 1) ` ` ` `res = (1LL * res * x) % p; ` ` ` ` ` `// y must be even now ` ` ` `y = y>>1; ` `// y = y/2 ` ` ` `x = (1LL * x * x) % p; ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Return number of ways to write 'n' ` `// as sum of even integers ` `int` `countEvenWays(` `int` `n) ` `{ ` ` ` `return` `power(2, n/2 - 1, MOD); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 6; ` ` ` `cout << countEvenWays(n) << ` `"\n"` `; ` ` ` ` ` `n = 8; ` ` ` `cout << countEvenWays(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// JAVA program to count ways to write ` `// number as sum of even integers ` ` ` `class` `GFG { ` ` ` ` ` `// Initialize mod variable as constant ` ` ` `static` `int` `MOD = ` `1000000007` `; ` ` ` ` ` `/* 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 result ` ` ` `if` `(y % ` `2` `== ` `1` `) ` ` ` `res = (` `1` `* res * x) % p; ` ` ` ` ` `// y must be even now ` ` ` `y = y >> ` `1` `; ` `// y = y/2 ` ` ` `x = (` `1` `* x * x) % p; ` ` ` `} ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Return number of ways to write ` ` ` `// 'n' as sum of even integers ` ` ` `static` `int` `countEvenWays(` `int` `n) ` ` ` `{ ` ` ` `return` `power(` `2` `, n/` `2` `- ` `1` `, MOD); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `6` `; ` ` ` `System.out.println(countEvenWays(n)); ` ` ` `n = ` `8` `; ` ` ` `System.out.println(countEvenWays(n)); ` ` ` `} ` `} ` ` ` `/* This code is contributed by Nikita Tiwari. */` |

*chevron_right*

*filter_none*

## Python

`# PYTHON program to count ways to write ` `# number as sum of even integers ` ` ` `# Initialize mod variable as constant ` `MOD ` `=` `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 result ` ` ` `if` `(y & ` `1` `) : ` ` ` `res ` `=` `(` `1` `*` `res ` `*` `x) ` `%` `p ` ` ` ` ` `# y must be even now ` ` ` `y ` `=` `y >> ` `1` `# y = y/2 ` ` ` `x ` `=` `(` `1` `*` `x ` `*` `x) ` `%` `p ` ` ` ` ` ` ` `return` `res ` ` ` ` ` `# Return number of ways to write 'n' ` `# as sum of even integers ` `def` `countEvenWays(n) : ` ` ` `return` `power(` `2` `, n` `/` `2` `-` `1` `, MOD) ` ` ` `# Driver code ` `n ` `=` `6` `print` `(` `int` `(countEvenWays(n))) ` `n ` `=` `8` `print` `(` `int` `(countEvenWays(n))) ` ` ` `# This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count ways to write ` `// number as sum of even integers ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Initialize mod variable as constant ` ` ` `static` `int` `MOD = 1000000007; ` ` ` ` ` `/* 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 result ` ` ` `if` `(y % 2 == 1) ` ` ` `res = (1 * res * x) % p; ` ` ` ` ` `// y must be even now ` ` ` `y = y >> 1; ` `// y = y/2 ` ` ` `x = (1 * x * x) % p; ` ` ` `} ` ` ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Return number of ways to write ` ` ` `// 'n' as sum of even integers ` ` ` `static` `int` `countEvenWays(` `int` `n) ` ` ` `{ ` ` ` `return` `power(2, n/2 - 1, MOD); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 6; ` ` ` `Console.WriteLine(countEvenWays(n)); ` ` ` ` ` `n = 8; ` ` ` `Console.WriteLine(countEvenWays(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count ways ` `// to write number as sum of ` `// even integers ` ` ` `// Initialize mod variable ` `// as constant ` `$MOD` `= 1000000000.0; ` ` ` `/* Iterative Function to ` `calculate (x^y)%p in O(log y) */` `function` `power(` `$x` `, ` `$y` `, ` `$p` `) ` `{ ` ` ` `// Initialize result ` ` ` `$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 result ` ` ` `if` `(` `$y` `& 1) ` ` ` `$res` `= (1 * ` `$res` `* ` ` ` `$x` `) % ` `$p` `; ` ` ` ` ` `// y must be even now ` ` ` `$y` `= ` `$y` `>> 1; ` `// y = y/2 ` ` ` `$x` `= (1 * ` `$x` `* ` ` ` `$x` `) % ` `$p` `; ` ` ` `} ` ` ` `return` `$res` `; ` `} ` ` ` `// Return number of ways ` `// to write 'n' as sum of ` `// even integers ` `function` `countEvenWays(` `$n` `) ` `{ ` ` ` `global` `$MOD` `; ` ` ` `return` `power(2, ` `$n` `/ ` ` ` `2 - 1, ` `$MOD` `); ` `} ` ` ` `// Driver code ` `$n` `= 6; ` `echo` `countEvenWays(` `$n` `), ` `"\n"` `; ` ` ` `$n` `= 8; ` `echo` `countEvenWays(` `$n` `); ` ` ` `// This code is contributed ` `// by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4 8

**Time complexity: **O(Log(n))

**Auxiliary space: **O(1)

This article is contributed by Shubham Bansal. 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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count ways to express 'n' as sum of odd integers
- Count ways to express a number as sum of exactly two numbers
- Count ways to express a number as sum of consecutive numbers
- Count of different ways to express N as the sum of 1, 3 and 4
- Ways to express a number as product of two different factors
- Number of ways in which N can be represented as the sum of two positive integers
- Number of ways to write N as a sum of K non-negative integers
- Number of ways to form a heap with n distinct integers
- Count number of ways to get Odd Sum
- Count number of ways to divide a number in 4 parts
- Count of integers up to N which represent a Binary number
- Count number of integers less than or equal to N which has exactly 9 divisors
- Count number of ways to arrange first N numbers
- Count number of ways to cover a distance | Set 2
- Count the number of ways to divide N in k groups incrementally
- Count number of ways to reach a given score in a Matrix
- Count ways to spell a number with repeated digits
- Count of ways to represent N as sum of a prime number and twice of a square
- Count of ways to split a given number into prime segments
- Count number of ways to divide an array into two halves with same sum