简体   繁体   中英

Recursive multiply function

I'm trying to do an assignment where it asks to produce a function that multiplies a positive integer by 123 without using loops or * or the divide operator.

function multiply(x, y){
    if (y === 0) return 0;
    return x + multiply(x, y - 1);
}

I have a function that can multiply 2 numbers but how can I structure this to multiply by a fixed number?

function multiply_123(x) {
     return multiply(x, 123);
}

or a little better

function multiply_123(x) {
     if(x==0) {
         return 0;
     } else {
         if(x > 123) {
             return multiply(x, 123);
     } else {
         return multiply(123, x);
     }
}

You could add the value if the other value is odd, then add the result of the double of the first value and the half of the other value, better nown as Ancient Egyptian multiplication .

 function multiply(a, b) { if (b === 1) return a; return (b & 1 && a) + multiply(a << 1, b >> 1); } console.log(multiply(4, 5)); console.log(multiply(5, 4));

You have a function that multiplies two numbers together, now you want to have a function that only takes one argument then "sets" the other. This is called partial application

This is the basic way to do it - make a new function that only takes a single parameter and calls the other function with that, as well as a constant:

 function multiply(x, y){ if (y === 0) return 0; return x + multiply(x, y - 1); } function multiplyBy123(x) { return multiply(x, 123); } console.log(multiplyBy123(3)); console.log(multiplyBy123(10));

This can be made more generic, too:

 function multiply(x, y){ if (y === 0) return 0; return x + multiply(x, y - 1); } function multiplyBy(x) { return function (y) { return multiply(x, y); } } var multiplyBy123 = multiplyBy(123); console.log(multiplyBy123(3)); console.log(multiplyBy123(10));

This technique is called currying - the basics is that you can break up a function with multiple parameters into something that takes one and returns a function that expects the rest. So in this case, multiplyBy takes x and returns a function that expects y then when that function is invoked with the parameter, it calls multiply with both x and y .

An even simpler and generic way to partially apply a function is to use Function.bind which allows you to set some of the parameters. You can set the first one and you will get a function that expects the second parameter:

 function multiply(x, y){ if (y === 0) return 0; return x + multiply(x, y - 1); } var multiplyBy123 = multiply.bind(null, 123); console.log(multiplyBy123(3)); console.log(multiplyBy123(10));

If you are allowed to change multiply , then you can make it even simpler and just use a default parameter value to set y to 123 if not specified:

 function multiply(x, y = 123){ if (y === 0) return 0; return x + multiply(x, y - 1); } console.log(multiply(3)); console.log(multiply(10));

Example with tail recursion:

const mult = (...args) => {
  const [ a, b, ...rest] = args
  if (args.length === 2) {
    return a * b
  }
  return mult(a * b, ...rest)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM