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)
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started