# Ways to represent a number as a sum of 1’s and 2’s

Given a positive integer **N**. The task is to find the number of ways of representing N as a sum of 1s and 2s.

**Examples:**

Input : N = 3 Output : 3 3 can be represented as (1+1+1), (2+1), (1+2). Input : N = 5 Output : 8

For N = 1, answer is 1.

For N = 2. (1 + 1), (2), answer is 2.

For N = 3. (1 + 1 + 1), (2 + 1), (1 + 2), answer is 3.

For N = 4. (1 + 1 + 1 + 1), (2 + 1 + 1), (1 + 2 + 1), (1 + 1 + 2), (2 + 2) answer is 5.

And so on.

It can be observe that it form Fibonacci Series. So, the number of ways of representing N as a sum of 1s and 2s is (N + 1)^{th} Fibonacci number.

*How ?*

We can easily see that the recursive function is exactly same as Fibonacci Numbers. To obtain the sum of N, we can add 1 to N – 1. Also, we can add 2 to N – 2. And only 1 and 2 are allowed to make the sum N. So, to obtain sum N using 1s and 2s, total ways are: number of ways to obtain (N – 1) + number of ways to obtain (N – 2).

We can find N’th Fibonacci Number in O(Log n) time. Please refer method 5 of this post.

Below is the implementation of this approach:

## C++

