A hailstone sequence starts with some positive integer. If that integer is even, then you divide it by two to get the next integer in the sequence, but if it is odd, then you multiply it by three and add one to get the next integer in the sequence. Then you use the value you just generated to find the next value, according to the same rules. Fo example, if our initial number is 3, the subsequent numbers will be: 10, 5, 16, 8, 4, 2, 1. Write a function named hailstone that takes a positive integer parameter as the initial number of a hailstone sequence and returns how many steps it takes to reach 1 (technically you could keep going 1, 4, 2, 1, 4, 2, etc. but you will stop when you first reach 1). If the starting integer is 1, the return value should be 0, since it takes no steps to reach 1 (we're already there). For example, if the starting integer is 3, then the sequence would go: 3, 10, 5, 16, 8, 4, 2, 1, and the return value should be 7, since took 7 steps to reach 1. Your function does not need to print anything out - just return a value. For example, your function could be used like this: answer = hailstone(1000) print(answer) You cannot use recursion, since we haven't covered that technique. That means that for this assignment you cannot have your function call itself. This includes indirect recursion, such as one function calling a second function that calls the first function. The file must be named: hailstone.py

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

In python, no recursion

A hailstone sequence starts with some positive integer. If that integer is even, then you divide it by two to get the next integer in the sequence, but if it is odd, then you
multiply it by three and add one to get the next integer in the sequence. Then you use the value you just generated to find the next value, according to the same rules. For
example, if our initial number is 3, the subsequent numbers will be: 10, 5, 16, 8, 4, 2, 1.
Write a function named hailstone that takes a positive integer parameter as the initial number of a hailstone sequence and returns how many steps it takes to reach 1
(technically you could keep going 1, 4, 2, 1, 4, 2, etc. but you will stop when you first reach 1). If the starting integer is 1, the return value should be 0, since it takes no
steps to reach 1 (we're already there). For example, if the starting integer is 3, then the sequence would go: 3, 10, 5, 16, 8, 4, 2, 1, and the return value should be 7, since it
took 7 steps to reach 1. Your function does not need to print anything out - just return a value.
For example, your function could be used like this:
answer = hailstone(1000)
print(answer)
You cannot use recursion, since we haven't covered that technique. That means that for this assignment you cannot have your function call itself. This includes indirect
recursion, such as one function calling a second function that calls the first function.
The file must be named: hailstone.py
Transcribed Image Text:A hailstone sequence starts with some positive integer. If that integer is even, then you divide it by two to get the next integer in the sequence, but if it is odd, then you multiply it by three and add one to get the next integer in the sequence. Then you use the value you just generated to find the next value, according to the same rules. For example, if our initial number is 3, the subsequent numbers will be: 10, 5, 16, 8, 4, 2, 1. Write a function named hailstone that takes a positive integer parameter as the initial number of a hailstone sequence and returns how many steps it takes to reach 1 (technically you could keep going 1, 4, 2, 1, 4, 2, etc. but you will stop when you first reach 1). If the starting integer is 1, the return value should be 0, since it takes no steps to reach 1 (we're already there). For example, if the starting integer is 3, then the sequence would go: 3, 10, 5, 16, 8, 4, 2, 1, and the return value should be 7, since it took 7 steps to reach 1. Your function does not need to print anything out - just return a value. For example, your function could be used like this: answer = hailstone(1000) print(answer) You cannot use recursion, since we haven't covered that technique. That means that for this assignment you cannot have your function call itself. This includes indirect recursion, such as one function calling a second function that calls the first function. The file must be named: hailstone.py
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY