122k views
1 vote
I really need help with CSC 137 Java ASAP!!!! but it's due date: Apr 21, 2023 at 12:00 PM

Chapter 12: Recursion
EX 12.4 The Hofstadter Q sequence is defined by Q(n) = Q(n – Q (n – 1)) + Q(n – Q(n – 2)). The first two values in the sequence are given as 1, 1. That is, Q(1) = 1, Q(2) = 1. Write a recursive method that accepts a positive integer n and finds the nth Hofstadter number.


EX 12.6 Write a recursive method that returns the value of N! (N factorial) using the definition given in this chapter. Explain why you would not normally use recursion to solve this problem.

2 Answers

5 votes
Yes so what you do is describe the answer in the best of your ability I don’t think they will count off for that just do your best but I will help you

Solve for Q for 12:33 -1
User Jaekyung
by
7.6k points
5 votes

Answer:

To solve Exercise 12.4, you can write a recursive method that accepts a positive integer n and finds the nth Hofstadter number. The recursive method can be defined as follows:

```

def hofstadter_number(n):

if n == 1 or n == 2:

return 1

else:

return hofstadter_number(n - hofstadter_number(n - 1)) + hofstadter_number(n - hofstadter_number(n - 2))

```

To solve Exercise 12.6, you can write a recursive method that returns the value of N! (N factorial) using the definition given in this chapter. The recursive method can be defined as follows:

```

def factorial(n):

if n == 0:

return 1

else:

return n * factorial(n - 1)

```

Normally, you would not use recursion to solve this problem because there is a much simpler and more efficient iterative solution. The iterative solution can be defined as follows:

```

def factorial(n):

result = 1

for i in range(1, n+1):

result *= i

return result

```

don't forget the brilliant mark

User Leen
by
8.6k points