Question
CODE NEEDS TO BE IN PYTHON.
OLA 5: Collatz Sequence Function 12 17 34 Due: Fri Oct 19, 2018 by 11:59 PM-may be tumed in until Oct 26 by 1159 PM with reduced points (per Open Lab- Project guidance found in the course syllabus) Assignment id: ola5 Assignment type: Project Required Files: ola5.py, myout.log Lab description: In this you will explore the Collatz mathematical sequence. This sequence eventually converges to the value 1, regardless of the initial input Requirements: 1. Write a function named collatz0 that has one parameter named number. If number is . even, then collatz0) should print number //2 and retum this value (integer divison). . odd, then collatz) should print and retum 3 number + 1 2. Write a program that lets the user type in an integer, and keeps calling the collatz() function on subsequent results until the function returns the value 1 Hints: . You must use a loop that exits the program only when the returned value is 1 . Remember to convert the returm value from input() to an integer with the int() function otherwise, it will be a string value. An integer number is even if number % 2 1+0, and its odd if number % 2 : . 1. A sample output of the program should look something like this: Enter number: 3 10 16
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

def collatz(num):   
if ( num %2 ==0 ) :#if number is even below calculation proceed
Number=num//2 #num value in with divied by two and assign new number variable
print(Number);#print value
if Number!=1:#if Number is not equal to 1
collatz(Number);#function are called
else:
return (Number)#else return number
elif (num % 2 == 1) : #else number is odd below calculation proceed
Number=3*num+1 #num value in with add one and multy by three and assign new number variable
print (Number)#print value
collatz(Number)#function are called
return Number;#return number


#user input and validation while loop
while True:
try:
num = int(input("Enter number:")) #user input and store value in num  
except ValueError:
print("Enter a valid integer")
continue
else:
if num < 1:#condition check
print("enter a valid positive integer")
continue
break

collatz(num)

Output:

Thank you if you have any query regarding above answer please ask me in comment box.

if you like my work appreciate with thums up.

Thank You.

Add a comment
Know the answer?
Add Answer to:
CODE NEEDS TO BE IN PYTHON. OLA 5: Collatz Sequence Function 12 17 34 Due: Fri...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ The Collatz Conjecture is a conjecture in mathematics that concerns a sequence sometimes known as...

    c++ The Collatz Conjecture is a conjecture in mathematics that concerns a sequence sometimes known as hailstone numbers. Given any positive integer n, the following term, n+1 is calculated as follows: If n is even, then n+1 is defined as n/2. If n is odd, then n+1 is defined as 3n + 1 The Collatz Conjecture states that, for any value of n, the sequence will always reach 1. Once the pattern reaches 1, it repeats indefinitely (3 * 1...

  • Language is Python 3, please include comments Question 1 Write function collatz, that takes a positive...

    Language is Python 3, please include comments Question 1 Write function collatz, that takes a positive integer x as input and prints the Collatz sequence starting at x. A Collatz sequence is obtained by repeatedly applying this rule to the previous number x in the sequence: 3/2 if x is even 3.2 +1 if x is odd. Your function should stop when the sequence gets to number 1. Note: It is an open question whether the Collatz sequence of every...

  • Please answere using python language codes. Write a program to print out Collatz sequence for a...

    Please answere using python language codes. Write a program to print out Collatz sequence for a user-supplied number. Prompt the user for a positive integer which will become the first number in the sequence. Next number in the sequence is derived as follows: If previous number is odd, the next number is 3 times the previous, plus 1. If previous number is even, the next number is half of the previous According to Collatz proposition, the sequence ultimately reaches 1...

  • In python: Not sure how to do a Collatz function. Remember the Collatz function? If x...

    In python: Not sure how to do a Collatz function. Remember the Collatz function? If x is odd, then collatz(x) = 3 ⋅ x + 1 but if x is even then collatz(x) = x/2. (remember to use // to divide by 2) Start by writing a Collatz() function that returns the value for a given input. Test this function for all numbers from 1 to 10. Next write a Collatz2() method. This method should take in a single number...

  • IN PYTHON 1.Choose a positive integer 2. To get the next number in the sequence we...

    IN PYTHON 1.Choose a positive integer 2. To get the next number in the sequence we do the following: If the integer is odd, we multiply by 3 and add 1. If the integer is even, we divide by 2. It is hypothesized that the above sequence will always converge to the value of 1, regardless of any valid initial choice. This hypothesis is known as the Collatz Conjecture. For example, if we start at 5, the numbers generated by...

  • In Python 3 - Write a program that reads a sequence of integer inputs from the...

    In Python 3 - Write a program that reads a sequence of integer inputs from the user. When the user is finished entering the integers, they will enter a 'q'. There is no need to check if the entry is a valid integer. All integers in the test data (input) will be between -255 and 255. The program should then print: The smallest and largest of the inputs. The number of even and odd inputs (0 should be considered even)...

  • # Write PYTHON programs that read a sequence of integer inputs and print # a.  The...

    # Write PYTHON programs that read a sequence of integer inputs and print # a.  The smallest and largest of the inputs. # b.  The number of even and odd inputs. # c.  Cumulative totals. For example, if the input is 1 7 2 9, the program should print # 1 8 10 19. # d.  All adjacent duplicates. For example, if the input is 1 3 3 4 5 5 6 6 6 2, the # program should print...

  • use python please #The Joyner Conjecture is a not-at-all famous mathematical #series inspired by the Collatz...

    use python please #The Joyner Conjecture is a not-at-all famous mathematical #series inspired by the Collatz Conjecture for use in this #class. # #The Joyner Conjecture proceeds as follows: # #Start with any number. If the number is divisible by 3, #divide it by 3. Otherwise, add 2 to the number. Eventually, #no matter what number you begin with, this series will run #into 1 or 2. If it runs into 1, it will repeat 1-3 forever. #If it runs...

  • Within the 1900 folder on your desktop, create a new folder namned Lab4HwLastnameFirstnane (5 pts) Problem...

    Within the 1900 folder on your desktop, create a new folder namned Lab4HwLastnameFirstnane (5 pts) Problem 1 Consider the sequence of integers defined as follows: • The first term is any positive integer. . For each term n in the sequence, the next tcrin is computed like this: If n is even the next term is n/2. If n is odd, the next term is 3n +1. • Stop once the sequence reaches 1. Here are a few examples of...

  • python programming X Canvas 4 → XCO Question 26 10 pts (SHORT ANSWER) Define a function...

    python programming X Canvas 4 → XCO Question 26 10 pts (SHORT ANSWER) Define a function named find_even_odd that has 1 parameter: num. When the function is called, it should use an if/else statement to determine whether the variable num is an even or odd number and display appropriate message as follows: • If the value in num is an even number, the progr will output the message: "XX is an EVEN number", where XX is the value in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT