Question

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 the sequence are 16, 8, 4, 2, 1. If we start at 6, the numbers in the sequence are 6, 3, 10, 5, 16, 8, 4, 2, 1.

For this problem, you have to write a program that does the following:

1. Prompt the user for a positive integer. You must ensure the number is positive. However if the user enters the number 0, you should exit the program instead of prompting again.

2. After receiving a valid input, you should iterate through the sequence, keeping track of the number of steps taken until you reach 1 (you may assume the sequence will always converge to 1 for the purpose of this problem). Print out the number of steps taken to reach convergence. Also print out both the largest and second largest values encountered along with their respective step numbers.

1 Please enter a positive integer: -2 2 Please enter a positive integer: 8 3 It took 3 steps for the sequence to reach 1. 4 T

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

# If you have a query/issue with respect to the answer, please drop a comment. I will surely try to address your query ASAP and resolve the issue

# # Please consider providing a thumbs up to this question if it helps you. by doing that, you will help other students who are facing a similar issue.

this code is meant for python3+

#---------------------INPUT/OUTPUT---------------------------

please enter a positive integer: -2 please enter a positive integer: 8 It took 3 steps for the sequence to reach 1. The large#----------------------------------------------------------------------

#initalizing num

num=-1

#loops until a valid number is entered

while(num<0):

    num=int(input("please enter a positive integer: "))

#to store number of steps

steps=0

#to store steps to get to the largeset num

step_largest=0

#initializing to input number

largest_num=num

#deafult value

second_largest=1

#deafult step

step_second_largest=0

#loops until num does not convergest to 1 given that num was greater than 0 to begin with

while(num!=1 and num>0):

    #incremtn step

    steps=steps+1

    #follow the formula

    if(num%2==1):

        num=num*3+1

    elif(num%2==0):

        num=num//2

    #to compare current num to largest num

    if(largest_num<num):

        largest_num=num

        step_largest=steps

    

    if(second_largest<num and num<largest_num):

        second_largest=num

        step_second_largest=steps

#checks if converged

if(num==1):

    #prints if it took steps

    if(steps!=0):

        print("It took",steps,"steps for the sequence to reach 1.")

    print("The largest number in the sequence was",largest_num,"(step =",step_largest,")")

    print("The second largest number in the sequence was",second_largest,"(step =",step_second_largest,")")



#----------------------------------------------------------------------------

i #initalizing num num=-1 #loops until a valid number is entered while(num<0): num=int(input(please enter a positive integer

largest_num=num step_largest=steps if(second largest<num and num_largest num): second largest=num step_second_largest=steps ##-----

Add a comment
Know the answer?
Add Answer to:
IN PYTHON 1.Choose a positive integer 2. To get the next number in the sequence we...
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
  • 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 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)...

  • A mathematical conjecture states that if we start with any positive number we can get to the value 1 by repeating 2 poss...

    A mathematical conjecture states that if we start with any positive number we can get to the value 1 by repeating 2 possible steps depending on the value of the current number. If the current value is even we divide the number by 2 and reapply the steps again (number / 2). If the number is odd we multiply the current number by 3 and add 1 to it and reply the previous steps again (number * 3 + 1)....

  • Python Script format please! 1. Write a script that takes in three integer numbers from the...

    Python Script format please! 1. Write a script that takes in three integer numbers from the user, calculates, and displays the sum, average, product, smallest, and largest of the numbers input. Important things to note: a. You cannot use the min() or max() functions, you must provide the logic yourself b. The calculated average must be displayed as an integer value (ex. If the sum of the three values is 7, the average displayed should be 2, not 2.3333). Example:...

  • Given an array of integers representing heights of consecutive steps, we say that a tumble is...

    Given an array of integers representing heights of consecutive steps, we say that a tumble is a sequence of strictly decreasing numbers and the height of the tumble is the difference between the height of the top step and the bottom step. For example, if there are consecutive steps with heights 16, 9, 4 then they form a tumble of height 12 (= 16-4). With consecutive steps of heights, 0, 4, 12, 4, 5, 7, 2, 1 there is a...

  • The Collatz conjecture concerns what happens when we take any positive integer n and apply the...

    The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorithm: if n is even 3 xn+1, if n is odd The conjecture states that when this algorithm is continually applied, all positive integers will eventually reach 1. For example, if n- 35, the sequence 1S 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4,2,'1 Write a C program using the fork) system call that generates this sequence in the...

  • A perfect number is a positive integer that is equal to the sum of its (proper)...

    A perfect number is a positive integer that is equal to the sum of its (proper) positive divisors, including 1 but excluding itself. A divisor of a number is one which divides the number evenly (i.e., without a remainder). For example, consider number 6. Its divisors are 1, 2, 3, and 6. Since we do not include number itself, we only have 1, 2, and 3. Because the sum of these divisors of 6 is 6, i.e., 1 + 2...

  • Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5,...

    Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1) The 3 is found by adding the two numbers before it (1+2), And the 5 is (2+3), and so on!         Example: the next number in the sequence above is 21+34 = 55 Source:...

  • Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns:...

    Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with: 4 for Visa cards 5 for MasterCard credit cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card...

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

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