Given a number N, the task is to find the index of smallest triangular number with N digits.
A number is termed as a triangular number if we can represent it in the form of a triangular grid of points such that the points form an equilateral triangle and each row contains as many points as the row number, i.e., the first row has one point, the second row has two points, the third row has three points and so on. The starting triangular numbers are 1, 3, 6, 10, 15, 21, 28…………
Input: N = 2
Smallest triangular number with 2 digits = 10, and 4 is the index of 10.
Input: N = 3
Smallest triangular number with
3 digits = 105, and 14 is the index of 105.
Approach: The key observation in the problem is that the index of smallest triangular numbers with N digits form a series which is –
1, 4, 14, 45, 141...
The term of the index of smallest triangular number with N digits will be
Below is the implementation of the above approach:
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.