Answered step by step
Verified Expert Solution
Question
1 Approved Answer
. 1 7 LAB: Hailstone Numbers Learning objectives In this lab, you will: Write a function which reduces a particular number to 1 using hailstone
LAB: Hailstone Numbers
Learning objectives
In this lab, you will:
Write a function which reduces a particular number to using hailstone number sequences
Write a function to compare the number of iterations it takes between two numbers to get to
Motivation
One currently unsolved problem in mathematics looks at sequences of numbers, sometimes called hailstone numbers.
Given a positive integer n the following rules will always create a sequence that ends with called the hailstone sequence:
If n is even, divide it by
If n is odd, multiply it by and add ien
Continue until n is
From any starting number, you can make a sequence of numbers, applying this rule repeatedly.
Mathematicians guess that if you apply the rule enough times to any natural number, you'll eventually end up with which then is tripled plus one to get then then again in an endless cycle The formalization of this guess is called the Collatz Conjecture, and it is one of the most wellknown open ie unsolved problems in mathematics.
Instructions:
Feel free to write your own assert statements to test and debug your functions.
Part : Generate a hailstone sequence
Write a function gethailstoneseq that takes an integer and returns a list that contains hailstone numbers as integers starting from that integer and up until the first digit
assert gethailstoneseq
assert gethailstoneseq
Part : Get the shorter hailstone sequence
Write a function shorterhailstoneseq that takes two integers and returns a list that contains the shorter hailstone sequence. If the sequences have the same length, return the one that corresponds to the first parameter. The function calls the gethailstoneseq as its helper function.
assert shorterhailstoneseq
assert shorterhailstoneseq
Deconstructing an example
Let's look at shorterhailstoneseq In this case, num is and num is
First, let's look at num:
since is even, we can get the next number by dividing it by so
since is odd, the next number is
then
then
then
then and we've arrived at in steps.
The resulting sequence is
Then, let's look at num:
since is odd, the next number is
since is even, the next number is
then
then
then
then
then and we've arrived at in steps.
The resulting sequence is
So since is strictly fewer than steps, our function returns the sequence that corresponds to the number ie
assert shorterhailstoneseq
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started