2017-09-25 36 views
3

的情況下獲取兩個整數的乘積我試圖找出在我的解決方案中不使用*就可以得到兩個整數乘積的方法。我得到的最接近的是如何在不使用*

/*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)) 
+0

'的(VAR I = 0;我 shmosel

回答

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) 
 
);

+2

如果'a'是負數,你會得到一個無限循環。 – Amy

+0

@amy jup固定... –

1

你可以做

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

 
console.log(productOfTwoInt(3,5))

+0

聰明,但只會對正整數有效。試試'productOfTwoInt(-2,5)'。 – dfsq

0

正如尼娜肖爾茨的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; 
 
    } 
 
\t \t 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--; 
 
     } 
 
    
 
\t return isPositive ? result : -result; 
 
    
 
    } 
 
    
 
    console.log(
 
     productOfTwoInt(4, 5), 
 
     productOfTwoInt(4, -5), 
 
     productOfTwoInt(-4, 5), 
 
     productOfTwoInt(-4, -5) 
 
    );

7

你可以採取一些位轉移,被稱爲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

+0

不錯的一個;)如何負數? –

+0

@Jonasw,你把標誌存儲起來,稍後再應用。 –

0

你可以用它做這樣

選項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

2

你可以用逆像這樣做:

function multiply(a, b) { 
    return a/(1/b) 
} 

multiply(2, 5) // 10 
multiply(10, 5) // 50