
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Squared Triangular Number Sum of Cubes
A square triangular number, also referred to as a triangular square number, is a number that is both a triangular number and a perfect square.
Square triangular numbers have an unlimited number of possible values; the first few are ?
0, 1, 36, 1225, 41616...
A triangular number or triangle number counts objects arranged in an equilateral triangle. The nth triangular number is the number of dots in the triangular arrangement with n dots on each side and is equal to the sum of the n natural numbers from 1 to n. The sequence of triangular numbers, starting with the 0th triangular number, is
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55...
A perfect square or square number or t is the product of some integer with itself. The sequence of a perfect square is
0, 1, 4, 9, 16, 25, 36, 49...
Problem Statement
You are given a number Sum. Print n if Sum equals the sum of the cubes of the first n natural integers; otherwise, print -1.
Example 1
Input
36
Output
3
Explanation ? The Sum is sum of cubes of the first 3 natural numbers.
1*1*1 + 2*2*2 + 3*3*3 = 36
Example 2
Input
22
Output
-1
Explanation
1*1*1 + 2*2*2 = 9 and 1*1*1 + 2*2*2 + 3*3*3 = 36, thus, there are no natural numbers whose sum of cubes equals the Sum.
We have two approaches to this problem.
Approach 1
The brute force method would be calculating the cubes of integers and storing their sum. We will do this while the sum is equal to or less than the given Sum. Then if the sum is equal to the given number print n, otherwise print -1.
Pseudocode
Start temp = 0 i = 0 While temp <= Sum: i = i + 1 add cube of i to temp if temp is equal to Sum then print n else print -1 End
Example
Below is a C++ Program to check if a given number equals the sum of the cubes of the first n natural integers.
#include <iostream> using namespace std; // This function returns n if the sum of cubes of first // n natural numbers is equal to the given Sum int calcSquaredTriangularNumber(int Sum){ // initialize a temporary variable to store sum int temp = 0; // Adding cubes of the numbers starting from 1 int n = 0; while ( temp < Sum){ n++; temp += n * n * n; } // If temp becomes equal to sum return n if (temp == Sum){ return n; } // otherwise return -1 return -1; } int main(){ int Sum = 36; // Function call int n = calcSquaredTriangularNumber(Sum); if(n>0){ cout << n ; return n; } else{ cout << "-1"; } return 0; }
Output
For input 36, the above C++ program will produce the following output ?
3
Approach 2
Now we will look at a more efficient solution.
First, we will check if the given number is a perfect square or not.
If not, we will return -1.
If it comes out to be a perfect square, then we will further check if the square root is a triangular number.
If true, then return the root. If not true, then return -1.
Pseudocode
Start check if the Sum is a perfect square find the squareRoot of the Sum checking if the squareRoot is a triangular number if true then return root else return false End
Example
Below is a C++ Program to check if a given number equals the sum of the cubes of the first n natural integers.
#include <bits/stdc++.h> using namespace std; // Calculates the root of n(n+1)/2 = numif the root is an integer, returns the root else returns -1 int checkTriangularNum(int num){ if (num < 0){ return false; } // Converting the equation n*(n+1)/2 = num in the form of a(n^2) + bn + c = 0"; int a = 1, b = 1; int c = num * (-2); int dis = (b * b) - (4 * a * c); if (dis < 0) return -1; // calculating the roots float root1 = ( -b + sqrt(dis)) / (2 * a); float root2 = ( -b - sqrt(dis)) / (2 * a); // checking if root1 is an integer if (root1 > 0 && floor(root1) == root1){ return root1; } // checking if root2 is an integer if (root2 > 0 && floor(root2) == root2){ return root2; } return -1; } // Calculates the square root of the numberreturns the root if it an integer, else returns -1 int checkPerfectSquare(double x){ // floating point value of square root double squareRoot = sqrt(x); // checking if the root is integer if ((squareRoot - floor(squareRoot)) == 0) return floor(squareRoot); else return -1; } // This function returns n if the sum of cubes of first // n natural numbers is equal to the given Sum int calcSquaredTriangularNumber(int Sum){ // checking if Sum is a perfect square int squareRoot = checkPerfectSquare(Sum); // if it's not a perfect square return -1 if (squareRoot == -1) return -1; // if is a perfect square, then check if it's a // triangular number or not return checkTriangularNum(squareRoot); } int main(){ int Sum = 36; // Function Call int n = calcSquaredTriangularNumber(Sum); if(n>0){ cout << n ; return n; } else { cout << "-1"; } return 0; }
Output
For input 36, the above C++ program will produce the following output ?
3