Given an integer x, write a function that multiplies x with 3.5 and returns the integer result. You are not allowed to use %, /, *.
Examples :
Input: 2
Output: 7
Input: 5
Output: 17 (Ignore the digits after decimal point)
Solution:
1. We can get x*3.5 by adding 2*x, x and x/2. To calculate 2*x, left shift x by 1 and to calculate x/2, right shift x by 1.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int multiplyWith3Point5(int x)
{
return (x<<1) + x + (x>>1);
}
int main()
{
int x = 4;
cout << " "<< multiplyWith3Point5(x);
getchar();
return 0;
}
|
C
#include <stdio.h>
int multiplyWith3Point5(int x)
{
return (x<<1) + x + (x>>1);
}
int main()
{
int x = 4;
printf("%d", multiplyWith3Point5(x));
getchar();
return 0;
}
|
Java
class GFG {
static int multiplyWith3Point5(int x)
{
return (x<<1) + x + (x>>1);
}
public static void main(String[] args)
{
int x = 2;
System.out.println(multiplyWith3Point5(x));
}
}
|
Python3
def multiplyWith3Point5(x):
return (x<<1) + x + (x>>1)
x = 4
print(multiplyWith3Point5(x))
|
C#
using System;
class GFG
{
static int multiplyWith3Point5(int x)
{
return (x<<1) + x + (x>>1);
}
public static void Main()
{
int x = 2;
Console.Write(multiplyWith3Point5(x));
}
}
|
PHP
<?php
function multiplyWith3Point5( $x)
{
return ($x << 1) + $x + ($x >> 1);
}
$x = 4;
echo multiplyWith3Point5($x);
?>
|
Javascript
<script>
function multiplyWith3Point5(x)
{
return (x<<1) + x + (x>>1);
}
var x = 4;
document.write(multiplyWith3Point5(x));
</script>
|
Time Complexity : O(1)
Space Complexity : O(1)
2. Another way of doing this could be (8*x – x)/2 (See below code). Thanks to Ajaym for suggesting this.
C++
#include <iostream>
using namespace std;
int multiplyWith3Point5(int x)
{
return ((x<<3) - x)>>1;
}
|
C
#include <stdio.h>
int multiplyWith3Point5(int x)
{
return ((x<<3) - x)>>1;
}
|
Java
import java.io.*;
class GFG
{
static int multiplyWith3Point5(int x)
{
return ((x<<3) - x)>>1;
}
}
|
Python3
def multiplyWith3Point5(x):
return ((x<<3) - x)>>1
|
C#
using System;
class GFG{
static int multiplyWith3Point5(int x)
{
return ((x<<3) - x)>>1;
}
}
|
Javascript
function multiplyWith3Point5(x)
{
return ((x<<3) - x)>>1;
}
|
Time Complexity : O(1)
Space Complexity : O(1)
Another Approach:
Another way of doing this could be by doing a binary multiplication by 7 then divide by 2 using only <<, ^, &, and >>.
But here we have to mention that only positive numbers can be passed to this method.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int multiplyWith3Point5(int x)
{
int r = 0;
int x1Shift = x << 1;
int x2Shifts = x << 2;
r = (x ^ x1Shift) ^ x2Shifts;
int c = (x & x1Shift) | (x & x2Shifts) | (x1Shift & x2Shifts);
while (c > 0) {
c <<= 1;
int t = r;
r ^= c;
c &= t;
}
r = r >> 1;
return r;
}
int main()
{
cout << (multiplyWith3Point5(5));
return 0;
}
|
C
#include <stdio.h>
int multiplyWith3Point5(int x)
{
int r = 0;
int x1Shift = x << 1;
int x2Shifts = x << 2;
r = (x ^ x1Shift) ^ x2Shifts;
int c = (x & x1Shift) | (x & x2Shifts) | (x1Shift & x2Shifts);
while (c > 0) {
c <<= 1;
int t = r;
r ^= c;
c &= t;
}
r = r >> 1;
return r;
}
int main()
{
printf("%d",(multiplyWith3Point5(5)));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int multiplyWith3Point5(int x)
{
int r = 0;
int x1Shift = x << 1;
int x2Shifts = x << 2;
r = (x ^ x1Shift) ^ x2Shifts;
int c = (x & x1Shift) | (x & x2Shifts)
| (x1Shift & x2Shifts);
while (c > 0) {
c <<= 1;
int t = r;
r ^= c;
c &= t;
}
r = r >> 1;
return r;
}
public static void main(String[] args)
{
System.out.println(multiplyWith3Point5(5));
}
}
|
Python3
def multiplyWith3Point5(x):
r = 0
x1Shift = x << 1
x2Shifts = x << 2
r = (x ^ x1Shift) ^ x2Shifts
c = (x & x1Shift) | (x & x2Shifts) | (x1Shift & x2Shifts)
while (c > 0):
c <<= 1
t = r
r ^= c
c &= t
r = r >> 1
return r
if __name__ == '__main__':
print(multiplyWith3Point5(5))
|
C#
using System;
class GFG{
static int multiplyWith3Point5(int x)
{
int r = 0;
int x1Shift = x << 1;
int x2Shifts = x << 2;
r = (x ^ x1Shift) ^ x2Shifts;
int c = (x & x1Shift) | (x & x2Shifts) |
(x1Shift & x2Shifts);
while (c > 0)
{
c <<= 1;
int t = r;
r ^= c;
c &= t;
}
r = r >> 1;
return r;
}
public static void Main(string[] args)
{
Console.WriteLine(multiplyWith3Point5(5));
}
}
|
Javascript
<script>
function multiplyWith3Polet5(x)
{
let r = 0;
let x1Shift = x << 1;
let x2Shifts = x << 2;
r = (x ^ x1Shift) ^ x2Shifts;
let c = (x & x1Shift) | (x & x2Shifts) |
(x1Shift & x2Shifts);
while (c > 0)
{
c <<= 1;
let t = r;
r ^= c;
c &= t;
}
r = r >> 1;
return r;
}
document.write(multiplyWith3Polet5(5));
</script>
|
Time Complexity : O(log n)
Space Complexity : O(1)
Please write comments if you find the above code/algorithm incorrect, or find better ways to solve the same problem
Last Updated :
13 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...