Given an integer n, calculate the square of a number without using *, / and pow().
Examples :
Input: n = 5
Output: 25
Input: 7
Output: 49
Input: n = 12
Output: 144
A Simple Solution is to repeatedly add n to result.
Below is the implementation of this idea.
C++
#include <iostream>
using namespace std;
int square( int n)
{
if (n < 0)
n = -n;
int res = n;
for ( int i = 1; i < n; i++)
res += n;
return res;
}
int main()
{
for ( int n = 1; n <= 5; n++)
cout << "n = " << n << ", n^2 = " << square(n)
<< endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int square( int n)
{
if (n < 0 )
n = -n;
int res = n;
for ( int i = 1 ; i < n; i++)
res += n;
return res;
}
public static void main(String[] args)
{
for ( int n = 1 ; n <= 5 ; n++)
System.out.println( "n = " + n
+ ", n^2 = " + square(n));
}
}
|
Python3
def square(n):
if (n < 0 ):
n = - n
res = n
for i in range ( 1 , n):
res + = n
return res
for n in range ( 1 , 6 ):
print ( "n =" , n, end = ", " )
print ( "n^2 =" , square(n))
|
C#
using System;
class GFG {
public static int square( int n)
{
if (n < 0)
n = -n;
int res = n;
for ( int i = 1; i < n; i++)
res += n;
return res;
}
public static void Main()
{
for ( int n = 1; n <= 5; n++)
Console.WriteLine( "n = " + n
+ ", n^2 = " + square(n));
}
}
|
PHP
<?php
function square( $n )
{
if ( $n < 0) $n = - $n ;
$res = $n ;
for ( $i = 1; $i < $n ; $i ++)
$res += $n ;
return $res ;
}
for ( $n = 1; $n <=5; $n ++)
echo "n = " , $n , ", " , "n^2 = " ,
square( $n ), "\n " ;
?>
|
Javascript
<script>
function square(n)
{
if (n < 0)
n = -n;
let res = n;
for (let i = 1; i < n; i++)
res += n;
return res;
}
for (let n = 1; n <= 5; n++)
document.write( "n= " + n + ", n^2 = " + square(n)
+ "<br>" );
</script>
|
Output
n = 1, n^2 = 1
n = 2, n^2 = 4
n = 3, n^2 = 9
n = 4, n^2 = 16
n = 5, n^2 = 25
Time Complexity: O(n)
Auxiliary Space: O(1)
Approach 2:
We can do it in O(Logn) time using bitwise operators. The idea is based on the following fact.
square(n) = 0 if n == 0
if n is even
square(n) = 4*square(n/2)
if n is odd
square(n) = 4*square(floor(n/2)) + 4*floor(n/2) + 1
Examples
square(6) = 4*square(3)
square(3) = 4*(square(1)) + 4*1 + 1 = 9
square(7) = 4*square(3) + 4*3 + 1 = 4*9 + 4*3 + 1 = 49
How does this work?
If n is even, it can be written as
n = 2*x
n2 = (2*x)2 = 4*x2
If n is odd, it can be written as
n = 2*x + 1
n2 = (2*x + 1)2 = 4*x2 + 4*x + 1
floor(n/2) can be calculated using a bitwise right shift operator. 2*x and 4*x can be calculated
Below is the implementation based on the above idea.
C++
#include <bits/stdc++.h>
using namespace std;
int square( int n)
{
if (n == 0)
return 0;
if (n < 0)
n = -n;
int x = n >> 1;
if (n & 1)
return ((square(x) << 2) + (x << 2) + 1);
else
return (square(x) << 2);
}
int main()
{
for ( int n = 1; n <= 5; n++)
cout << "n = " << n << ", n^2 = " << square(n)
<< endl;
return 0;
}
|
Java
class GFG {
static int square( int n)
{
if (n == 0 )
return 0 ;
if (n < 0 )
n = -n;
int x = n >> 1 ;
;
if (n % 2 != 0 )
return ((square(x) << 2 ) + (x << 2 ) + 1 );
else
return (square(x) << 2 );
}
public static void main(String args[])
{
for ( int n = 1 ; n <= 5 ; n++)
System.out.println( "n = " + n
+ " n^2 = " + square(n));
}
}
|
Python3
def square(n):
if (n = = 0 ):
return 0
if (n < 0 ):
n = - n
x = n >> 1
if (n & 1 ):
return ((square(x) << 2 )
+ (x << 2 ) + 1 )
else :
return (square(x) << 2 )
for n in range ( 1 , 6 ):
print ( "n = " , n, " n^2 = " ,
square(n))
|
C#
using System;
class GFG {
static int square( int n)
{
if (n == 0)
return 0;
if (n < 0)
n = -n;
int x = n >> 1;
;
if (n % 2 != 0)
return ((square(x) << 2) + (x << 2) + 1);
else
return (square(x) << 2);
}
static void Main()
{
for ( int n = 1; n <= 5; n++)
Console.WriteLine( "n = " + n
+ " n^2 = " + square(n));
}
}
|
PHP
<?php
function square( $n )
{
if ( $n ==0) return 0;
if ( $n < 0) $n = - $n ;
$x = $n >> 1;
if ( $n & 1)
return ((square( $x ) << 2) +
( $x << 2) + 1);
else
return (square( $x ) << 2);
}
for ( $n = 1; $n <= 5; $n ++)
echo "n = " , $n , ", n^2 = " , square( $n ), "\n" ;
?>
|
Javascript
<script>
function square(n)
{
if (n == 0)
return 0;
if (n < 0)
n = -n;
let x = n >> 1;
if (n & 1)
return ((square(x) << 2) + (x << 2) + 1);
else
return (square(x) << 2);
}
for (let n = 1; n <= 5; n++)
document.write( "n = " + n + ", n^2 = " + square(n)
+ "<br>" );
</script>
|
Output
n = 1, n^2 = 1
n = 2, n^2 = 4
n = 3, n^2 = 9
n = 4, n^2 = 16
n = 5, n^2 = 25
Time Complexity: O(log n)
Auxiliary Space: O(log n) as well, as the number of function calls stored in the call stack will be logarithmic to the size of the input
Approach 3:
For a given number `num` we get square of it by multiplying number as `num * num`.
Now write one of `num` in square `num * num` in terms of power of `2`. Check below examples.
Eg: num = 10, square(num) = 10 * 10
= 10 * (8 + 2) = (10 * 8) + (10 * 2)
num = 15, square(num) = 15 * 15
= 15 * (8 + 4 + 2 + 1) = (15 * 8) + (15 * 4) + (15 * 2) + (15 * 1)
Multiplication with power of 2's can be done by left shift bitwise operator.
Below is the implementation based on the above idea.
C++
#include <iostream>
using namespace std;
int square( int num)
{
if (num < 0) num = -num;
int result = 0, times = num;
while (times > 0)
{
int possibleShifts = 0, currTimes = 1;
while ((currTimes << 1) <= times)
{
currTimes = currTimes << 1;
++possibleShifts;
}
result = result + (num << possibleShifts);
times = times - currTimes;
}
return result;
}
int main()
{
for ( int n = 10; n <= 15; ++n)
cout << "n = " << n << ", n^2 = " << square(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG{
public static int square( int num)
{
if (num < 0 )
num = -num;
int result = 0 , times = num;
while (times > 0 )
{
int possibleShifts = 0 ,
currTimes = 1 ;
while ((currTimes << 1 ) <= times)
{
currTimes = currTimes << 1 ;
++possibleShifts;
}
result = result + (num << possibleShifts);
times = times - currTimes;
}
return result;
}
public static void main(String[] args)
{
for ( int n = 10 ; n <= 15 ; ++n)
{
System.out.println( "n = " + n +
", n^2 = " +
square(n));
}
}
}
|
Python3
def square(num):
if (num < 0 ):
num = - num
result, times = 0 , num
while (times > 0 ):
possibleShifts, currTimes = 0 , 1
while ((currTimes << 1 ) < = times):
currTimes = currTimes << 1
possibleShifts + = 1
result = result + (num << possibleShifts)
times = times - currTimes
return result
for n in range ( 10 , 16 ):
print ( "n =" , n, ", n^2 =" , square(n))
|
C#
using System;
class GFG {
static int square( int num)
{
if (num < 0)
num = -num;
int result = 0, times = num;
while (times > 0)
{
int possibleShifts = 0,
currTimes = 1;
while ((currTimes << 1) <= times)
{
currTimes = currTimes << 1;
++possibleShifts;
}
result = result + (num << possibleShifts);
times = times - currTimes;
}
return result;
}
static void Main() {
for ( int n = 10; n <= 15; ++n)
{
Console.WriteLine( "n = " + n +
", n^2 = " +
square(n));
}
}
}
|
Javascript
<script>
function square(num)
{
if (num < 0) num = -num;
let result = 0, times = num;
while (times > 0)
{
let possibleShifts = 0, currTimes = 1;
while ((currTimes << 1) <= times)
{
currTimes = currTimes << 1;
++possibleShifts;
}
result = result + (num << possibleShifts);
times = times - currTimes;
}
return result;
}
for (let n = 10; n <= 15; ++n)
document.write( "n = " + n + ", n^2 = " + square(n) + "<br>" );
</script>
|
Output
n = 10, n^2 = 100
n = 11, n^2 = 121
n = 12, n^2 = 144
n = 13, n^2 = 169
n = 14, n^2 = 196
n = 15, n^2 = 225
Time Complexity: O(logn)
Auxiliary Space: O(1)
Thanks to Sanjay for approach 3 solution.
This article is contributed by Ujjwal Jain. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
C++
#include <iostream>
using namespace std;
int square( int num)
{
if (num < 0)
num = -num;
int power = 0, result = 0;
int temp = num;
while (temp) {
if (temp & 1) {
result += (num << power);
}
power++;
temp = temp >> 1;
}
return result;
}
int main()
{
for ( int n = 10; n <= 15; ++n)
cout << "n = " << n << ", n^2 = " << square(n)
<< endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class Main {
public static int square( int num)
{
if (num < 0 )
num = -num;
int power = 0 , result = 0 ;
int temp = num;
while (temp > 0 ) {
if ((temp & 1 ) > 0 ) {
result += (num << power);
}
power++;
temp = temp >> 1 ;
}
return result;
}
public static void main(String[] args) {
for ( int n = 10 ; n <= 15 ; ++n)
System.out.println( "n = " + n + ", n^2 = " + square(n));
}
}
|
Python3
def square(num):
if num < 0 :
num = - num
power, result = 0 , 0
temp = num
while temp:
if temp & 1 :
result + = (num << power)
power + = 1
temp = temp >> 1
return result
for n in range ( 10 , 16 ):
print (f "n = {n}, n^2 = {square(n)}" )
|
Javascript
function square(num) {
if (num < 0)
num = -num;
let power = 0, result = 0;
let temp = num;
while (temp) {
if (temp & 1) {
result += (num << power);
}
power++;
temp = temp >> 1;
}
return result;
}
for (let n = 10; n <= 15; ++n)
console.log(`n = ${n}, n^2 = ${square(n)}`);
|
C#
using System;
public class Program
{
public static int Square( int num)
{
if (num < 0)
num = -num;
int power = 0, result = 0;
int temp = num;
while (temp > 0)
{
if ((temp & 1) > 0)
{
result += (num << power);
}
power++;
temp = temp >> 1;
}
return result;
}
public static void Main()
{
for ( int n = 10; n <= 15; ++n)
Console.WriteLine( "n = " + n + ", n^2 = " + Square(n));
}
}
|
Output
n = 10, n^2 = 100
n = 11, n^2 = 121
n = 12, n^2 = 144
n = 13, n^2 = 169
n = 14, n^2 = 196
n = 15, n^2 = 225
Time Complexity: O(logn)
Auxiliary Space: O(1)