- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Smallest triangular number larger than p
We will discuss triangular numbers and how we can find the smallest triangular number just larger than a given number “num”. We will first discuss what exactly is a triangular number and then will find out the smallest triangular number just larger than “num”
We will see two different approaches for the same. In the first approach, we will run a simple loop to generate the output, while in our second approach, we will first generate a general formula for calculating the desired number and then will directly apply that formula to get the smallest triangular number.
Problem Statement
We have to find out the smallest triangular number just larger than “num”.
We are given multiple boxes which contain balls. The number of balls the box contains is a distinct triangular number for all the boxes. The boxes are numbered from 1 to n. We have to find out which box will contain the minimum number of balls after removing the “num” number of balls from the box.
Let us understand this using an example
Input number was: num = 5
We had to find out which box will contain the minimum number of balls after removing 5 balls
Output:3rd box will contain a minimum of balls after removing 4 balls.
Solution for this example −
Boxes with number of balls: {1 3 6 10 ....} Box 3 will contain only 1 ball after removing 4 balls from it.
What is a triangular number?
A triangular number is a number which can be represented in the form of an equilateral triangular grid. The number of points in a row is always equal to the row number i.e., first row will contain 1 point, the 2nd row will contain 2 points and so on. Few triangular numbers are: 1, 3, 6, 10, 15…. Let us now deduct a formula for nth triangular number −
We know, the nth row in a triangular number contains n points, hence a triangular number can be represented as the sum of points in each row. We also know, in nth triangular number, there are n rows, so nth triangular number can be given by the sum of first n natural numbers.
Approach 1: (Direct Approach)
In this approach, we will run a single loop and calculate the difference between the given number and nth triangular number, the moment we get the difference>=0, we will get the desired box number hence we will truncate the loop.
For the triangular number, we will keep on adding n to the existing (n-1)th triangular number to calculate the value of the next triangular number.
Algorithm
STEP 1 − Initialize the variable triangular_number with 0.
STEP 2 − Run a for loop and keep adding n for each iteration.
STEP 3 − Keep calculating the difference between a triangular number and the given number “num”.
STEP 4 − The moment we get difference >=0, We will print n as the desired box number.
Example
Implementation of this approach in C++ is given as −
#include <iostream> using namespace std; int main(){ int num = 1234; int triangular_number = 0; for (int n=1; triangular_number<=num; n++){ triangular_number = triangular_number + n; if((triangular_number-num)>=0){ cout<<"The smallest triangular number larger than "<<num<<" is "<<n; return 0; } } }
Output
The smallest triangular number larger than 1234 is 50
Approach 2: Formula Based Approach
In this approach, we will first generate a general formula for calculating the desired number and then will directly apply that formula to get the smallest triangular number just larger than the given number.
Triangular number for nth box number is given by −
Triangular number = (n*(n+1))/2
To get the smallest box number “n” such that triangular number >= num.
i.e. (n*(n+1))/2 >= num
This means we have to solve for −
n2 + n – 2*num >= 0
Using this equation, we get
n = ceil( (sqrt(8*num+1)-1)/2 )
Example
Code for this approach is given by −
#include<bits/stdc++.h> using namespace std; int boxnum(int num){ return ceil( ( sqrt( 8*num + 1 ) -1 ) / 2 ) ; } int main(){ int num = 1234 ; cout << "The smallest triangular number larger than "<<num<<" is "<<boxnum(num); return 0; }
Output
The smallest triangular number larger than 1234 is 50
Time Complexity for this approach − O(logn)
Space Complexity − O(1) as we are only using constant extra space.
In this article, we have discussed two different methods to find the smallest triangular number just larger than a given number “num”. The first method simply calculated the triangular number by running a loop and adding n for each iteration. We simultaneously calculated the difference between the given number and triangular number. In the second approach, we generated a mathematical formula to calculate our desired output.