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 = 2Output :6Explanation :3, 4, 33, 34, 43, 44 are numbers having length 2 and digits 3 and 4 only.Input :N = 1Output :2Explanation :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. 2^{1} = 2, 2^{1} + 2^{2} = 2 + 4 = 6, 2^{1} + 2^{2} + 2^{3} = 2 + 4 + 8 = 14, 2^{1} + 2^{2} + 2^{3} + 2^{4} = 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 2^{N+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 <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP 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. ` `function` `numbers(` `$n` `) ` `{ ` ` ` `return` `(pow(2, ` `$n` `+ 1)) - 2; ` `} ` ` ` `// Driver code ` `$n` `= 2; ` ` ` `echo` `numbers(` `$n` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

6

## Recommended Posts:

- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count numbers formed by given two digit with sum having given digits
- Find position of given term in a series formed with only digits 4 and 7 allowed
- Length of the smallest number which is divisible by K and formed by using 1's only
- Find Nth even length palindromic number formed using digits X and Y
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Find maximum number that can be formed using digits of a given number
- Count number of binary strings of length N having only 0's and 1's
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Sum of all numbers that can be formed with permutations of n digits
- Count of alphabets whose ASCII values can be formed with the digits of N
- Length of longest subarray having only K distinct Prime Numbers
- Maximize count of strings of length 3 that can be formed from N 1s and M 0s
- Min and max length subarray having adjacent element difference atmost K
- Find the largest number that can be formed by changing at most K digits
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Count of substrings formed using a given set of characters only

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.