📜  具有N位数字的最小三角数的索引

📅  最后修改于: 2021-04-29 11:04:22             🧑  作者: Mango

给定数字N ,任务是找到具有N个数字的最小三角数的索引。

例子:

方法:对该问题的主要观察是,具有N位数字的最小三角数的索引形成一个序列,该序列为–

1, 4, 14, 45, 141...

N^{th}   带有N位数字的最小三角数的索引项将是\lfloor \sqrt{2*10^{n-1} \rceil}
下面是上述方法的实现:

C++
// C++ implementation of
// the above approach
 
#include 
using namespace std;
 
// Function to return index of smallest
// triangular no n digits
int findIndex(int n)
{
    float x = sqrt(2 * pow(10, (n - 1)));
    return round(x);
}
 
// Driver Code
int main()
{
    int n = 3;
    cout << findIndex(n);
 
    return 0;
}


Java
// Java implementation of the above approach
class GFG{
 
// Function to return index of smallest
// triangular no n digits
static double findIndex(int n)
{
    double x = Math.sqrt(2 * Math.pow(10, (n - 1)));
    return Math.round(x);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 3;
    System.out.print(findIndex(n));
}
}
 
// This code is contributed by shubham


Python3
# Python3 implementation of
# the above approach
import math
 
# Function to return index of smallest
# triangular no n digits
def findIndex(n):
 
    x = math.sqrt(2 * math.pow(10, (n - 1)));
    return round(x);
 
# Driver Code
n = 3;
print(findIndex(n));
 
# This code is contributed by Code_Mech


C#
// C# implementation of the above approach
using System;
class GFG{
 
// Function to return index of smallest
// triangular no n digits
static double findIndex(int n)
{
    double x = Math.Sqrt(2 * Math.Pow(10, (n - 1)));
    return Math.Round(x);
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 3;
    Console.Write(findIndex(n));
}
}
 
// This code is contributed by AbhiThakur


Javascript


输出:
14