David
David

Reputation: 141

Homework help yet again please. Recursively multiply Evens?

Below in quotes is my actual assignment and below that is my current code. Can someone point me in the right direction.

Write a recursive method called multiplyEvens that returns the product of the first n even integers. For example, multiplyEvens(1) returns 2, and multiplyEvens(4) returns 384 (because 2 * 4 * 6 * 8 = 384). Throw an IllegalArgumentException if passed a value less then 1.

  private static int multiplyEvens(int n)
  {  
      if(n%2 == 0)
      {           
          System.out.println(n*n);
          return multiplyEvens(n*n);
                      // I'm lost
      }
      System.out.println();
      return n; 
  }

Upvotes: 1

Views: 4994

Answers (6)

mprivat
mprivat

Reputation: 21902

Here you go:

public static void main(String[] args) {
    System.out.println(multiplyEvens(4));
}

private static int multiplyEvens(int n) {
    if(n < 1) throw new IllegalArgumentException("Value less than 1 not supported");
    else if(n == 1) return 2;
    else return multiplyEvens(n-1) * (n*2);
}

Here the basics for recursive algorithms. You have to assume that you have the result calculated for n-1. Then you return the results for n-1 adjusted for n. (That is, you multiply by the nth even number, n*2). The recursive method call does the rest, it in turns assumes it has already computed the value for n-2, etc... until n==0, for which the result is 1

Upvotes: 0

rickz
rickz

Reputation: 4474

Multiplying n evens is the same as
2^n * n!
All you have to do is google factorial recursion java

Upvotes: 0

LeleDumbo
LeleDumbo

Reputation: 9340

You should do it backwards. i.e. on each recursive call, return (2 * n) * recursive call with decreased n. The base case is when n = 1, which the function simply returns 2 without multiplying it with another recursive call (to fulfill that IllegalArgumentException that you need to throw for n < 1).

Upvotes: 1

John3136
John3136

Reputation: 29266

Walk it through on paper.

start with n = 1

1%2 = 1 so you don't do into your loop, and return value = 1 (wrong)

try n = 2

2%2 = 0, so you go into your loop and call multiplyEvens(2*2)
4%2 = 0, so you go into your loop and call multiplyEvens(4*4)
16%2 = 0 ...

By now you should be starting to get a clue of at least one problem with your approach...

Upvotes: 5

Makoto
Makoto

Reputation: 106470

Anything that can be done recursively can be done iteratively. Think of it like this: When you're writing a for-loop, there's an extra value that you need to have before you determine the parity (even/odd) value of whatever value you're working on, up to n.

Since this is homework, here's the broad stroke:

  • Your recursive condition may take two parameters instead of one.
  • You should multiply values together until some value i <=n.

You're mostly there.

Upvotes: 3

Jason S
Jason S

Reputation: 189826

Try recursing on n-1 rather than on n*n, and see if you can figure out how the value of multiplyEvens(n) relates to multiplyEvens(n-1). Maybe that will give you a start in the right direction.

Upvotes: 5

Related Questions