MOTIVECODEX
MOTIVECODEX

Reputation: 2752

Recursive method - Java

Addition information:

Chip doesn't support multiplication, only addition. I should work around this problem by creating a recursive method, mult(), that performs multiplication of x and y by adding x to itself y times. Its arguments are x and y and its return value is the product of x and y. I should then write the method and a main() to call it.

It's pure logical thinking, but I get lost every time I try to think what to do.

I am stuck at the math part.. What I have, that doesn't work and I know the math is wrong, but I am not good at this:

public static void mult(int x, int y) {
    x = 0;
    y = 0;
    if (y > 0) {
        for (int i = 0; i < y; i++) {
            x = x * (x * y);
            return mult(x, y);
        }
    }
}

Upvotes: 0

Views: 16030

Answers (6)

MOTIVECODEX
MOTIVECODEX

Reputation: 2752

public static int mult(int x, int y) {
        if (y == 0) {
            return 0;
        }
        if (y > 0) {
            return x + mult(x, y - 1);
        } else {
            return -x + mult(x, y + 1);
        }
    }

this was the solution by the way

Upvotes: 1

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 235984

All you need to remember is that a multiplication is a repeated addition (assuming that both operands are >= 0), so we have:

  • The base case is when y is zero
  • If y is not zero, then add x one more time, and subtract 1 from y

Notice that as long as y is positive, it'll eventually have a value of zero. So basically we keep adding x a total number of y times; this is what I mean:

public static int mult(int x, int y) {
    if (y == 0)
        return 0;
    return x + mult(x, y-1);
}

The same code can be written in a tail-recursive style, too - meaning: there's nothing to do after the recursive call returns, and this is important for certain languages that support a so-called tail-call optimization:

public static int mult(int x, int y, int accumulator) {
    if (y == 0)
        return accumulator;
    return mult(x, y-1, x + accumulator);
}

The above will get called as follows, noticing that the last parameter is always initialized in zero:

mult(10, 5, 0)
=> 50

Upvotes: 2

user2336315
user2336315

Reputation: 16067

One possibility is to use an accumulator which will store the current value of the multiplication. I replace missing statements by ??? :

public static void main(String []args){
        System.out.println(mult(2,5));
 }
    public static int mult(int x, int y) {
      if(???) return ???;
      else return multAcc(???,???,???);
    }

    private static int multAcc(int x, int y, int acc){
        if(???) return ???;
        else return multAcc(???, ???, ???);
    }

Upvotes: 3

Display Name
Display Name

Reputation: 8128

Java has no TCO by design, so using recursion for linear (not tree-like) processes is very bad idea. Especially for such task, which will most likely become a bottleneck in your program. Use loop instead.

Oh, it must be recursive anyway? Looks like a homework task. Do it yourself then.

Upvotes: 2

subsub
subsub

Reputation: 1857

... by adding x to itself y times.

You could actually do that, instead of multiplying. Oh, and maybe if you don't set both x and y to zero, you would have something to add ;-)

One last thing: If you want a recursive solution, you don't need the for-loop.

Upvotes: 2

duffymo
duffymo

Reputation: 308733

When I hear "recursion", I expect to see two things:

  1. A function calling itself with modified arguments each time.
  2. A stopping condition right at the top that tells the function when to stop, avoiding an infinite stack.

So where are yours? Start with writing those down in words before you write code.

Upvotes: 10

Related Questions