Find perfect square using binary search
Here given code implementation process.
// C program for
// Find perfect square using binary search
#include <stdio.h>
// Find perfect square of given number using recursion
// and implemented by binary search
int findPerfectSquare(int num, int start, int last)
{
if (start > last)
{
// When result are not exist
return 0;
}
else if (start == last)
{
if (start *start == num)
{
return start;
}
return 0;
}
else
{
// Get the middle at the range from start to last
int mid = (start + last) / 2;
if (mid *mid == num)
{
// When mid is perfect square of given num
return mid;
}
if (mid *mid < num)
{
// Change the current range from mid+1 to last
return findPerfectSquare(num, mid + 1, last);
}
else
{
// Change the current range from start to last-1
return findPerfectSquare(num, start, last - 1);
}
}
}
// Handles the request to find perfect square of given num
void isPerfectSquare(int num)
{
if (num < 0)
{
// Because negative number cannot be a perfect square
return;
}
// Display given number
printf("\n Given number : %d", num);
int result = findPerfectSquare(num, 1, num);
if (result > 0)
{
printf("\n Perfect Square : (%d²)", result);
}
else
{
printf("\n Perfect Square : None");
}
}
int main(int argc, char
const *argv[])
{
// Test Cases
isPerfectSquare(9);
isPerfectSquare(196);
isPerfectSquare(37);
isPerfectSquare(324);
return 0;
}
input
Given number : 9
Perfect Square : (3²)
Given number : 196
Perfect Square : (14²)
Given number : 37
Perfect Square : None
Given number : 324
Perfect Square : (18²)
/*
Java Program
Find perfect square using binary search
*/
public class PerfectSquare
{
// Find perfect square of given number using recursion
// and implemented by binary search
public int findPerfectSquare(int num, int start, int last)
{
if (start > last)
{
// When result are not exist
return 0;
}
else if (start == last)
{
if (start * start == num)
{
return start;
}
return 0;
}
else
{
// Get the middle at the range from start to last
int mid = (start + last) / 2;
if (mid * mid == num)
{
// When mid is perfect square of given num
return mid;
}
if (mid * mid < num)
{
// Change the current range from mid+1 to last
return findPerfectSquare(num, mid + 1, last);
}
else
{
// Change the current range from start to last-1
return findPerfectSquare(num, start, last - 1);
}
}
}
// Handles the request to find perfect square of given num
public void isPerfectSquare(int num)
{
if (num < 0)
{
// Because negative number cannot be a perfect square
return;
}
// Display given number
System.out.print("\n Given number : " + num);
int result = findPerfectSquare(num, 1, num);
if (result > 0)
{
System.out.print("\n Perfect Square : (" + result + "²)");
}
else
{
System.out.print("\n Perfect Square : None");
}
}
public static void main(String[] args)
{
PerfectSquare task = new PerfectSquare();
// Test Cases
task.isPerfectSquare(9);
task.isPerfectSquare(196);
task.isPerfectSquare(37);
task.isPerfectSquare(324);
}
}
input
Given number : 9
Perfect Square : (3²)
Given number : 196
Perfect Square : (14²)
Given number : 37
Perfect Square : None
Given number : 324
Perfect Square : (18²)
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Find perfect square using binary search
*/
class PerfectSquare
{
public:
// Find perfect square of given number using recursion
// and implemented by binary search
int findPerfectSquare(int num, int start, int last)
{
if (start > last)
{
// When result are not exist
return 0;
}
else if (start == last)
{
if (start *start == num)
{
return start;
}
return 0;
}
else
{
// Get the middle at the range from start to last
int mid = (start + last) / 2;
if (mid *mid == num)
{
// When mid is perfect square of given num
return mid;
}
if (mid *mid < num)
{
// Change the current range from mid+1 to last
return this->findPerfectSquare(num, mid + 1, last);
}
else
{
// Change the current range from start to last-1
return this->findPerfectSquare(num, start, last - 1);
}
}
}
// Handles the request to find perfect square of given num
void isPerfectSquare(int num)
{
if (num < 0)
{
// Because negative number cannot be a perfect square
return;
}
// Display given number
cout << "\n Given number : " << num;
int result = this->findPerfectSquare(num, 1, num);
if (result > 0)
{
cout << "\n Perfect Square : (" << result << "²)";
}
else
{
cout << "\n Perfect Square : None";
}
}
};
int main()
{
PerfectSquare *task = new PerfectSquare();
// Test Cases
task->isPerfectSquare(9);
task->isPerfectSquare(196);
task->isPerfectSquare(37);
task->isPerfectSquare(324);
return 0;
}
input
Given number : 9
Perfect Square : (3²)
Given number : 196
Perfect Square : (14²)
Given number : 37
Perfect Square : None
Given number : 324
Perfect Square : (18²)
// Include namespace system
using System;
/*
Csharp Program
Find perfect square using binary search
*/
public class PerfectSquare
{
// Find perfect square of given number using recursion
// and implemented by binary search
public int findPerfectSquare(int num, int start, int last)
{
if (start > last)
{
// When result are not exist
return 0;
}
else if (start == last)
{
if (start * start == num)
{
return start;
}
return 0;
}
else
{
// Get the middle at the range from start to last
int mid = (start + last) / 2;
if (mid * mid == num)
{
// When mid is perfect square of given num
return mid;
}
if (mid * mid < num)
{
// Change the current range from mid+1 to last
return this.findPerfectSquare(num, mid + 1, last);
}
else
{
// Change the current range from start to last-1
return this.findPerfectSquare(num, start, last - 1);
}
}
}
// Handles the request to find perfect square of given num
public void isPerfectSquare(int num)
{
if (num < 0)
{
// Because negative number cannot be a perfect square
return;
}
// Display given number
Console.Write("\n Given number : " + num);
int result = this.findPerfectSquare(num, 1, num);
if (result > 0)
{
Console.Write("\n Perfect Square : (" + result + "²)");
}
else
{
Console.Write("\n Perfect Square : None");
}
}
public static void Main(String[] args)
{
PerfectSquare task = new PerfectSquare();
// Test Cases
task.isPerfectSquare(9);
task.isPerfectSquare(196);
task.isPerfectSquare(37);
task.isPerfectSquare(324);
}
}
input
Given number : 9
Perfect Square : (3²)
Given number : 196
Perfect Square : (14²)
Given number : 37
Perfect Square : None
Given number : 324
Perfect Square : (18²)
<?php
/*
Php Program
Find perfect square using binary search
*/
class PerfectSquare
{
// Find perfect square of given number using recursion
// and implemented by binary search
public function findPerfectSquare($num, $start, $last)
{
if ($start > $last)
{
// When result are not exist
return 0;
}
else if ($start == $last)
{
if ($start * $start == $num)
{
return $start;
}
return 0;
}
else
{
// Get the middle at the range from start to last
$mid = (int)(($start + $last) / 2);
if ($mid * $mid == $num)
{
// When mid is perfect square of given num
return $mid;
}
if ($mid * $mid < $num)
{
// Change the current range from mid+1 to last
return $this->findPerfectSquare($num, $mid + 1, $last);
}
else
{
// Change the current range from start to last-1
return $this->findPerfectSquare($num, $start, $last - 1);
}
}
}
// Handles the request to find perfect square of given num
public function isPerfectSquare($num)
{
if ($num < 0)
{
// Because negative number cannot be a perfect square
return;
}
// Display given number
echo("\n Given number : ".$num);
$result = $this->findPerfectSquare($num, 1, $num);
if ($result > 0)
{
echo("\n Perfect Square : (".$result.
"²)");
}
else
{
echo("\n Perfect Square : None");
}
}
}
function main()
{
$task = new PerfectSquare();
// Test Cases
$task->isPerfectSquare(9);
$task->isPerfectSquare(196);
$task->isPerfectSquare(37);
$task->isPerfectSquare(324);
}
main();
input
Given number : 9
Perfect Square : (3²)
Given number : 196
Perfect Square : (14²)
Given number : 37
Perfect Square : None
Given number : 324
Perfect Square : (18²)
/*
Node JS Program
Find perfect square using binary search
*/
class PerfectSquare
{
// Find perfect square of given number using recursion
// and implemented by binary search
findPerfectSquare(num, start, last)
{
if (start > last)
{
// When result are not exist
return 0;
}
else if (start == last)
{
if (start * start == num)
{
return start;
}
return 0;
}
else
{
// Get the middle at the range from start to last
var mid = parseInt((start + last) / 2);
if (mid * mid == num)
{
// When mid is perfect square of given num
return mid;
}
if (mid * mid < num)
{
// Change the current range from mid+1 to last
return this.findPerfectSquare(num, mid + 1, last);
}
else
{
// Change the current range from start to last-1
return this.findPerfectSquare(num, start, last - 1);
}
}
}
// Handles the request to find perfect square of given num
isPerfectSquare(num)
{
if (num < 0)
{
// Because negative number cannot be a perfect square
return;
}
// Display given number
process.stdout.write("\n Given number : " + num);
var result = this.findPerfectSquare(num, 1, num);
if (result > 0)
{
process.stdout.write("\n Perfect Square : (" + result + "²)");
}
else
{
process.stdout.write("\n Perfect Square : None");
}
}
}
function main()
{
var task = new PerfectSquare();
// Test Cases
task.isPerfectSquare(9);
task.isPerfectSquare(196);
task.isPerfectSquare(37);
task.isPerfectSquare(324);
}
main();
input
Given number : 9
Perfect Square : (3²)
Given number : 196
Perfect Square : (14²)
Given number : 37
Perfect Square : None
Given number : 324
Perfect Square : (18²)
# Python 3 Program
# Find perfect square using binary search
class PerfectSquare :
# Find perfect square of given number using recursion
# and implemented by binary search
def findPerfectSquare(self, num, start, last) :
if (start > last) :
# When result are not exist
return 0
elif (start == last) :
if (start * start == num) :
return start
return 0
else :
# Get the middle at the range from start to last
mid = int((start + last) / 2)
if (mid * mid == num) :
# When mid is perfect square of given num
return mid
if (mid * mid < num) :
# Change the current range from mid+1 to last
return self.findPerfectSquare(num, mid + 1, last)
else :
# Change the current range from start to last-1
return self.findPerfectSquare(num, start, last - 1)
# Handles the request to find perfect square of given num
def isPerfectSquare(self, num) :
if (num < 0) :
# Because negative number cannot be a perfect square
return
# Display given number
print("\n Given number : ", num, end = "")
result = self.findPerfectSquare(num, 1, num)
if (result > 0) :
print("\n Perfect Square : (", result ,"²)", end = "",sep = "")
else :
print("\n Perfect Square : None", end = "")
def main() :
task = PerfectSquare()
# Test Cases
task.isPerfectSquare(9)
task.isPerfectSquare(196)
task.isPerfectSquare(37)
task.isPerfectSquare(324)
if __name__ == "__main__": main()
input
Given number : 9
Perfect Square : (3²)
Given number : 196
Perfect Square : (14²)
Given number : 37
Perfect Square : None
Given number : 324
Perfect Square : (18²)
# Ruby Program
# Find perfect square using binary search
class PerfectSquare
# Find perfect square of given number using recursion
# and implemented by binary search
def findPerfectSquare(num, start, last)
if (start > last)
# When result are not exist
return 0
elsif (start == last)
if (start * start == num)
return start
end
return 0
else
# Get the middle at the range from start to last
mid = (start + last) / 2
if (mid * mid == num)
# When mid is perfect square of given num
return mid
end
if (mid * mid < num)
# Change the current range from mid+1 to last
return self.findPerfectSquare(num, mid + 1, last)
else
# Change the current range from start to last-1
return self.findPerfectSquare(num, start, last - 1)
end
end
end
# Handles the request to find perfect square of given num
def isPerfectSquare(num)
if (num < 0)
# Because negative number cannot be a perfect square
return
end
# Display given number
print("\n Given number : ", num)
result = self.findPerfectSquare(num, 1, num)
if (result > 0)
print("\n Perfect Square : (", result ,"²)")
else
print("\n Perfect Square : None")
end
end
end
def main()
task = PerfectSquare.new()
# Test Cases
task.isPerfectSquare(9)
task.isPerfectSquare(196)
task.isPerfectSquare(37)
task.isPerfectSquare(324)
end
main()
input
Given number : 9
Perfect Square : (3²)
Given number : 196
Perfect Square : (14²)
Given number : 37
Perfect Square : None
Given number : 324
Perfect Square : (18²)
/*
Scala Program
Find perfect square using binary search
*/
class PerfectSquare()
{
// Find perfect square of given number using recursion
// and implemented by binary search
def findPerfectSquare(num: Int, start: Int, last: Int): Int = {
if (start > last)
{
// When result are not exist
return 0;
}
else if (start == last)
{
if (start * start == num)
{
return start;
}
return 0;
}
else
{
// Get the middle at the range from start to last
var mid: Int = (start + last) / 2;
if (mid * mid == num)
{
// When mid is perfect square of given num
return mid;
}
if (mid * mid < num)
{
// Change the current range from mid+1 to last
return findPerfectSquare(num, mid + 1, last);
}
else
{
// Change the current range from start to last-1
return findPerfectSquare(num, start, last - 1);
}
}
}
// Handles the request to find perfect square of given num
def isPerfectSquare(num: Int): Unit = {
if (num < 0)
{
// Because negative number cannot be a perfect square
return;
}
// Display given number
print("\n Given number : " + num);
var result: Int = findPerfectSquare(num, 1, num);
if (result > 0)
{
print("\n Perfect Square : (" + result + "²)");
}
else
{
print("\n Perfect Square : None");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: PerfectSquare = new PerfectSquare();
// Test Cases
task.isPerfectSquare(9);
task.isPerfectSquare(196);
task.isPerfectSquare(37);
task.isPerfectSquare(324);
}
}
input
Given number : 9
Perfect Square : (3²)
Given number : 196
Perfect Square : (14²)
Given number : 37
Perfect Square : None
Given number : 324
Perfect Square : (18²)
/*
Swift 4 Program
Find perfect square using binary search
*/
class PerfectSquare
{
// Find perfect square of given number using recursion
// and implemented by binary search
func findPerfectSquare(_ num: Int, _ start: Int, _ last: Int) -> Int
{
if (start > last)
{
// When result are not exist
return 0;
}
else if (start == last)
{
if (start * start == num)
{
return start;
}
return 0;
}
else
{
// Get the middle at the range from start to last
let mid = (start + last) / 2;
if (mid * mid == num)
{
// When mid is perfect square of given num
return mid;
}
if (mid * mid < num)
{
// Change the current range from mid+1 to last
return self.findPerfectSquare(num, mid + 1, last);
}
else
{
// Change the current range from start to last-1
return self.findPerfectSquare(num, start, last - 1);
}
}
}
// Handles the request to find perfect square of given num
func isPerfectSquare(_ num: Int)
{
if (num < 0)
{
// Because negative number cannot be a perfect square
return;
}
// Display given number
print("\n Given number : ", num,terminator: "");
let result = self.findPerfectSquare(num, 1, num);
if (result > 0)
{
print("\n Perfect Square : (", result ,"²)",
separator: "", terminator: "");
}
else
{
print("\n Perfect Square : None", terminator: "");
}
}
}
func main()
{
let task = PerfectSquare();
// Test Cases
task.isPerfectSquare(9);
task.isPerfectSquare(196);
task.isPerfectSquare(37);
task.isPerfectSquare(324);
}
main();
input
Given number : 9
Perfect Square : (3²)
Given number : 196
Perfect Square : (14²)
Given number : 37
Perfect Square : None
Given number : 324
Perfect Square : (18²)
/*
Kotlin Program
Find perfect square using binary search
*/
class PerfectSquare
{
// Find perfect square of given number using recursion
// and implemented by binary search
fun findPerfectSquare(num: Int, start: Int, last: Int): Int
{
if (start > last)
{
// When result are not exist
return 0;
}
else if (start == last)
{
if (start * start == num)
{
return start;
}
return 0;
}
else
{
// Get the middle at the range from start to last
val mid: Int = (start + last) / 2;
if (mid * mid == num)
{
// When mid is perfect square of given num
return mid;
}
if (mid * mid < num)
{
// Change the current range from mid+1 to last
return this.findPerfectSquare(num, mid + 1, last);
}
else
{
// Change the current range from start to last-1
return this.findPerfectSquare(num, start, last - 1);
}
}
}
// Handles the request to find perfect square of given num
fun isPerfectSquare(num: Int): Unit
{
if (num < 0)
{
// Because negative number cannot be a perfect square
return;
}
// Display given number
print("\n Given number : " + num);
val result: Int = this.findPerfectSquare(num, 1, num);
if (result > 0)
{
print("\n Perfect Square : (" + result + "²)");
}
else
{
print("\n Perfect Square : None");
}
}
}
fun main(args: Array < String > ): Unit
{
val task: PerfectSquare = PerfectSquare();
// Test Cases
task.isPerfectSquare(9);
task.isPerfectSquare(196);
task.isPerfectSquare(37);
task.isPerfectSquare(324);
}
input
Given number : 9
Perfect Square : (3²)
Given number : 196
Perfect Square : (14²)
Given number : 37
Perfect Square : None
Given number : 324
Perfect Square : (18²)
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