Question: Using Python code in Jupiter notebook can someone please help me answer this Problem? could you please type your code as a response please so

Using Python code in Jupiter notebook can someone please help me answer this Problem? could you please type your code as a response please so it's easier to read? Thank you.

Using Python code in Jupiter notebook can someone please help me answer

Here we will investigate sequences of natural numbers often called the 'hailstone' numbers. The sequence is generated by following a few rules: - If the current number is even, divide by 2. - If the current number is odd, multiply by 3 and add 1. - The sequence stops if it reaches the number 1. For example, starting with 6 , the resulting sequence would be [6,3,10,5,16,8,4,2,1] The Collatz conjecture says that from any starting value this sequence will always reach 1 , rather than falling into a repeating pattern. 1. Write a generator called that yields the sequence given a starting number. Remember that a generator is a specific Python construct, which uses the yield keyword. 2. Print the result starting at 6 , and check that it is the same as the example above. 3. Calculate the length of the sequence for all starting numbers up to 2,500,000. For which starting number is the sequence longest, and how long is it? Use the generator, but you can convert the output to a list to find the length. 4. Write another generator following slightly different rules, called hailstone2. In this, if the number is odd, multiply by 3 and subtract 1 . Use a to keep track of all the numbers that have already occurred, and break out of the generator if one repeats (otherwise you'll enter an infinite loop). 5. Show the result for hailstone2 starting at the number 3271 . At what number does this sequence enter an infinite loop

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!