multiplication of two nmbers such that there are minimum number of addition operations
Write a recursive method to get the multiplication of two numbers such that there are minimum number of addition operations.
// the number of m is n m>n
int minAdd(int m,int n) { //modify the name as minAdd(int bigNumber,int smallNumber) which is clear to understand the meaning
if(n==0) return 0;
if(n==1) return m;
if(m<n) return minAdd(n,m);
int res = minAdd(m,n>>1);
if((n&0x1)==1) {
return res+res+m;
} else {
return res+res;
}
}
int minMulti(int m,int n) {
if(m>n) {
return minAdd(m,n);
} else {
return minAdd(n,m);
}
}