Multiplication with a power of 2
Last Updated :
26 May, 2022
Given two numbers x and n, we need to multiply x with 2n
Examples :
Input : x = 25, n = 3
Output : 200
25 multiplied by 2 raised to power 3
is 200.
Input : x = 70, n = 2
Output : 280
A simple solution is to compute n-th power of 2 and then multiply with x.
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll power2(ll n)
{
if (n == 0)
return 1;
if (n == 1)
return 2;
return power2(n / 2) *
power2(n / 2);
}
ll multiply(ll x, ll n)
{
return x * power2(n);
}
int main()
{
ll x = 70, n = 2;
cout<<multiply(x, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static long power2( long n)
{
if (n == 0 )
return 1 ;
if (n == 1 )
return 2 ;
return power2(n / 2 )
* power2(n / 2 );
}
static long multiply( long x, long n)
{
return x * power2(n);
}
public static void main(String[] args)
{
long x = 70 , n = 2 ;
System.out.println(multiply(x, n));
}
}
|
Python3
def power2(n):
if (n = = 0 ):
return 1
if (n = = 1 ):
return 2
return power2(n / 2 ) *
power2(n / 2 );
def multiply(x, n):
return x * power2(n);
x = 70
n = 2
print (multiply(x, n))
|
C#
using System;
class GFG {
static long power2( long n)
{
if (n == 0)
return 1;
if (n == 1)
return 2;
return power2(n / 2)
* power2(n / 2);
}
static long multiply( long x, long n)
{
return x * power2(n);
}
public static void Main()
{
long x = 70, n = 2;
Console.WriteLine(multiply(x, n));
}
}
|
PHP
<?php
function power2( $n )
{
if ( $n == 0)
return 1;
if ( $n == 1)
return 2;
return power2( $n / 2) *
power2( $n / 2);
}
function multiply( $x , $n )
{
return $x * power2( $n );
}
$x = 70; $n = 2;
echo multiply( $x , $n );
?>
|
Javascript
<script>
function power2(n)
{
if (n == 0)
return 1;
if (n == 1)
return 2;
return power2(n / 2) *
power2(n / 2);
}
function multiply( x, n)
{
return x * power2(n);
}
let x = 70
let n = 2;
document.write( multiply(x, n));
</script>
|
Output :
280
Time complexity : O(logn)
Auxiliary Space : O(logn)
An efficient solution is to use bitwise leftshift operator. We know 1 << n means 2 raised to power n.
C++
#include <stdio.h>
typedef long long int ll;
ll multiply(ll x, ll n)
{
return x << n;
}
int main()
{
ll x = 70, n = 2;
printf ( "%lld" , multiply(x, n));
return 0;
}
|
Java
import java.util.*;
class GFG {
static long multiply( long x, long n)
{
return x << n;
}
public static void main(String[] args)
{
long x = 70 , n = 2 ;
System.out.println(multiply(x, n));
}
}
|
Python3
def multiply( x , n ):
return x << n
x = 70
n = 2
print ( multiply(x, n))
|
C#
using System;
class GFG {
static int multiply( int x, int n)
{
return x << n;
}
public static void Main()
{
int x = 70, n = 2;
Console.WriteLine(multiply(x, n));
}
}
|
PHP
<?php
function multiply( $x , $n )
{
return $x << $n ;
}
$x = 70;
$n = 2;
echo multiply( $x , $n );
?>
|
Javascript
<script>
function multiply(x, n)
{
return x << n;
}
let x = 70;
let n = 2;
document.write(multiply(x, n));
</script>
|
Output :
280
Time Complexity : O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...