【问题标题】:How to get the product of two integers without using *如何在不使用 * 的情况下获得两个整数的乘积
【发布时间】:2018-03-06 17:09:58
【问题描述】:

我正在尝试找出在我的解决方案中不使用 * 的情况下获得两个整数乘积的方法。我得到的最接近的是

/*Example: 2,5 output ====> 10 */
/*Example: 10,5 output ====> 50 */

const productOfTwoInt = (int,int2) => {
  var data1 = 0;
  var data2 = 0;

  var result;
  var result2;  

  for(var x = 0; x <= int; x++) {
      result = x += data1
      console.log(result)
  }

  for(var j = 0; j <= int2; j++) {
      result2 = j += data2
  }

  return result + result2    
}

console.log(productOfTwoInt(3,5))

【问题讨论】:

  • for (var i = 0; i &lt; int; i++) result += int2;

标签: javascript math integer


【解决方案1】:

您的解决方案看起来已经相当不错了。这可以简化为:

function multiply(a,b){
  //just one minus, lets swap
  if(a<0 && b>0) [a,b] = [b,a];
  //two minuses: let them be positive
  if(a<0 && b<0) ( a = Math.abs(a), b = Math.abs(b) );
  var result = 0;
  while(a--){
    result += b;
  }
  return result;
}

console.log(
  multiply(1,2),
  multiply(3,4),
  multiply(6,7),
  multiply(-1,-2),
  multiply(1,-2)
);

【讨论】:

  • 如果a 是负数,你会得到一个无限循环。
【解决方案2】:

你可以的

const productOfTwoInt = (x, y) => {
    let z = Math.log(x) + Math.log(y);
    return Math.round(Math.exp(z));
}

console.log(productOfTwoInt(3,5))

【讨论】:

  • 智能,但仅适用于正整数。试试productOfTwoInt(-2, 5)
【解决方案3】:

正如 Nina Scholz 的 answer 所建议的,“俄罗斯乘法”:

const productOfTwoInt = (a, b) => {

    var a0 = a;
    var result = 0;

    while (1) {
        if (a0 % 2 !== 0)
            result += b;
            
        if (a0 == 1 || a0 == -1) break;
        
        a0 = Math.floor(a0 / 2);
        b *= 2;
    }
		return a > 0 ? result : -result;
}

console.log(
    productOfTwoInt(4, 5),
    productOfTwoInt(4, -5),
    productOfTwoInt(-40, 5),
    productOfTwoInt(-4, -50)
);

传统解决方案:

 const productOfTwoInt = (int, int2) => {
        
        let result = 0;
        let isPositive = int > 0;
        let i = 0;
    
    
        while (i != int) {
            result += int2;
            if (isPositive)
                i++;
            else
                i--;
        }
    
	return isPositive ? result : -result;
    
    }
    
    console.log(
      productOfTwoInt(4, 5),
      productOfTwoInt(4, -5),
      productOfTwoInt(-4, 5),
      productOfTwoInt(-4, -5)
    );

【讨论】:

    【解决方案4】:

    您可以进行一些位移,称为ancient egyptian multiplicationrussian multiplication

      a     b     p   comment
    ----  ----  ----  ----------------------------------
      6     8     0   skip, because a is even
      3    16    16   add 16 to p, because a is odd
      1    32    48   add 32 to p, because a is odd
      0    64    48   stop iteration, because a is zero
    

    function product(a, b) {
        var p = 0;
        while (a) {
            p += (a & 1) && b;
            a >>= 1;
            b <<= 1;
        }
        return p;
    }
    
    console.log(product(6, 8)); // 48

    【讨论】:

    • 不错 ;) 负数怎么样?
    • @Jonasw,您可以存储该标志并稍后应用它。
    【解决方案5】:

    你可以这样做

    选项 1

    function mul(m,n){
       // make it more efficient
        if(m<n)
         {
          var temp =m;
          m=n;
          n=temp;
         }   
     var v=0;
     if(n==1) 
          return m;
     v = mul(m,n>>1);
     v = v+v;
     if(n&1) 
          v+= m;
     return v;
    }
    
    console.log(mul(-2,5));
    

    Working Example

    【讨论】:

      【解决方案6】:

      你可以用这样的逆来做:

      function multiply(a, b) {
        return a / (1 / b)
      }
      
      multiply(2, 5)  // 10
      multiply(10, 5)  // 50
      

      【讨论】:

        猜你喜欢
        • 2015-07-27
        • 1970-01-01
        • 2014-03-13
        • 2018-10-02
        • 1970-01-01
        • 2011-01-31
        • 1970-01-01
        • 2020-08-10
        • 2019-07-19
        相关资源
        最近更新 更多