`// C++ program to find number of ways to representing ` `// a number as a sum of 1's and 2's ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to multiply matrix. ` `void` `multiply(` `int` `F[2][2], ` `int` `M[2][2]) ` `{ ` ` ` `int` `x = F[0][0]*M[0][0] + F[0][1]*M[1][0]; ` ` ` `int` `y = F[0][0]*M[0][1] + F[0][1]*M[1][1]; ` ` ` `int` `z = F[1][0]*M[0][0] + F[1][1]*M[1][0]; ` ` ` `int` `w = F[1][0]*M[0][1] + F[1][1]*M[1][1]; ` ` ` ` ` `F[0][0] = x; ` ` ` `F[0][1] = y; ` ` ` `F[1][0] = z; ` ` ` `F[1][1] = w; ` `} ` ` ` `// Power function in log n ` `void` `power(` `int` `F[2][2], ` `int` `n) ` `{ ` ` ` `if` `( n == 0 || n == 1) ` ` ` `return` `; ` ` ` `int` `M[2][2] = {{1,1},{1,0}}; ` ` ` ` ` `power(F, n/2); ` ` ` `multiply(F, F); ` ` ` ` ` `if` `(n%2 != 0) ` ` ` `multiply(F, M); ` `} ` ` ` `/* function that returns (n+1)th Fibonacci number ` ` ` `Or number of ways to represent n as sum of 1's ` ` ` `2's */` `int` `countWays(` `int` `n) ` `{ ` ` ` `int` `F[2][2] = {{1,1},{1,0}}; ` ` ` `if` `(n == 0) ` ` ` `return` `0; ` ` ` `power(F, n); ` ` ` `return` `F[0][0]; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `cout << countWays(n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find number of ` `// ways to representing a number ` `// as a sum of 1's and 2's ` `class` `GFG ` `{ ` ` ` `// Function to multiply matrix. ` ` ` `static` `void` `multiply(` `int` `F[][], ` `int` `M[][]) ` ` ` `{ ` ` ` `int` `x = F[` `0` `][` `0` `] * M[` `0` `][` `0` `] + F[` `0` `][` `1` `] * M[` `1` `][` `0` `]; ` ` ` `int` `y = F[` `0` `][` `0` `] * M[` `0` `][` `1` `] + F[` `0` `][` `1` `] * M[` `1` `][` `1` `]; ` ` ` `int` `z = F[` `1` `][` `0` `] * M[` `0` `][` `0` `] + F[` `1` `][` `1` `] * M[` `1` `][` `0` `]; ` ` ` `int` `w = F[` `1` `][` `0` `] * M[` `0` `][` `1` `] + F[` `1` `][` `1` `] * M[` `1` `][` `1` `]; ` ` ` ` ` `F[` `0` `][` `0` `] = x; ` ` ` `F[` `0` `][` `1` `] = y; ` ` ` `F[` `1` `][` `0` `] = z; ` ` ` `F[` `1` `][` `1` `] = w; ` ` ` `} ` ` ` ` ` `// Power function in log n ` ` ` `static` `void` `power(` `int` `F[][], ` `int` `n) ` ` ` `{ ` ` ` `if` `(n == ` `0` `|| n == ` `1` `) ` ` ` `{ ` ` ` `return` `; ` ` ` `} ` ` ` `int` `M[][] = {{` `1` `, ` `1` `}, {` `1` `, ` `0` `}}; ` ` ` ` ` `power(F, n / ` `2` `); ` ` ` `multiply(F, F); ` ` ` ` ` `if` `(n % ` `2` `!= ` `0` `) ` ` ` `{ ` ` ` `multiply(F, M); ` ` ` `} ` ` ` `} ` ` ` ` ` `/* function that returns (n+1)th Fibonacci number ` ` ` `Or number of ways to represent n as sum of 1's ` ` ` `2's */` ` ` `static` `int` `countWays(` `int` `n) ` ` ` `{ ` ` ` `int` `F[][] = {{` `1` `, ` `1` `}, {` `1` `, ` `0` `}}; ` ` ` `if` `(n == ` `0` `) ` ` ` `{ ` ` ` `return` `0` `; ` ` ` `} ` ` ` `power(F, n); ` ` ` `return` `F[` `0` `][` `0` `]; ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `System.out.println(countWays(n)); ` ` ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find number of ways to ` `# representing a number as a sum of 1's and 2's ` ` ` `# Function to multiply matrix. ` `def` `multiply(F, M): ` ` ` ` ` `x ` `=` `F[` `0` `][` `0` `] ` `*` `M[` `0` `][` `0` `] ` `+` `F[` `0` `][` `1` `] ` `*` `M[` `1` `][` `0` `] ` ` ` `y ` `=` `F[` `0` `][` `0` `] ` `*` `M[` `0` `][` `1` `] ` `+` `F[` `0` `][` `1` `] ` `*` `M[` `1` `][` `1` `] ` ` ` `z ` `=` `F[` `1` `][` `0` `] ` `*` `M[` `0` `][` `0` `] ` `+` `F[` `1` `][` `1` `] ` `*` `M[` `1` `][` `0` `] ` ` ` `w ` `=` `F[` `1` `][` `0` `] ` `*` `M[` `0` `][` `1` `] ` `+` `F[` `1` `][` `1` `] ` `*` `M[` `1` `][` `1` `] ` ` ` ` ` `F[` `0` `][` `0` `] ` `=` `x ` ` ` `F[` `0` `][` `1` `] ` `=` `y ` ` ` `F[` `1` `][` `0` `] ` `=` `z ` ` ` `F[` `1` `][` `1` `] ` `=` `w ` ` ` `# Power function in log n ` `def` `power(F, n): ` ` ` ` ` `if` `( n ` `=` `=` `0` `or` `n ` `=` `=` `1` `): ` ` ` `return` ` ` `M ` `=` `[[` `1` `, ` `1` `],[` `1` `, ` `0` `]] ` ` ` ` ` `power(F, n ` `/` `/` `2` `) ` ` ` `multiply(F, F) ` ` ` ` ` `if` `(n ` `%` `2` `!` `=` `0` `): ` ` ` `multiply(F, M) ` ` ` `#/* function that returns (n+1)th Fibonacci number ` `# Or number of ways to represent n as sum of 1's ` `# 2's */ ` `def` `countWays(n): ` ` ` `F ` `=` `[[` `1` `, ` `1` `], [` `1` `, ` `0` `]] ` ` ` `if` `(n ` `=` `=` `0` `): ` ` ` `return` `0` ` ` `power(F, n) ` ` ` ` ` `return` `F[` `0` `][` `0` `] ` ` ` `# Driver Code ` `n ` `=` `5` `print` `(countWays(n)) ` ` ` `# This code is contributed by mohit kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find number of ` `// ways to representing a number ` `// as a sum of 1's and 2's ` `class` `GFG ` `{ ` ` ` ` ` `// Function to multiply matrix. ` ` ` `static` `void` `multiply(` `int` `[,]F, ` `int` `[,]M) ` ` ` `{ ` ` ` `int` `x = F[0,0] * M[0,0] + F[0,1] * M[1,0]; ` ` ` `int` `y = F[0,0] * M[0,1] + F[0,1] * M[1,1]; ` ` ` `int` `z = F[1,0] * M[0,0] + F[1,1] * M[1,0]; ` ` ` `int` `w = F[1,0] * M[0,1] + F[1,1] * M[1,1]; ` ` ` ` ` `F[0,0] = x; ` ` ` `F[0,1] = y; ` ` ` `F[1,0] = z; ` ` ` `F[1,1] = w; ` ` ` `} ` ` ` ` ` `// Power function in log n ` ` ` `static` `void` `power(` `int` `[,]F, ` `int` `n) ` ` ` `{ ` ` ` `if` `(n == 0 || n == 1) ` ` ` `{ ` ` ` `return` `; ` ` ` `} ` ` ` `int` `[,]M = {{1, 1}, {1, 0}}; ` ` ` ` ` `power(F, n / 2); ` ` ` `multiply(F, F); ` ` ` ` ` `if` `(n % 2 != 0) ` ` ` `{ ` ` ` `multiply(F, M); ` ` ` `} ` ` ` `} ` ` ` ` ` `/* function that returns (n+1)th Fibonacci number ` ` ` `Or number of ways to represent n as sum of 1's ` ` ` `2's */` ` ` `static` `int` `countWays(` `int` `n) ` ` ` `{ ` ` ` `int` `[,]F = {{1, 1}, {1, 0}}; ` ` ` `if` `(n == 0) ` ` ` `{ ` ` ` `return` `0; ` ` ` `} ` ` ` `power(F, n); ` ` ` `return` `F[0,0]; ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 5; ` ` ` `System.Console.WriteLine(countWays(n)); ` ` ` `} ` `} ` ` ` `// This code contributed by mits ` |

*chevron_right*

*filter_none*

**Output:**

8

**Time Complexity: **O(logn).

This article is contributed by **Anuj Chauhan**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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:

- Number of ways to represent a number as sum of k fibonacci numbers
- Different ways to represent N as sum of K non-zero integers
- Minimum number of given powers of 2 required to represent a number
- Count of integers up to N which represent a Binary number
- Number of ways to split a binary number such that every part is divisible by 2
- Number of ways to pair people
- Number of ways to get even sum by choosing three numbers from 1 to N
- Number of ways to go from one point to another in a grid
- Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes
- Number of ways to reach the end of matrix with non-zero AND value
- Number of ways to convert a character X to a string Y
- Count ways to express even number ‘n’ as sum of even integers
- Number of ways to write N as a sum of K non-negative integers
- Number of ways to cut a stick of length N into K pieces
- Number of ways to color N-K blocks using given operation
- Total number of ways to place X and Y at n places such that no two X are together
- Number of ways to place two queens on a N*N chess-board
- Number of ways to reach Nth floor by taking at-most K leaps
- Number of ways to merge two arrays such retaining order
- Number of ways to arrange a word such that no vowels occur together