Check perfect square using addition/subtraction
Last Updated :
01 Apr, 2021
Given a positive integer n, check if it is perfect square or not using only addition/subtraction operations and in minimum time complexity.
We strongly recommend you to minimize your browser and try this yourself first.
We can use the property of odd number for this purpose:
Addition of first n odd numbers is always perfect square
1 + 3 = 4,
1 + 3 + 5 = 9,
1 + 3 + 5 + 7 + 9 + 11 = 36 ...
Below is the implementation of above idea :
C++
#include <bits/stdc++.h>
using namespace std;
bool isPerfectSquare( int n)
{
for ( int sum = 0, i = 1; sum < n; i += 2) {
sum += i;
if (sum == n)
return true ;
}
return false ;
}
int main()
{
isPerfectSquare(35) ? cout << "Yes\n" : cout << "No\n" ;
isPerfectSquare(49) ? cout << "Yes\n" : cout << "No\n" ;
return 0;
}
|
Java
public class GFG {
static boolean isPerfectSquare( int n)
{
for ( int sum = 0 , i = 1 ; sum < n; i += 2 ) {
sum += i;
if (sum == n)
return true ;
}
return false ;
}
public static void main(String args[])
{
if (isPerfectSquare( 35 ))
System.out.println( "Yes" );
else
System.out.println( "NO" );
if (isPerfectSquare( 49 ))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isPerfectSquare(n):
i = 1
the_sum = 0
while the_sum < n:
the_sum + = i
if the_sum = = n:
return True
i + = 2
return False
if __name__ = = "__main__" :
print ( 'Yes' ) if isPerfectSquare( 35 ) else print ( 'NO' )
print ( 'Yes' ) if isPerfectSquare( 49 ) else print ( 'NO' )
|
C#
using System;
public class GFG {
static bool isPerfectSquare( int n)
{
for ( int sum = 0, i = 1; sum < n; i += 2) {
sum += i;
if (sum == n)
return true ;
}
return false ;
}
public static void Main(String[] args)
{
if (isPerfectSquare(35))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
if (isPerfectSquare(49))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function isPerfectSquare( $n )
{
for ( $sum = 0, $i = 1; $sum < $n ;
$i += 2)
{
$sum += $i ;
if ( $sum == $n )
return true;
}
return false;
}
if (isPerfectSquare(35))
echo "Yes\n" ;
else
echo "No\n" ;
if (isPerfectSquare(49))
echo "Yes\n" ;
else
echo "No\n" ;
?>
|
Javascript
<script>
function isPerfectSquare(n)
{
for (let sum = 0, i = 1; sum < n; i += 2) {
sum += i;
if (sum == n)
return true ;
}
return false ;
}
if (isPerfectSquare(35))
document.write( "Yes" + "<br/>" );
else
document.write( "NO" + "<br/>" );
if (isPerfectSquare(49))
document.write( "Yes" + "<br/>" );
else
document.write( "No" + "<br/>" );
</script>
|
Output :
No
Yes
Time Complexity: O(n)
Auxiliary Space: O(1)
How does this work?
Below is explanation of above approach.
1 + 3 + 5 + ... (2n-1) = ?(2*i - 1) where 1<=i<=n
= 2*?i - ?1 where 1<=i<=n
= 2n(n+1)/2 - n
= n(n+1) - n
= n2
Reference:
https://www.geeksforgeeks.org/sum-first-n-odd-numbers-o1-complexity/
http://blog.jgc.org/2008/02/sum-of-first-n-odd-numbers-is-always.html
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...