[Challenge] Multiply 2 numbers without '+-*/' operators and 'for' and 'while' keywords
Keff

Keff @nombrekeff

Location:
Remote
Joined:
Jun 29, 2019

[Challenge] Multiply 2 numbers without '+-*/' operators and 'for' and 'while' keywords

Publish Date: Jul 11 '22
12 8

Following on the previous challenge, I thought of doing a similar challenge but for multiplication.

Can you multiply 2 numbers without using the following operators and keywords?

Rules:

  • Multiply a and b
  • Don't use operators: +-*/
  • Don't use keywords: for/while

Pseudocode:

a = 2
b = 32

multiply(a, b) => 64
Enter fullscreen mode Exit fullscreen mode

Test:

multiply(a, b) == a * b
Enter fullscreen mode Exit fullscreen mode

You can use any language you want, additionally you can add a note indicating which language it is, so people not familiar with the language can know!

That's all, have fun!!

My solution:

Comments 8 total

  • Matt Ellen-Tsivintzeli
    Matt Ellen-TsivintzeliJul 11, 2022

    Nice! My version only works for positive integers. I'm not sure how to do it for floating point:

      function plultimy(a, b)
      {
        let p1 = '1'.repeat(a);
        let p2 = p1.repeat(b);
        return p2.length;
      }
    
    Enter fullscreen mode Exit fullscreen mode
    • Keff
      KeffJul 11, 2022

      Nice solution, did not think of this one!!!! I also don't know how to make this solution work with floating points, seems nontrivial...

    • Matt Ellen-Tsivintzeli
      Matt Ellen-TsivintzeliJul 11, 2022

      OK, this seems very silly, but worth a try. I think this works for floating point numbers:

        function plultimy(a, b)
        {
          let negative = false;
      
          if(a < 0)
          {
            negative = true;
            a = Math.abs(a);
          }
      
          if(b < 0)
          {
            negative = true && !negative;
            b = Math.abs(b);
          }
      
          let astr = a.toString(10);
          let adec = 0;
          if(astr.indexOf('.') > ''.indexOf('l')) 
          {
            adec = astr.split('.')[1].length;
          }
      
          a = parseInt(astr.replace('.', ''), 10);
      
          let bstr = b.toString(10);
          let bdec = 0;
          if(bstr.indexOf('.') > ''.indexOf('l')) 
          {
            bdec = bstr.split('.')[1].length;
          }
      
          b = parseInt(bstr.replace('.', ''), 10);
      
          let p1 = '1'.repeat(a);
          let p2 = p1.repeat(b);
      
          let predot = p2.length.toString();
          let withdot = predot;
      
          let fulldec = '1'.repeat(adec).concat('1'.repeat(bdec)).length
      
          if(fulldec > 0)
          {
            if(predot.length < fulldec)
            {
              let diff = '1'.repeat(fulldec).slice(predot.length).length;
              predot = '0'.repeat(diff).concat(predot);
            }
            withdot = predot.slice(0, predot.slice(fulldec).length).concat('.').concat(predot.slice(predot.slice(fulldec).length));
          }
      
          let result = (negative ? String.fromCodePoint(45) : '').concat(withdot);
      
          return result;
        }
      
      Enter fullscreen mode Exit fullscreen mode
      • Keff
        KeffJul 11, 2022

        Wow, how ugly xD I love it!!!! This is why I love putting this quirky little challenges out there!!

  • Rudy Nappée
    Rudy NappéeJul 11, 2022

    One liner 😉

    const multiply = (a, b) => new Array(a).fill(0).flatMap(_ => new Array(b).fill(0)).length
    
    Enter fullscreen mode Exit fullscreen mode
    • Keff
      KeffJul 12, 2022

      Nice solution, this one wont work for floating point numbers right?

      • Rudy Nappée
        Rudy NappéeJul 12, 2022

        Sure, and it would be better not trying multiply(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER) :D

  • Jon Randy 🎖️
    Jon Randy 🎖️Jul 12, 2022

    Basically a repost of my dangerous add 😋

    const multiply = (a,b)=>eval(`${a}${String.fromCharCode(42)}${b}`)
    
    Enter fullscreen mode Exit fullscreen mode
Add comment