Given an integer N, the task is to find the count of natural octal numbers up to N digits.
Input: N = 1
1, 2, 3, 4, 5, 6, 7 are 1 digit Natural Octal numbers.
Input: N = 2
There are a total of 56 two digit octal numbers and 7 one digit octal numbers. Therefore, 56 + 7 = 63.
Approach: On observing carefully, a geometric progression series is formed [ 7 56 448 3584 28672 229376… ] with the first term being 7 and a common ratio of 8.
Nth term = Number of Octal numbers of N digits = 7 * 8N - 1
Finally, count of all octal numbers up to N digits can be found out by iterating a loop from 1 to N and calculating the sum of ith term using the above formula.
Below is the implementation of the above approach:
Time Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.