Answer to Question #294078 in Python for Hannah Sox

Question #294078
def total_number_sequence(m, n):
    if m < n:
        return 0

    if n == 0:
        return 1

    res = (total_number_sequence(m - 1, n) +
           total_number_sequence(m // 2, n - 1))
    return res


if __name__ == '__main__':
    m = 10
    n = 100
    print('Total number of possible sequences:', total_number_sequence(m, n))

PROBLEM: 3N+1 sequence: determine which value(s) of N generate the longest sequence for the famous 3N+1 sequence. Refer to 8.5 in your runestone text for more details. Output the value(s) of N that generate the longest sequence. 

C version: use 100 as the value of N


Consider the following to help you better understand how to solve this problem. 

OUTPUT FORMAT:

C version - a single number



What is wrong with my code?


1
Expert's answer
2022-02-05T14:56:28-0500

The first condition is always true, since m < n. Therefore, the function will always return 0.

In addition, as I understand it, you are trying to use recursion into lines with a variable res. Well, you need to understand that calling the first function recursively will not make it possible to call the second one, since the function will constantly start doing everything from the beginning.


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS