Find the number of digits in the nth number constructed by using 6, 1, 4 and 9 as the only digits in the ascending order.

First few numbers constructed by using only 6, 1, 4 and 9 as digits in the ascending order would be: 1, 6, 4,

9, 11, 14, 16, 19, 41, 44, 46, 49, 61, 64, 66, 69, 91, 94, 96, 99, 111, 114, 116, 119 and so on.

**Examples:**

Input : 6 Output : 2 6th digit of the series is 14 which has 2 digits. Input : 21 Output : 3 21st digit of the series is 111 which has 3 digits.

**Simple Approach:** This is a brute force approach.

1. Initialize a number to 1 and a counter to 0.

2. Check if the initialized number has only 6, 1, 4 or 9 as it’s digits.

3. If it has only the mentioned digits then increase the counter by 1.

4. Increase the number and repeat the above steps until the counter is less than n.**Note:** The value of n could be large and hence this approach can’t work as it’s not time efficient.

**Efficient Approach:** You can calculate the number of k digit numbers in O (1) time and they will be always be power of 4, for instance number of 1 digit numbers in the series would be 4, number of 2 digit numbers in the series would be 16 and so on.

- Count all subsequent k digit numbers and keep adding them to a sum.
- Break the loop when sum is greater than or equal to n.
- Maintain a counter to keep track of the number of digits.
- The value of the counter at the break of the loop will indicate the answer.

Below is the implementation of the above approach:

## C++

`// C++ program to count number of digits` `// in n-th number made of given four digits.` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Efficient function to calculate number` `// of digits in the nth number constructed` `// by using 6, 1, 4 and 9 as digits in the` `// ascending order.` `int` `number_of_digits(` `int` `n)` `{` ` ` `int` `i, res, sum = 0;` ` ` `// Number of digits increase after` ` ` `// every i-th number where i increases in` ` ` `// powers of 4.` ` ` `for` `(i = 4, res = 1;; i *= 4, res++) {` ` ` `sum += i;` ` ` `if` `(sum >= n)` ` ` `break` `; ` ` ` `}` ` ` `return` `res;` `}` `// Driver code` `int` `main()` `{` ` ` `int` `n = 21;` ` ` `cout << number_of_digits(n) << endl;` ` ` `return` `0;` `}` `//Thic code is contributed by Mayank Tyagi` |

## Java

`// Java program to count` `// number of digits in` `// n-th number made of` `// given four digits.` `import` `java.io.*;` `class` `GFG {` ` ` `// Efficient function to` ` ` `// calculate number of digits` ` ` `// in the nth number constructed` ` ` `// by using 6, 1, 4 and 9 as` ` ` `// digits in the ascending order.` ` ` `static` `int` `number_of_digits(` `int` `n)` ` ` `{` ` ` `int` `i;` ` ` `int` `res;` ` ` `int` `sum = ` `0` `;` ` ` `// Number of digits increase` ` ` `// after every i-th number` ` ` `// where i increases in powers of 4.` ` ` `for` `(i = ` `4` `, res = ` `1` `;; i *= ` `4` `, res++) {` ` ` `sum += i;` ` ` `if` `(sum >= n)` ` ` `break` `;` ` ` `}` ` ` `return` `res;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `n = ` `21` `;` ` ` `System.out.println(number_of_digits(n));` ` ` `}` `}` `// This code is contributed` `// by akt_mit` |

## Python3

`# Python3 program to count number of` `# digits in n-th number made of given` `# four digits.` `# Efficient function to calculate number` `# of digits in the nth number constructed` `# by using 6, 1, 4 and 9 as digits in the` `# ascending order.` `def` `number_of_digits(n):` ` ` `i ` `=` `4` ` ` `res ` `=` `1` ` ` `sum` `=` `0` ` ` `# Number of digits increase after` ` ` `# every i-th number where i increases` ` ` `# in powers of 4.` ` ` `while` `(` `True` `):` ` ` `i ` `*` `=` `4` ` ` `res ` `+` `=` `1` ` ` `sum` `+` `=` `i` ` ` `if` `(` `sum` `>` `=` `n):` ` ` `break` ` ` `return` `res` `# Driver Code` `n ` `=` `21` `print` `(number_of_digits(n))` `# This code is contributed by mits` |

## C#

`// C# program to count` `// number of digits in` `// n-th number made of` `// given four digits.` `using` `System;` `public` `class` `GFG {` ` ` `// Efficient function to` ` ` `// calculate number of digits` ` ` `// in the nth number constructed` ` ` `// by using 6, 1, 4 and 9 as` ` ` `// digits in the ascending order.` ` ` `static` `int` `number_of_digits(` `int` `n)` ` ` `{` ` ` `int` `i;` ` ` `int` `res;` ` ` `int` `sum = 0;` ` ` `// Number of digits increase` ` ` `// after every i-th number` ` ` `// where i increases in powers of 4.` ` ` `for` `(i = 4, res = 1;; i *= 4, res++) {` ` ` `sum += i;` ` ` `if` `(sum >= n)` ` ` `break` `;` ` ` `}` ` ` `return` `res;` ` ` `}` ` ` `// Driver Code` ` ` `static` `public` `void` `Main()` ` ` `{` ` ` `int` `n = 21;` ` ` `Console.WriteLine(number_of_digits(n));` ` ` `}` `}` |

## PHP

`<?php` `// PHP program to count number` `// of digits in n-th number` `// made of given four digits.` `// Efficient function to` `// calculate number of digits` `// in the nth number constructed` `// by using 6, 1, 4 and 9 as` `// digits in the ascending order.` `function` `number_of_digits(` `$n` `)` `{` ` ` `$i` `; ` `$res` `; ` `$sum` `= 0;` ` ` `// Number of digits increase` ` ` `// after every i-th number` ` ` `// where i increases in` ` ` `// powers of 4.` ` ` `for` `(` `$i` `= 4, ` `$res` `= 1;;` ` ` `$i` `*= 4, ` `$res` `++)` ` ` `{` ` ` `$sum` `+= ` `$i` `;` ` ` `if` `(` `$sum` `>= ` `$n` `)` ` ` `break` `;` ` ` `}` ` ` `return` `$res` `;` `}` `// Driver Code` `$n` `= 21;` `echo` `number_of_digits(` `$n` `),` `"\n"` `;` ` ` `// This code is contributed by ajit` `?>` |

## Javascript

`<script>` `// Javascript program to count number of digits` `// in n-th number made of given four digits.` `// Efficient function to calculate number` `// of digits in the nth number constructed` `// by using 6, 1, 4 and 9 as digits in the` `// ascending order.` `function` `number_of_digits(n)` `{` ` ` `let i, res, sum = 0;` ` ` `// Number of digits increase after` ` ` `// every i-th number where i increases in` ` ` `// powers of 4.` ` ` `for` `(i = 4, res = 1;; i *= 4, res++)` ` ` `{` ` ` `sum += i;` ` ` ` ` `if` `(sum >= n)` ` ` `break` `; ` ` ` `}` ` ` `return` `res;` `}` `// Driver code` `let n = 21;` `document.write(number_of_digits(n) + ` `"<br>"` `);` `// This code is contributed by Manoj.` `</script>` |

**Output**

3

**Note:** Since n could be really large we have used boost library, to know more about boost library give this article a read: Advanced C++ with boost library

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****.**