Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me answer this problem please showing a step-by-step walk-through on how to solve this code with Comments throughout the code, please? can

Can someone help me answer this problem please showing a step-by-step walk-through on how to solve this code with Comments throughout the code, please? can this be done in Python-style code within a juypter notebook, can I ask that when you write your code to please double-check that it runs without any errors as well as I've been having trouble running code on here before and it is not working. With this being said can you type out your code so it's easier to follow/read thank you again in advance sorry for the long message.

image text in transcribed

image text in transcribed

[ ] \# Use this set of imports at the top of your code \# You can import other standard packages if needed \%matplotlib inline import matplotlib.pyplot as plt import numpy as np import numpy.random as rnd import pandas as pd 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 hailstone 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 set 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

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

Recommended Textbook for

Databases Illuminated

Authors: Catherine M. Ricardo

1st Edition

0763733148, 978-0763733148

Students also viewed these Databases questions

Question

How could assessment be used in an employee development program?

Answered: 1 week ago