How to Multiply a and B With My Recursive Method

How to multiply 2 numbers using recursion

The issue is that multiply's static variables persist from call to call, which throws the second calculation off. It is possible to bandage this wound, but it's better to address the underlying design problem that is compelling use of static variables in the first place. There is no need to artificially maintain state in the function using i (the number of additions to perform) and c (a product accumulator).

Given that multiplication is repeated addition of a b times, you can establish a base case of b == 0 and recursively add a, incrementing or decrementing b (depending on b's sign) until it reaches 0. The product accumulator c is replaced by the function return value and the number of multiplications i is represented by b.

Using this approach, each stack frame's state is naturally self-reliant.

#include <stdio.h>

int multiply(int a, int b) {
if (b > 0) {
return a + multiply(a, b - 1);
}
else if (b < 0) {
return -a + multiply(a, b + 1);
}

return 0;
}

int main() {
printf("%d\n", multiply(3, 6));
printf("%d\n", multiply(9, 9));
printf("%d\n", multiply(-6, 2));
printf("%d\n", multiply(6, -2));
printf("%d\n", multiply(-7, -3));
printf("%d\n", multiply(0, 7));
printf("%d\n", multiply(7, 0));
printf("%d\n", multiply(0, 0));
return 0;
}

Output:

18
81
-12
-12
21
0
0
0

As a final note, I recommend following proper code style. Minifying your code and using single-character variable names only makes debugging more difficult (someone has since de-minified the original code in an edit).

Multiplication using only recursion

Integers in Python are immutable. That means you can't modify them in place, only rebind a variable in the current scope to a new integer. This means you need to use the return values of things like incr and decr, and you need to write your add and mult functions to return their results too.

Currently your code ignores the results of the recursive calls to add and mult and unconditionally returns 0 at the end of each function. That's always going to do the wrong thing.

I think you want something more like this:

def add(a, b):
'''Returns the sum of a and b'''
# using only incr, decr, zero, and recursion

if zero(b):
return a

a = incr(a)
b = decr(b)
return add(a,b)

def mult(a, b):
'''Returns the product of a and b'''
# using only add, incr, decr, zero, and recursion

if zero(b):
return 0

b = decr(b)
c = mult(a, b)
return add(a, c)

Both of these should work for any integer a and any non-negative integer b. If you want to support negative b values, you'd need new "primitive" functions (e.g. a sign function, perhaps).

how to multiply two positive integers using recursion?

What you're doing here isn't a recursive function due to your for-loop in the function.
You'd need to implement it as follows:

#include <stdio.h>

int product(int prod, int amount) {
if (amount == 0)
return 0;

return prod + product(prod, amount - 1);
}

int main() {

int c = product(2, 3);

return 0;
}

a to the power of b - recursive algorithm

Your function int power(int a, int b) returns an int.
So every time return a * power(a, b - 1); is called, a is multiplied by the value returned by power(a, b - 1) until you get b == 0 which returns 1.

At the end you get:

return (3 * (3 * (3 * (3 * (3 * (3 * (3 * 1)))))));

The value of b is the one that stops the recursivity and make you get a result. If b wasn't decreased, you'd be in an infinite loop.

So to answer to your question, neither a or b are multiplied, since all is in the return value. Only b is decreased to make the number of loops expected.

I hope this helped you understand.

How do I do recursive multiplication when one or both of the factors is/are negative?

if (num1 == 0 || num2 == 0) {
return 0;
}

else if( num2 < 0 ) {
return - num1 + multiply2(num1, num2 + 1);
}

else {
return num1 + multiply2(num1, num2 - 1);
}

How to make recursive function that multiply two positive numbers without using mutiplication operator

Its so simple. see this code:

int multiplication(int a, int b){
if(b==1|| b==0)
return a;
else
return a+multiplication(a,--b);
}

I have not tested it. Just share it for idea.

Recursive method for multiplying arraylist elements

First you will have to create the logic for the multiply button, something like:

  JButton b2 = new JButton("Multiply!");
b2.addActionListener(e -> {
try {
num = Integer.parseInt(tf.getText());
int result = multiple_recursive(numbers, 0);
// choose label to set the value.


}catch (Exception x){
// solve exception
}
});

then defined the method to multiply recursively:

public static int multiple_recursive(List<Integer> numbers, int count){
if(numbers.size() == count){
return 1;
}
return numbers.get(count) * multiple_recursive(numbers, count + 1);
}

So the idea is you pass the list and a count variable. This count will tell the current position of the list of numbers. For each recursive call you take the current element from the list numbers.get(count) and call again recursively but adding one to the current count i.e., multiple_recursive(numbers, count + 1);. The recursive calls should stop as soon as the current count is the same as the list size, we return 1, because of the multiplicative identity property of that value.

For a list with the element {5, 2, 10}. The iterations will be like:

  1. numbers.size() == count False
  2. return numbers.get(0) * multiple_recursive(numbers, 1);
  3. return 5 * (multiple_recursive(numbers, 1));
  4. return 5 * (numbers.get(1) * multiple_recursive(numbers, 2));
  5. return 5 * (2 * multiple_recursive(numbers, 2));
  6. return 5 * (2 * (numbers.get(2) * multiple_recursive(numbers, 3)));
  7. return 5 * (2 * (10 * multiple_recursive(numbers, 3)));
  8. since numbers.size() == count is True then
  9. return 5 * (2 * (10 * 1));
  10. return 50


Related Topics



Leave a reply



Submit