Index of smallest triangular number with N digits
Last Updated :
21 Dec, 2023
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…………
Examples:
Input: N = 2
Output: 4
Smallest triangular number with 2 digits = 10, and 4 is the index of 10.
Input: N = 3
Output: 14
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:
C++
#include <bits/stdc++.h>
using namespace std;
int findIndex( int n)
{
float x = sqrt (2 * pow (10, (n - 1)));
return round(x);
}
int main()
{
int n = 3;
cout << findIndex(n);
return 0;
}
|
Java
class GFG{
static double findIndex( int n)
{
double x = Math.sqrt( 2 * Math.pow( 10 , (n - 1 )));
return Math.round(x);
}
public static void main(String[] args)
{
int n = 3 ;
System.out.print(findIndex(n));
}
}
|
Python3
import math
def findIndex(n):
x = math.sqrt( 2 * math. pow ( 10 , (n - 1 )));
return round (x);
n = 3 ;
print (findIndex(n));
|
C#
using System;
class GFG{
static double findIndex( int n)
{
double x = Math.Sqrt(2 * Math.Pow(10, (n - 1)));
return Math.Round(x);
}
public static void Main(String[] args)
{
int n = 3;
Console.Write(findIndex(n));
}
}
|
Javascript
<script>
function findIndex( n) {
let x = Math.sqrt(2 * Math.pow(10, (n - 1)));
return Math.round(x);
}
let n = 3;
document.write(findIndex(n));
</script>
|
Time complexity: O(logn)
Auxiliary space: O(1)
Python program to find the index of the smallest triangular number with N digits without taking input:
Approach steps:
1.Import the math module.
2.Define a function smallest_triangular_index that takes an integer n as input. The function will return the index of the smallest triangular number with n digits.
3.Calculate the minimum triangular number with n digits by using the formula min_triangular = ceil(sqrt(8 * 10^(n-1) + 1) – 1) / 2, where ceil is the ceiling function, sqrt is the square root function, and ^ is the exponentiation operator.
4.This formula is derived from the fact that the kth triangular number is equal to (k * (k + 1)) / 2, so the minimum triangular number with n digits will be greater than or equal to (10^(n-1) – 1) / 2. We can solve for k using the quadratic formula and take the ceiling of the positive root to find the minimum value of k that satisfies this inequality.
5.Return the value of min_triangular as the index of the smallest triangular number with n digits.
6.In the example usage, create an integer n and call the smallest_triangular_index function with this argument. Finally, print the index of the smallest triangular number with n digits.
C++
#include <iostream>
#include <cmath>
int smallestTriangularIndex( int n) {
int minTriangular = static_cast < int >( ceil ( sqrt (8 * pow (10, n - 1) + 1) - 1) / 2);
return minTriangular;
}
int main() {
int n = 3;
int index = smallestTriangularIndex(n);
std::cout << "Index of the smallest triangular number with " << n << " digits is " << index << std::endl;
return 0;
}
|
Java
public class SmallestTriangularIndex {
public static int smallestTriangularIndex( int n) {
int minTriangular = ( int ) Math.ceil(Math.sqrt( 8 * Math.pow( 10 , n - 1 ) + 1 ) - 1 ) / 2 ;
return minTriangular;
}
public static void main(String[] args) {
int n = 3 ;
int index = smallestTriangularIndex(n);
System.out.println( "Index of the smallest triangular number with " +
n + " digits is " + index);
}
}
|
Python3
import math
def smallest_triangular_index(n):
min_triangular = int (math.ceil(math.sqrt( 8 * math. pow ( 10 , n - 1 ) + 1 ) - 1 ) / 2 )
return min_triangular
n = 3
index = smallest_triangular_index(n)
print ( "Index of the smallest triangular number with" , n, "digits is" , index)
|
C#
using System;
class Program
{
static int SmallestTriangularIndex( int n)
{
int minTriangular = ( int )Math.Ceiling(Math.Sqrt(8 * Math.Pow(10, n - 1) + 1) - 1) / 2;
return minTriangular;
}
static void Main( string [] args)
{
int n = 3;
int index = SmallestTriangularIndex(n);
Console.WriteLine( "Index of the smallest triangular number with " + n +
" digits is " + index);
}
}
|
Javascript
function smallestTriangularIndex(n) {
const minTriangular = Math.ceil(Math.sqrt(8 * Math.pow(10, n - 1) + 1) - 1) / 2;
return minTriangular;
}
const n = 3;
const index = smallestTriangularIndex(n);
console.log(`Index of the smallest triangular number with ${n} digits is ${index}`);
|
OutputIndex of the smallest triangular number with 3 digits is 14
Time complexity: O(1).
Space complexity: O(1).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...