Question

In: Computer Science

3. The Hofstadter Conway sequence is defined by a(1)=a(2)=1 and (for n>2 by) a(n)=a(a(n-1))+a(n-a(n-1)). Write a...

3. The Hofstadter Conway sequence is defined by a(1)=a(2)=1 and (for n>2 by) a(n)=a(a(n-1))+a(n-a(n-1)). Write a function to quickly compute this sequence.
>>> [hc(i) for i in range(1,20)]
[1, 1, 2, 2, 3, 4, 4, 4, 5, 6, 7, 7, 8, 8, 8, 8, 9, 10, 11]

Solutions

Expert Solution

The programming language used is Python 3.8.

The function name is hc() which takes in the parameter value n.

The code snippet of the function is:

def hc(n): 
    List = [0, 1, 1] 
    if n<= 0:
      return 0
    if n==1:
      List.pop(0)
      List.pop(1)
      print(List)
    elif n==2:
      List.pop(0)
      print(List)
    else:
      for i in range(3,n+1): 
        List.append( List[List[i - 1]] + List[i - List[i - 1]]) 
      List.pop(0)
      print(List)

Initially a list List[] is created with values 0,1,1 in it.

And based on the value of n passed to it the contents in the list is printed.

If the value of n is less than or equal to 0 no output will be printed.

List.pop() function was used to remove the value 0 from the list List[] as this value was only added for calculations.

The screenshot of the code and the output generated is attached for your reference:

Hope it helps!!!


Related Solutions

2. The Fibonacci sequence is defined as f(n) = f(n - 1) + f(n - 2)...
2. The Fibonacci sequence is defined as f(n) = f(n - 1) + f(n - 2) with f(0) = 0 and f(1) = 1. Find f(54) by a program or maually. Note that this number must be positive and f(53) = 53.......73 (starting with 53 and ending with 73). I must admit that my three machines including a desktop are unable to find f(54) and they quit during computation. The answer is f(54) = 86267571272 */ The Java code: public...
Write the first four terms of the sequence defined by the recursive formula a1 = 2, an = an − 1 + n.
Write the first four terms of the sequence defined by the recursive formula a1 = 2, an = an − 1 + n.
#2 The Jacobsthal sequence is defined by J(1)=J(2)=1 and J(n)=J(n-1)+2J(n-2). Use recursion to write a function...
#2 The Jacobsthal sequence is defined by J(1)=J(2)=1 and J(n)=J(n-1)+2J(n-2). Use recursion to write a function that takes in a positive integer n and returns the nth Jacobsthal number. >>> J(8) 85 >>> J(9) 171 #3 Use recursion to write a function that takes in a positive integer n and returns all n digit numbers containing only odd digits. >>> f(1) [1, 3, 5, 7, 9] >>> f(2) [11, 13, 15, 17, 19, 31, 33, 35, 37, 39, 51, 53,...
For any sequence, S[1…n], of length n, a basement is defined as a contiguous subsequence of...
For any sequence, S[1…n], of length n, a basement is defined as a contiguous subsequence of S, which we denote by S[ i, i + 1, ..., j – 1, j ], with 1 £ i < i + 1 <   j – 1 < j £ n, satisfying the following conditions: S[ i ] > S[ i + 1] S[ j – 1] < S[ j ] 1 £ i < i + 1 <   j – 1 <...
Write a combinatorial proof for 1 n + 2 ( n − 1 ) + 3...
Write a combinatorial proof for 1 n + 2 ( n − 1 ) + 3 ( n − 2 ) + ⋯ + ( n − 1 ) 2 + n 1 = ( n + 2 choose 3 ) .
Given a sequence x(n) for 0 ≤ n ≤ 3, where x(0)=4, x(1)=3, x(2)=2, and x(3)=1,...
Given a sequence x(n) for 0 ≤ n ≤ 3, where x(0)=4, x(1)=3, x(2)=2, and x(3)=1, evaluate your DFT X(k)
Write the first four terms of the sequence defined by the explicit formula an = 10n + 3.
Write the first four terms of the sequence defined by the explicit formula an = 10n + 3.
Let {an} be a sequence defined recursively by a1 = 1 and an+1 = 2√ 1...
Let {an} be a sequence defined recursively by a1 = 1 and an+1 = 2√ 1 + an where n ∈ N (b) Does {an} converge or diverge? Justify your answer, making sure to cite appropriate hypotheses/theorem(s) used. Hint : Try BMCT [WHY?]. (c) (Challenge) If {an} converges then find its limit. Make sure to fully justify your answer.
Write and upload a MATLAB script to do the following. Compute the sequence S(n+1) = (2...
Write and upload a MATLAB script to do the following. Compute the sequence S(n+1) = (2 – K) S(n) – S(n-1) ;   Assume S(1) = 0, S(2) = 1; (a)    Case 1, Assume K = 1 (b)    Case 2, Assume K = 2 (c)     Case 3, Assume K = 4 Plot all of these on the same plot, for N = 1 to 20
Write a recursive formula for the arithmetic sequence 0, −1/2 , −1, −3/2 , … , and then find the 31st term.
Write a recursive formula for the arithmetic sequence 0, −1/2 , −1, −3/2 , … , and then find the 31st term.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT