Give a fast method to multiply a number by 7.
Solution :
- We multiply by 7 by first multiplying by 8 and subtracting the number from the result.
- Multiplication by 8 is equivalent to shifting left 3 bits.
- Time : O(1)
- Space : O(1)
int multiply_7(int N){
return (N << 3) - N;
}
No comments:
Post a Comment