Question

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 no matter what the starting number is.

Your program should print out all numbers in the sequence down to 1. At the end, display number of iterations it took to arrive at 1, and average of all values in sequence. For prettier output, insert a line break after printing every five numbers, align the numbers in columns and format the average value to two decimal places.

After printing a Collatz sequence, your program should ask the user if they want to print out another sequence. The loop will continue until user decides to quit.

Review the sample run below to clearly understand all requirements.

Enter a positive number, or zero to quit: 57

  172   86   43  130
   65  196   98   49  148
   74   37  112   56   28
   14    7   22   11   34
   17   52   26   13   40
   20   10    5   16    8
    4    2    1

It took 32 iterations to arrive at 1. The average value is 51.66

Enter the next positive number, or zero to quit: 11

    11   34   17   52   26
   13   40   20   10    5
   16    8    4    2    1

It took 14 iterations to arrive at 1.

The average value is 18.50

Enter the next positive number, or zero to quit: 0

Have a nice day!

Your submission must consist of following tasks: Task 1 Draw a flowchart that presents the steps of the algorithm required to perform the task specified. You can draw the flowcharts with a pen/pencil on a piece of paper and scan it for submission, as long as the handwriting is clear and legible. However, it is strongly recommended to draw flowcharts using a drawing software. Here are links to some free drawing tools

Draw.io Lucidchart

yEd Graph Editor

Pencil Project

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#CODE:

while(True):
n = int(input("Enter a positive number, or zero to quit: "))
if n == 0:
print("Have a nice day!")
break
else :
res = [n]
c = 0
while(True):
if n==1 :
print("It tooks %d iterations to arrive at 1"%c)
break
if n%2!=0 :
n = (n*3)+1
elif n%2==0 :
n = n//2
res.append(n)
c+=1
print("The Average Value is ",sum(res)/c)
print(res)
print()

#OUTPUT:

#FLOWCHART:

Flow chart can be written by individual . And doesn't included line break and quitting loop by entering '0' in flowchart.

And array is taken to print Collatz sequence.

Feel free to ask any doubts regarding my solution and reply me in comments.

Thank You

Add a comment
Know the answer?
Add Answer to:
Please answere using python language codes. Write a program to print out Collatz sequence for a...
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
  • 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...

  • 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...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list or string or loops (while and for) or if,elif,else : You are given a sequence of n non-zero integers a1,a2,…,an. Determine if the given sequence contains a pair of adjacent elements of the same sign (both negative or both positive). Two elements are called adjacent if they stand next to each other in the sequence. Input The first line contains an integer n...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list or string or loops (while and for) or if,elif,else : i have 15 min to submit the solution please help me   You are given a sequence of n non-zero integers a1,a2,…,an. Determine if the given sequence contains a pair of adjacent elements of the same sign (both negative or both positive). Two elements are called adjacent if they stand next to each other...

  • Write a code using loop and flag function in python jupitior notebook: Ask the user for...

    Write a code using loop and flag function in python jupitior notebook: Ask the user for the number of items they bought. Also ask the price of each item they bought and quantity purchased. Display the total amount they owe. Ask the user for a number n. Calculate 1+2+3+….+n. Ask the user for a number n. Calculate 1*2*3*…*n. Ask the user for number of rows (r) and columns (c). Print * for r rows and c columns. Ask the user...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list or string or loops (while and for) or if,elif,else : Alex got a sequence of n integers a1,a2,…,an as a birthday present. Alex doesn't like negative numbers, so he decided to erase the minus signs from all negative numbers. As a result, he got a sequence of non-negative numbers. Print the resulting sequence. For example, if the sequence is  1, 5, -3, 4,...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list : You are given a non-decreasing sequence of n positive integers a1,a2,…,an. Print the number of distinct values in the sequence. For example, if the sequence is 1,2,2,2,3,4,4,5,7,10, the answer is 6 since distinct values are 1,2,3,4,5,7,10. Input The first line contains a positive integer n (1≤n≤1000) — the length of the sequence. The second line contains n space-separated positive integers a1,a2,…,an (1≤ai≤1000)...

  • Step 1. Write a program in assembly language (using macros) to print out the following messages...

    Step 1. Write a program in assembly language (using macros) to print out the following messages on the screen [20 marks]: Hello, programmers! Welcome to the world of, Linux assembly programming! Step 2. Write a C function to passed two numbers to the function and calculate the sum of all the numbers between them (both inclusive). [50 marks] a. Make a version with for loop b. Make a version with while loop c. Make a version with do.. while loop...

  • Task 1 Write a Python program that reads in any number of birth years until the...

    Task 1 Write a Python program that reads in any number of birth years until the number zero is entered. The program will then print out the average age and how old the youngest and oldest, respectively. For this task, you can start from the pseudo-code (separately) you received to get started. Example of program execution (user entries in numbers (year)): Enter year of birth. To finish, enter the number 0. Year: 1998 Year: 1932 Year: 1887 Error: Unreasonable year....

  • Write a Python program (using a nested loop) to print out the following pattern (there is...

    Write a Python program (using a nested loop) to print out the following pattern (there is a space between the numbers next to each other). 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1

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