Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(1) (20 points) Consider the function F:N + N given by 3n+1, if n is odd, n/2, if n is even F(n)= { Write Python

image text in transcribed

(1) (20 points) Consider the function F:N + N given by 3n+1, if n is odd, n/2, if n is even F(n)= { Write Python code to accomplish the following: (i) Write a Python function named F3n1 which takes a positive integer n, and computes and returns the integer value F(n) as defined above (use the integer division operator For F3n1 (10) should return 5 and F3n1(5) should return 16. 17 instead of the ordinary division operator). If , (ii) Write a Python function named sequence which takes a positive integer n and returns a list containing the sequence n, F(n), F(F(n)), F(F(F(n))),... up to and including the first occurrence of a 1. For example, the command print sequence(10) should give the output (10, 5, 16, 8, 4, 2, 1] You should use the function F3n1 you wrote for Part (i). (iii) Below those two functions, insert the following code, which I will use to test your functions. N = int(input("Enter a positive integer n:")) seq = sequence (N) print("The resulting sequence is: {0}", format (seq)) print ("It has length {0}", format (len(seq)))

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Introduce and define metals and nonmetals and explain with examples

Answered: 1 week ago

Question

What is IUPAC system? Name organic compounds using IUPAC system.

Answered: 1 week ago

Question

What happens when carbonate and hydrogen react with carbonate?

Answered: 1 week ago

Question

1. How did you go about making your selection?

Answered: 1 week ago