Find the nearest next perfect square of a number
The problem is to find the nearest next perfect square of a given number. A perfect square is an integer that is the square of an integer. For example, 4, 9, 16, 25, etc., are perfect squares. Given a number, we need to find the smallest perfect square that is greater than the given number.
Explanation using Example
Let's consider the test cases from the provided code:
- Test Case 1: Given number = 10 The square root of 10 is approximately 3.1623. The next nearest integer is 4, which represents the next perfect square. Therefore, the output for this test case is "Next Perfect Square (4²) : 16".
-
Test Case 2: Given number = 121 The square root of 121 is exactly 11. The next nearest integer is 12, which represents the next perfect square. Therefore, the output for this test case is "Next Perfect Square (12²) : 144".
-
Test Case 3: Given number = 4 The square root of 4 is exactly 2. The next nearest integer is 3, which represents the next perfect square. Therefore, the output for this test case is "Next Perfect Square (3²) : 9".
Pseudocode
nextPerfectSquare(num)
nextSqrt = floor(sqrt(num))
nextSqrt = nextSqrt + 1
print "Given number : num"
print "Next Perfect Square (nextSqrt²) : nextSqrt * nextSqrt"
Algorithm Explanation
The nextPerfectSquare
function finds the nearest next perfect square of the given number using the
following steps:
- Calculate the square root of the given number using the
Math.sqrt
function. The result is a floating-point number. - Round down the square root to the nearest integer using
Math.floor
to get the integer part. - Increment the rounded square root by 1 to find the next perfect square.
- Print the given number and the square of the next perfect square.
Code Solution
Here given code implementation process.
// Java Program for
// Find the nearest next perfect square of a number
public class PerfectSquare
{
public void nextPerfectSquare(int num)
{
// Calculate square root of given number
int nextSqrt = (int) Math.floor(Math.sqrt(num));
// Next perfect sqrt
nextSqrt = nextSqrt + 1;
// Display given number
System.out.print("\n Given number : " + num);
// Display next perfect sqrt
System.out.print("\n Next Perfect Square (" + nextSqrt + "²) : "
+ (nextSqrt * nextSqrt));
}
public static void main(String[] args)
{
PerfectSquare task = new PerfectSquare();
// Test Cases
task.nextPerfectSquare(10);
task.nextPerfectSquare(121);
task.nextPerfectSquare(4);
}
}
input
Given number : 10
Next Perfect Square (4²) : 16
Given number : 121
Next Perfect Square (12²) : 144
Given number : 4
Next Perfect Square (3²) : 9
// Include header file
#include <iostream>
#include <math.h>
using namespace std;
// C++ Program for
// Find the nearest next perfect square of a number
class PerfectSquare
{
public: void nextPerfectSquare(int num)
{
// Calculate square root of given number
int nextSqrt = (int) floor(sqrt(num));
// Next perfect sqrt
nextSqrt = nextSqrt + 1;
// Display given number
cout << "\n Given number : " << num;
// Display next perfect sqrt
cout << "\n Next Perfect Square (" << nextSqrt << "²) : " << (nextSqrt *nextSqrt);
}
};
int main()
{
PerfectSquare *task = new PerfectSquare();
// Test Cases
task->nextPerfectSquare(10);
task->nextPerfectSquare(121);
task->nextPerfectSquare(4);
return 0;
}
input
Given number : 10
Next Perfect Square (4²) : 16
Given number : 121
Next Perfect Square (12²) : 144
Given number : 4
Next Perfect Square (3²) : 9
// Include namespace system
using System;
// Csharp Program for
// Find the nearest next perfect square of a number
public class PerfectSquare
{
public void nextPerfectSquare(int num)
{
// Calculate square root of given number
int nextSqrt = (int) Math.Floor(Math.Sqrt(num));
// Next perfect sqrt
nextSqrt = nextSqrt + 1;
// Display given number
Console.Write("\n Given number : " + num);
// Display next perfect sqrt
Console.Write("\n Next Perfect Square (" + nextSqrt + "²) : " + (nextSqrt * nextSqrt));
}
public static void Main(String[] args)
{
PerfectSquare task = new PerfectSquare();
// Test Cases
task.nextPerfectSquare(10);
task.nextPerfectSquare(121);
task.nextPerfectSquare(4);
}
}
input
Given number : 10
Next Perfect Square (4²) : 16
Given number : 121
Next Perfect Square (12²) : 144
Given number : 4
Next Perfect Square (3²) : 9
<?php
// Php Program for
// Find the nearest next perfect square of a number
class PerfectSquare
{
public function nextPerfectSquare($num)
{
// Calculate square root of given number
$nextSqrt = (int) floor(sqrt($num));
// Next perfect sqrt
$nextSqrt = $nextSqrt + 1;
// Display given number
echo("\n Given number : ".$num);
// Display next perfect sqrt
echo("\n Next Perfect Square (".$nextSqrt.
"²) : ".($nextSqrt * $nextSqrt));
}
}
function main()
{
$task = new PerfectSquare();
// Test Cases
$task->nextPerfectSquare(10);
$task->nextPerfectSquare(121);
$task->nextPerfectSquare(4);
}
main();
input
Given number : 10
Next Perfect Square (4²) : 16
Given number : 121
Next Perfect Square (12²) : 144
Given number : 4
Next Perfect Square (3²) : 9
// Node JS Program for
// Find the nearest next perfect square of a number
class PerfectSquare
{
nextPerfectSquare(num)
{
// Calculate square root of given number
var nextSqrt = Math.floor(Math.sqrt(num));
// Next perfect sqrt
nextSqrt = nextSqrt + 1;
// Display given number
process.stdout.write("\n Given number : " + num);
// Display next perfect sqrt
process.stdout.write("\n Next Perfect Square (" + nextSqrt + "²) : "
+ (nextSqrt * nextSqrt));
}
}
function main()
{
var task = new PerfectSquare();
// Test Cases
task.nextPerfectSquare(10);
task.nextPerfectSquare(121);
task.nextPerfectSquare(4);
}
main();
input
Given number : 10
Next Perfect Square (4²) : 16
Given number : 121
Next Perfect Square (12²) : 144
Given number : 4
Next Perfect Square (3²) : 9
import math
# Python 3 Program for
# Find the nearest next perfect square of a number
class PerfectSquare :
def nextPerfectSquare(self, num) :
# Calculate square root of given number
nextSqrt = math.floor(math.sqrt(num))
# Next perfect sqrt
nextSqrt = nextSqrt + 1
# Display given number
print("\n Given number : ", num, end = "")
# Display next perfect sqrt
print("\n Next Perfect Square (", nextSqrt ,"²) : ",
(nextSqrt * nextSqrt), end = "")
def main() :
task = PerfectSquare()
# Test Cases
task.nextPerfectSquare(10)
task.nextPerfectSquare(121)
task.nextPerfectSquare(4)
if __name__ == "__main__": main()
input
Given number : 10
Next Perfect Square ( 4 ²) : 16
Given number : 121
Next Perfect Square ( 12 ²) : 144
Given number : 4
Next Perfect Square ( 3 ²) : 9
# Ruby Program for
# Find the nearest next perfect square of a number
class PerfectSquare
def nextPerfectSquare(num)
# Calculate square root of given number
nextSqrt = (Math.sqrt(num)).floor()
# Next perfect sqrt
nextSqrt = nextSqrt + 1
# Display given number
print("\n Given number : ", num)
# Display next perfect sqrt
print("\n Next Perfect Square (", nextSqrt ,"²) : ", (nextSqrt * nextSqrt))
end
end
def main()
task = PerfectSquare.new()
# Test Cases
task.nextPerfectSquare(10)
task.nextPerfectSquare(121)
task.nextPerfectSquare(4)
end
main()
input
Given number : 10
Next Perfect Square (4²) : 16
Given number : 121
Next Perfect Square (12²) : 144
Given number : 4
Next Perfect Square (3²) : 9
// Scala Program for
// Find the nearest next perfect square of a number
class PerfectSquare()
{
def nextPerfectSquare(num: Int): Unit = {
// Calculate square root of given number
var nextSqrt: Int = Math.floor(scala.math.sqrt(num)).toInt;
// Next perfect sqrt
nextSqrt = nextSqrt + 1;
// Display given number
print("\n Given number : " + num);
// Display next perfect sqrt
print("\n Next Perfect Square (" + nextSqrt + "²) : " +
(nextSqrt * nextSqrt));
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: PerfectSquare = new PerfectSquare();
// Test Cases
task.nextPerfectSquare(10);
task.nextPerfectSquare(121);
task.nextPerfectSquare(4);
}
}
input
Given number : 10
Next Perfect Square (4²) : 16
Given number : 121
Next Perfect Square (12²) : 144
Given number : 4
Next Perfect Square (3²) : 9
import Foundation;
// Swift 4 Program for
// Find the nearest next perfect square of a number
class PerfectSquare
{
func nextPerfectSquare(_ num: Int)
{
// Calculate square root of given number
var nextSqrt = Int(floor(sqrt(Double(num))));
// Next perfect sqrt
nextSqrt = nextSqrt + 1;
// Display given number
print("\n Given number : ", num, terminator: "");
// Display next perfect sqrt
print("\n Next Perfect Square (", nextSqrt ,"²) : ",
(nextSqrt * nextSqrt), terminator: "");
}
}
func main()
{
let task = PerfectSquare();
// Test Cases
task.nextPerfectSquare(10);
task.nextPerfectSquare(121);
task.nextPerfectSquare(4);
}
main();
input
Given number : 10
Next Perfect Square ( 4 ²) : 16
Given number : 121
Next Perfect Square ( 12 ²) : 144
Given number : 4
Next Perfect Square ( 3 ²) : 9
// Kotlin Program for
// Find the nearest next perfect square of a number
class PerfectSquare
{
fun nextPerfectSquare(num: Int): Unit
{
// Calculate square root of given number
var nextSqrt: Int = Math.floor(Math.sqrt(num.toDouble())).toInt();
// Next perfect sqrt
nextSqrt = nextSqrt + 1;
// Display given number
print("\n Given number : " + num);
// Display next perfect sqrt
print("\n Next Perfect Square (" + nextSqrt + "²) : "
+ (nextSqrt * nextSqrt));
}
}
fun main(args: Array < String > ): Unit
{
val task: PerfectSquare = PerfectSquare();
// Test Cases
task.nextPerfectSquare(10);
task.nextPerfectSquare(121);
task.nextPerfectSquare(4);
}
input
Given number : 10
Next Perfect Square (4²) : 16
Given number : 121
Next Perfect Square (12²) : 144
Given number : 4
Next Perfect Square (3²) : 9
Time Complexity
The time complexity of the provided algorithm is constant, O(1)
. This is because the algorithm performs
a fixed number of arithmetic and printing operations, irrespective of the size of the input number.
Resultant Output Explanation
For Test Case 1 with "Given number = 10", the algorithm calculates the next perfect square, which is 16 (4²). The output is "Next Perfect Square (4²) : 16".
For Test Case 2 with "Given number = 121", the algorithm calculates the next perfect square, which is 144 (12²). The output is "Next Perfect Square (12²) : 144".
For Test Case 3 with "Given number = 4", the algorithm calculates the next perfect square, which is 9 (3²). The output is "Next Perfect Square (3²) : 9".
The code successfully finds the nearest next perfect square for the given test cases. It is a simple and efficient algorithm, suitable for small to large input numbers.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment