Programming is a way to instruct the computer to perform various tasks. You can solve a variety of problems using programming, including a massive range of math problems. In this article, you’ll learn how to find the total number of perfect squares between two numbers using C++, Python, and JavaScript.
Problem Statement
You’re given two numbers num1 and num2. You need to find the total number of perfect squares between num1 and num2.
Example 1: Let num1 = 10 and num2 = 100.
Perfect squares between 10 and 100: 16, 25, 36, 49, 64, 81, 100.
Thus, the output is 7.
Example 2: Let num1 = 15 and num2 = 82.
Perfect squares between 15 and 82: 16, 25, 36, 49, 64, 81.
Thus, the output is 6.
Example 3: Let num1 = 3 and num2 = 36.
Perfect squares between 3 and 36: 4, 9, 16, 25, 36.
Thus, the output is 5.
Basic Approach to Find the Total Number of Perfect Squares Between Two Numbers
C++ Program to Count the Total Number of Perfect Squares Between Two Numbers
Below is the C++ implementation to solve the problem:
// C++ program to count the total number of
// perfect squares between 2 numbers
#include <iostream>
using namespace std;
int countTotalSquares(int num1, int num2)
{
int result = 0;
for(int i=num1; i<=num2; i++)
{
for(int j=1; j*j<=i; j++)
{
if(j*j == i)
{
result++;
}
}
}
return result;
}
int main()
{
int num1 = 10;
int num2 = 100;
cout << "Total number of perfect squares between " << num1 << " and " << num2 << ": " << countTotalSquares(num1, num2) << endl;
int num3 = 15;
int num4 = 82;
cout << "Total number of perfect squares between " << num3 << " and " << num4 << ": " << countTotalSquares(num3, num4) << endl;
int num5 = 3;
int num6 = 36;
cout << "Total number of perfect squares between " << num5 << " and " << num6 << ": " << countTotalSquares(num5, num6) << endl;
int num7 = 12;
int num8 = 65;
cout << "Total number of perfect squares between " << num7 << " and " << num8 << ": " << countTotalSquares(num7, num8) << endl;
return 0;
}
Output:
Total number of perfect squares between 10 and 100: 7
Total number of perfect squares between 15 and 82: 6
Total number of perfect squares between 3 and 36: 5
Total number of perfect squares between 12 and 65: 5
Python Program to Count the Total Number of Perfect Squares Between Two Numbers
Next, the Python code to find the total number of perfect squares between two numbers:
# Python program to count the total number of
# perfect squares between 2 numbers
def countTotalSquares(num1, num2):
result = 0
for i in range(num1, num2+1):
j = 1
while j * j <= i:
if j * j == i:
result = result + 1
j = j + 1
return result
num1 = 10
num2 = 100
print("Total number of perfect squares between", num1, "and", num2, ":", countTotalSquares(num1, num2))
num3 = 15
num4 = 82
print("Total number of perfect squares between", num3, "and", num4, ":", countTotalSquares(num3, num4))
num5 = 3
num6 = 36
print("Total number of perfect squares between", num5, "and", num6, ":", countTotalSquares(num5, num6))
num7 = 12
num8 = 65
print("Total number of perfect squares between", num7, "and", num8, ":", countTotalSquares(num7, num8))
Output:
Total number of perfect squares between 10 and 100: 7
Total number of perfect squares between 15 and 82: 6
Total number of perfect squares between 3 and 36: 5
Total number of perfect squares between 12 and 65: 5
JavaScript Program to Count the Total Number of Perfect Squares Between Two Numbers
Here’s how you find the total number of perfect squares between two numbers in JavaScript:
// JavaScript program to count the total number of
// perfect squares between 2 numbers
function countTotalSquares(num1, num2) {
var result = 0;
for(let i=num1; i<=num2; i++) {
for(let j=1; j*j<=i; j++) {
if(j*j == i) {
result++;
}
}
}
return result;
}
var num1 = 10;
var num2 = 100;
document.write("Total number of perfect squares between " + num1 + " and " + num2 + ": " + countTotalSquares(num1, num2) + "<br>");
var num3 = 15;
var num4 = 82;
document.write("Total number of perfect squares between " + num3 + " and " + num4 + ": " + countTotalSquares(num3, num4) + "<br>");
var num5 = 3;
var num6 = 36;
document.write("Total number of perfect squares between " + num5 + " and " + num6 + ": " + countTotalSquares(num5, num6) + "<br>");
var num7 = 12;
var num8 = 65;
document.write("Total number of perfect squares between " + num7 + " and " + num8 + ": " + countTotalSquares(num7, num8) + "<br>");
Output:
Total number of perfect squares between 10 and 100: 7
Total number of perfect squares between 15 and 82: 6
Total number of perfect squares between 3 and 36: 5
Total number of perfect squares between 12 and 65: 5
Efficient Approach to Find the Number of Perfect Squares Between Two Numbers
You can also find the total number of perfect squares between two numbers using the following formula:
Total no. of perfect squares between num1 and num2 = floor(sqrt(num2)) - ceil(sqrt(num1)) + 1
The time complexity of this solution ( O(log num2) ) is better than the previous approach ( O((num2-num1) * sqrt(num2)) ).
C++ Implementation Using Efficient Formula
Below is the C++ implementation to find the total number of perfect squares between two numbers:
// C++ program to count the total number of
// perfect squares between 2 numbers
#include <bits/stdc++.h>
using namespace std;
int countTotalSquares(int num1, int num2)
{
return (floor(sqrt(num2)) - ceil(sqrt(num1)) + 1);
}
int main()
{
int num1 = 10;
int num2 = 100;
cout << "Total number of perfect squares between " << num1 << " and " << num2 << ": " << countTotalSquares(num1, num2) << endl;
int num3 = 15;
int num4 = 82;
cout << "Total number of perfect squares between " << num3 << " and " << num4 << ": " << countTotalSquares(num3, num4) << endl;
int num5 = 3;
int num6 = 36;
cout << "Total number of perfect squares between " << num5 << " and " << num6 << ": " << countTotalSquares(num5, num6) << endl;
int num7 = 12;
int num8 = 65;
cout << "Total number of perfect squares between " << num7 << " and " << num8 << ": " << countTotalSquares(num7, num8) << endl;
return 0;
}
Output:
Total number of perfect squares between 10 and 100: 7
Total number of perfect squares between 15 and 82: 6
Total number of perfect squares between 3 and 36: 5
Total number of perfect squares between 12 and 65: 5
Python Implementation Using Efficient Formula
Below is the Python implementation to find the total number of perfect squares between two numbers:
# Python program to count the total number of
# perfect squares between 2 numbers
def countTotalSquares(num1, num2):
return (math.floor(math.sqrt(num2)) - math.ceil(math.sqrt(num1)) + 1)
num1 = 10
num2 = 100
print("Total number of perfect squares between", num1, "and", num2, ":", countTotalSquares(num1, num2))
num3 = 15
num4 = 82
print("Total number of perfect squares between", num3, "and", num4, ":", countTotalSquares(num3, num4))
num5 = 3
num6 = 36
print("Total number of perfect squares between", num5, "and", num6, ":", countTotalSquares(num5, num6))
num7 = 12
num8 = 65
print("Total number of perfect squares between", num7, "and", num8, ":", countTotalSquares(num7, num8))
Output:
Total number of perfect squares between 10 and 100: 7
Total number of perfect squares between 15 and 82: 6
Total number of perfect squares between 3 and 36: 5
Total number of perfect squares between 12 and 65: 5
JavaScript Implementation Using Efficient Formula
Below is the JavaScript implementation to find the total number of perfect squares between two numbers:
// JavaScript program to count the total number of
// perfect squares between 2 numbers
function countTotalSquares(num1, num2) {
return (Math.floor(Math.sqrt(num2)) - Math.ceil(Math.sqrt(num1)) + 1);
}
var num1 = 10;
var num2 = 100;
document.write("Total number of perfect squares between " + num1 + " and " + num2 + ": " + countTotalSquares(num1, num2) + "<br>");
var num3 = 15;
var num4 = 82;
document.write("Total number of perfect squares between " + num3 + " and " + num4 + ": " + countTotalSquares(num3, num4) + "<br>");
var num5 = 3;
var num6 = 36;
document.write("Total number of perfect squares between " + num5 + " and " + num6 + ": " + countTotalSquares(num5, num6) + "<br>");
var num7 = 12;
var num8 = 65;
document.write("Total number of perfect squares between " + num7 + " and " + num8 + ": " + countTotalSquares(num7, num8) + "<br>");
Output:
Total number of perfect squares between 10 and 100: 7
Total number of perfect squares between 15 and 82: 6
Total number of perfect squares between 3 and 36: 5
Total number of perfect squares between 12 and 65: 5
Monetize Your Programming Skills
Coding is considered to be one of the best career skills of the 21st century. It offers unlimited opportunities for your creative ideas that can earn you some cash. You can make money by freelancing, writing technical blogs, developing apps and APIs, selling eBooks and courses, etc. The only way you’ll discover which you like is to dive in!
Read Next
About The Author
Programming