Related Articles
Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
• Last Updated : 25 Mar, 2021

Given a number N. Find the count of such numbers that can be formed using digits 3 and 4 only and having length at max N.
Examples:

```Input : N = 2
Output : 6
Explanation : 3, 4, 33, 34, 43, 44
are numbers having length 2 and digits 3 and 4 only.

Input : N = 1
Output : 2
Explanation : 3, 4 are the only such numbers.```

Approach : There are 2 numbers of length 1. They are 3 and 4. There are 4 numbers of length 2. They are 33, 34, 43 and 44. There are 8 such numbers of length 3. They are 333, 334, 343, 344, 433, 434, 443, 444. For each addition of 1 to the length, the number of numbers is increased times 2.
It is easy to prove: to any number of the previous length one can append 3 or 4, so one number of the previous length creates two numbers of the next length.
So for the length N the amount of such numbers of the length exactly N is 2*N. But in the problem, we need the number of numbers of length not greater than N. Let’s sum up them. 21 = 2, 21 + 22 = 2 + 4 = 6, 21 + 22 + 23 = 2 + 4 + 8 = 14, 21 + 22 + 23 + 24 = 2 + 4 + 8 + 16 = 30.
One can notice that the sum of all previous powers of two is equal to the next power of two minus the first power of two. So the answer to the problem is 2N+1 – 2.
Below is the implementation of the above approach :

## C++

 `// Cpp program to find the count of numbers that``// can be formed using digits 3, 4 only and``// having length at max N.``#include ``using` `namespace` `std;` `// Function to find the count of numbers that``// can be formed using digits 3, 4 only and``// having length at max N.``long` `long` `numbers(``int` `n)``{``    ``return` `(``long` `long``)(``pow``(2, n + 1)) - 2;``}` `// Driver code``int` `main()``{``    ``int` `n = 2;` `    ``cout << numbers(n);` `    ``return` `0;``}`

## Java

 `// Java program to find the count of numbers that``// can be formed using digits 3, 4 only and``// having length at max N.` `class` `GFG``{``    ` `// Function to find the count of numbers that``// can be formed using digits 3, 4 only and``// having length at max N.``static` `long` `numbers(``int` `n)``{``    ``return` `(``long``)(Math.pow(``2``, n + ``1``)) - ``2``;``}` `// Driver code``public` `static` `void` `main(String args[])``{``    ``int` `n = ``2``;` `    ``System.out.println( numbers(n));``}``}` `// This code is contributed by Arnab Kundu`

## Python3

 `# Python3 program to find the count of``# numbers that can be formed using digits``# 3, 4 only and having length at max N.` `# Function to find the count of numbers``# that can be formed using digits 3, 4``# only and having length at max N.``def` `numbers(n):``    ``return` `pow``(``2``, n ``+` `1``) ``-` `2` `# Driver code``n ``=` `2``print``(numbers(n))` `# This code is contributed``# by Shrikant13`

## C#

 `// C# program to find the count of numbers that``// can be formed using digits 3, 4 only and``// having length at max N.``using` `System;` `class` `GFG``{``    ` `// Function to find the count of numbers that``// can be formed using digits 3, 4 only and``// having length at max N.``static` `long` `numbers(``int` `n)``{``    ``return` `(``long``)(Math.Pow(2, n + 1)) - 2;``}` `// Driver code``static` `void` `Main()``{``    ``int` `n = 2;` `    ``Console.WriteLine( numbers(n));``}``}` `// This code is contributed by mits`

## PHP

 ``

## Javascript

 ``
Output:
`6`

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up