“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
Read more comments...
Give a fast way to multiply a number by 7.
5,802 Views | (12 votes, avg: 2.67)
n<<3 - n = 7n
N<<3+N=7N
this will work only for integers !!what about doubles , floats ????
deep, If interviewer asks you to do this for float or doubles.. you need to say “there is no way except ‘7*N’ “.
Hope you are happy now.
Just a correction :
N
Give a fast way to multiply a number by n.
deep,
Depending on the architecture and instruction timings, something like
const double A = N+N; result = (A+A)+(A+N);
might be faster (4 additions, and maybe two of them can run in parallel) than just doing result = N*7.0;
7N = N
Assuming binary numbers, 7 can be broken down as (4 + 2 + 1) So the multiplication with x becomes
x*(4+2+1) giving x*4 + x*2 + x Using shift operations and addition, multiplication should be faster.
I’m not sure about the correctness of this answer though.
Name (required)
Mail (will not be published) (required)
Your Answer
To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
n<<3 - n = 7n
N<<3+N=7N
this will work only for integers !!
what about doubles , floats ????
deep,
If interviewer asks you to do this for float or doubles.. you need to say “there is no way except ‘7*N’ “.
Hope you are happy now.
Just a correction :
N
Give a fast way to multiply a number by n.
deep,
Depending on the architecture and instruction timings, something like
const double A = N+N;
result = (A+A)+(A+N);
might be faster (4 additions, and maybe two of them can run in parallel) than just doing
result = N*7.0;
7N = N
Assuming binary numbers,
7 can be broken down as (4 + 2 + 1)
So the multiplication with x becomes
x*(4+2+1) giving x*4 + x*2 + x
Using shift operations and addition, multiplication should be faster.
I’m not sure about the correctness of this answer though.
Leave an Answer/Comment