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 N^{th}
term of the index of smallest triangular number with N digits will be \lfloor \sqrt{2*10^{n-1} \rceil}
Below is the implementation of the above approach:
C++
// C++ implementation of
// the above approach
#include <bits/stdc++.h>
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
<script>
// Javascript implementation of the above approach
// Function to return index of smallest
// triangular no n digits
function findIndex( n) {
let x = Math.sqrt(2 * Math.pow(10, (n - 1)));
return Math.round(x);
}
// Driver code
let n = 3;
document.write(findIndex(n));
// This code is contributed by todaysgaurav
</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) {
// Calculate the minimum triangular number with N digits
int minTriangular = static_cast<int>(ceil(sqrt(8 * pow(10, n - 1) + 1) - 1) / 2);
// Return the index of the minimum triangular number
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) {
// Calculate the minimum triangular number with N digits
int minTriangular = (int) Math.ceil(Math.sqrt(8 * Math.pow(10, n - 1) + 1) - 1) / 2;
// Return the index of the minimum triangular number
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
# program to find the index of the smallest triangular number with N digits
import math
def smallest_triangular_index(n):
# calculate the minimum triangular number with N digits
min_triangular = int(math.ceil(math.sqrt(8 * math.pow(10, n-1) + 1) - 1) / 2)
# return the index of the minimum triangular number
return min_triangular
# example usage
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)
{
// Calculate the minimum triangular number with N digits
int minTriangular = (int)Math.Ceiling(Math.Sqrt(8 * Math.Pow(10, n - 1) + 1) - 1) / 2;
// Return the index of the minimum triangular number
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) {
// Calculate the minimum triangular number with N digits
const minTriangular = Math.ceil(Math.sqrt(8 * Math.pow(10, n - 1) + 1) - 1) / 2;
// Return the index of the minimum triangular number
return minTriangular;
}
// Example usage
const n = 3;
const index = smallestTriangularIndex(n);
console.log(`Index of the smallest triangular number with ${n} digits is ${index}`);
// This code is contributed by Dwaipayan Bandyopadhyay
OutputIndex of the smallest triangular number with 3 digits is 14
Time complexity: O(1).
Space complexity: O(1).
Similar Reads
Smallest odd number with N digits
Given a number N. The task is to find the smallest N digit ODD number.Examples: Input: N = 1 Output: 1 Input: N = 3 Output: 101 Approach: There can be two cases depending on the value of N. Case 1 : If N = 1 then answer will be 1. Case 2 : If N != 1 then answer will be (10^(n-1)) + 1 because the ser
3 min read
Smallest Even number with N digits
Given a number N, the task is to find the smallest Even number with N digits.Examples: Input: N = 1 Output: 0 Input: N = 2 Output: 10 Approach: Case 1 : If N = 1 then answer will be 0. Case 2 : if N != 1 then answer will be (10^(N-1)) because the series of smallest even numbers will go on like, 0, 1
2 min read
Smallest number whose sum of digits is square of N
Given an integer N, the task is to find the smallest number whose sum of digits is N2. Examples: Input: N = 4 Output: 79 24 = 16 sum of digits of 79 = 76 Input: N = 6 Output: 9999 210 = 1024 which has 4 digits Approach: The idea is to find the general term for the smallest number whose sum of digits
3 min read
Find the smallest number whose sum of digits is N
Given a positive integers N, the task is to find the smallest number whose sum of digits is N.Example: Input: N = 10Output: 19Explanation: 1 + 9 = 10 = N Input: N = 18Output: 99Explanation: 9 + 9 = 18 = N Naive Approach: A Naive approach is to run a loop of i starting from 0 and find Sum of digits o
6 min read
Find the kth smallest number with sum of digits as m
Given two integers M and K, the task is to find the Kth smallest number with digit sum as M.Examples: Input: M = 5, K = 3 Output: 23 Sequence of numbers starting from 1 with digit sum as 5 is as follows: 5 14 23 32 41 So 3rd smallest number is 23Input: M = 4, K = 1 Output: 4 Approach: We need to fin
6 min read
Find smallest number with given digits and sum of digits
Given two positive integers P and Q, find the minimum integer containing only digits P and Q such that the sum of the digits of the integer is N. Example: Input: N = 11, P = 4, Q = 7 Output: 47Explanation: There are two possible integers that can be formed from 4 and 7 such that their sum is 11 i.e.
9 min read
Smallest odd digits number not less than N
Given a number N, the task is to find the smallest number not less than N, which has all digits odd. Examples: Input: N = 1345 Output: 13511351 is the smallest number not less than N, whose all digits are odd. Input: N = 2397 Output: 3111 3111 is the smallest number not less than N, whose all digits
15+ min read
Smallest and Largest N-digit number starting and ending with N
Given an integer N, the task is to find the smallest and the largest N-digit numbers which start and ends with digit N.Examples: Input: N = 3 Output: Smallest Number = 303 Largest Number = 393 Explanation: 303 is the smallest 3 digit number starting and ending with 3. 393 is the largest 3 digit numb
9 min read
Number with even sum of digits
Fixed compiling error in java programA positive integer is considered a good number if sum of its digits is even. Find n-th smallest good number. Examples : Input : n = 1 Output : 2 First good number is smallest positive number with sum of digits even which is 2. Input : n = 10 Output : 20 A simple
5 min read
Smallest number greater than Y with sum of digits equal to X
Given two integers X and Y, find the minimal number with the sum of digits X, which is strictly greater than Y. Examples: Input: X = 18, Y = 99 Output: 189 Explanation: 189 is the smallest number greater than 99 having sum of digits = 18. Input: X = 12, Y = 72 Output: 75 Explanation: 75 is the small
11 min read