Question: Compute f(n) such that (3 cases, depending on the value of n): f(1) = 1 f(n) = n + f(n-1) for n>1, n is even

Compute f(n) such that (3 cases, depending on the value of n): f(1) = 1 f(n) = n + f(n-1) for n>1, n is even f(n) = n * f(n-1) for n>1, n is odd

So the method you are writing is named f() and will take in an integer value. Then it will return an integer result. Because the method is named f(), you can call it in your code just like that. So the code in this case will look a lot like the right side of the formula in each case. I recommend using if statements to determine which case you are in and then returning the value based on the formula for that case (a recursive call being made in the 2nd and 3rd cases)

Compute f(n) such that (3 cases, depending on the value of n):

Function.java + New 1 public class Function { 2 public static int f(int n) { 3 //TODO: complete this method 4 } 5 6 public static void main(String[] args) { //this main method is not used by Mimir's tests //you can write your own if you want to test the f() method directly //by running it in your IDE 10 } 11 }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!