# Recursive Functions Project Help Using Java

This is a Definition and writing of Recursive Functions Project Help completed by a java homework help specialist from our platform. It shows various steps in defining recursive functions given the type signature. It also shows how to test the respective functions for correctness. ## Defining Various Sequences Given The Type Signature

• Deﬁne factorial with the following type signature:
public static double factorial ( double n)
It’s speciﬁcation is given above.
• Deﬁne ﬁbonacci with the following type signature:
public static double fibonacci( double n)
It must follow this speciﬁcation:
F(0) = 0
F(1) = 1
F(n) = F(n−1) + F(n−2)
##### Defining Unnamed Sequences Given The Type Signatures
• Deﬁne an unnamed sequence with the following type signature:
public static double sequence1 ( double n)
It must follow this speciﬁcation:
F(0) = 2
F(1) = 2 F(n) = 2∗F(n−1)−1
• Deﬁne an unnamed sequence with the following type signature:
public static double sequence2 ( double n)
It must follow this speciﬁcation:
F(0) = 5
F(1) = 5
F(n) = 3∗F(n−1)
##### Defining Named Sequences Given Type Signatures
• Deﬁne Hofstadter’s Q-sequence with the following type signature:
public static double hofsQ( double n)
It must follow this speciﬁcation:
F(0) = 1
F(1) = 1
F(2) = 1
F(n) = F(n−F(n−1)) + F(n−F(n−2))
• Deﬁne the tetration operation with the following type signature where a is being taken to the tetration power of n:
public static double tetration( double a , double n)
It must follow this speciﬁcation:
F(a,0) = 1
F(a,n) = aF(a,n−1)
Remember that ˆ in Java does not perform exponentiation but rather XOR. Use Math.pow to perform exponentiation.
• Deﬁne a mathematical sequence of your choice. The only speciﬁcation that it must follow is that it must return a double and take at least one double as an argument.
##### Solution
public class Main {
public static void main(String[] args) {
System.out.println("factorial(2) = " + factorial(2));
System.out.println("factorial(10) = " + factorial(10));
System.out.println("fibonacci(2) = " + fibonacci(2));
System.out.println("fibonacci(10) = " + fibonacci(10));
System.out.println("sequence1(2) = " + sequence1(2));
System.out.println("sequence1(10) = " + sequence1(10));
System.out.println("sequence2(2) = " + sequence2(2));
System.out.println("sequence2(10) = " + sequence2(10));
System.out.println("hofsQ(2) = " + hofsQ(2));
System.out.println("hofsQ(10) = " + hofsQ(10));
System.out.println("tetration(2, 3) = " + tetration(2, 3));
System.out.println("tetration(3, 2) = " + tetration(3, 2));
System.out.println("tetration(3, 3) = " + tetration(3, 3));
}
public static double factorial(double n) {
if (n >= 1)
return n * factorial(n - 1);
else
return 1;
}
public static double fibonacci(double n) {
if (n == 0) {
return 0;
}
if (n == 1 || n == 2) {
return 1;
}
return fibonacci(n - 2) + fibonacci(n - 1);
}
public static double sequence1(double n) {
if (n == 0 || n == 1)
return 2;
return 2 * sequence1(n - 1) - 1;
}
public static double sequence2(double n) {
if (n == 0 || n == 1)
return 5;
return 3 * sequence2(n - 1);
}
public static double hofsQ(double n) {
if (n == 0 || n == 1 || n == 2)
return 1;
return hofsQ(n - hofsQ(n - 1)) + hofsQ(n - hofsQ(n - 2));
}
public static double tetration(double a, double n) {
if (n == 0)
return 1;
return Math.pow(a, tetration(a, n - 1));
}
// your chosen math sequence goes here
}
Testing the Functions
Here is some output from these functions that you can use to make sure your functions are correct:
fibonacci(2) = 1.0
fibonacci(10) = 55.0
sequence1(2) = 3.0
sequence1(10) = 513.0
sequence2(2) = 15.0
sequence2(10) = 98415.0
hofsQ(2) = 1.0
hofsQ(10) = 6.0
tetration(2,3) = 16.0
tetration(3,2) = 27.0
tetration(3,3) = 7.625597484987E12 Related Topics