Question

***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in...

***Python

Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in the sequence, the next number in the sequence is determined by two simple rules:

  • If the current number n is odd, the next number in the sequence is equal to 3 * n + 1

  • If the current number n is even instead, the next number in the sequence is equal to one half of n (i.e., n divided by 2)

We repeat this process until it produces 1 (which is where the "mathematical curiosity" comes into play; so far, mathematicians have been unable to prove that this process always (eventually) produces the value 1, but no one has been able to find a counterexample that doesn't eventually reach 1). A Hailstone sequence can begin with any positive integer. For example, the integer 6 produces the hailstone sequence

6, 3, 10, 5, 16, 8, 4, 2, 1

In the code cell below, use the input() command to read in an integer from the user (assume that this integer is always positive and greater than 1). Then use a loop (we recommend a while loop) and any extra variables that you may need to follow the rules above to generate a Hailstone sequence from your starting number. DO NOT print out the Hailstone numbers that you generate; instead, your code should print exactly one number when it finishes: the total number of values in the Hailstone sequence that you generated (including both the starting value and the final 1 that ends the sequence).

Helpful Hint: One easy way to check whether an integer is even or odd is to use the modulus operator. If the number modulo 2 produces 0 as its remainder, then that number is even; if the remainder is 1, then the number is odd. For example, 7 % 2 returns 1, indicating that 7 is odd.

Examples

The starting value 2 produces the output 2 (internally, it generates the sequence 2, 1).

The starting value 17 produces the output 13 (internally, it generates the sequence 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1).

****This is the full question

Homework Answers

Answer #1

Program Code Screenshot :

Sample Output :

Program Code to Copy

#Read an integer
n = int(input())
c = 1
#Loop until number is not 1
while n!=1:
#Check if even or odd
if n%2==0:
n=n//2
else:
n = 3*n+1
c=c+1
print('Total number of values in the sequence ',c)

Know the answer?
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
Similar Questions
I am a student taking python programming. Can this problem be modified using the define main...
I am a student taking python programming. Can this problem be modified using the define main method, def main()? import random #function definition #check for even and return 0 if even def isEven(number): if(number%2==0): return 0 #return 1 if odd else: return 1 #count variables even =0 odd = 0 c = 0 #loop iterates for 100 times for i in range(100): #generate random number n = random.randint(0,1000) #function call val = isEven(n) #check value in val and increment if(val==0):...
1. Write 3n + 1 Sequence String Generator function (1 point) In lab5.py complete the function...
1. Write 3n + 1 Sequence String Generator function (1 point) In lab5.py complete the function sequence that takes one parameter: n, which is the initial value of a sequence of integers. The 3n + 1 sequence starts with an integer, n in this case, wherein each successive integer of the sequence is calculated based on these rules: 1. If the current value of n is odd, then the next number in the sequence is three times the current number...
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
FOR PROBLEMS 1 AND 2. Use the sequence formulas. Carry out your calculations to the final...
FOR PROBLEMS 1 AND 2. Use the sequence formulas. Carry out your calculations to the final number. Round the results to 1 decimal place.    1. Consider the sequence: 1/2, 1, 2, 4, 8, a. Find the 30th term of the sequence. b. Find the sum of the first 30 terms of the sequence. 2. You give your daughter a piggy bank with $2 in it. She puts $5 in it the next week and each week thereafter. How much...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will implement Queue Abstract Data Type with the following functions/methods.  Any build-in/pre-defined Queue function/library (e.g., java.util.Queue in Java) is NOT allowed to use in your code. push(Element):  insert the input Element (e.g., String or Integer in Java) to the end of the queue. pop(): remove the head element of the queue and print the head element on screen. count():  return the total number of elements in the queue...
using dr.racket programing language If we write a function that tests whether a list contains only...
using dr.racket programing language If we write a function that tests whether a list contains only strings, odd numbers, or even numbers, you will notice that the code that iterates through the list stays the same, with the only change being the predicate function that checks for the desired list element. If we were to write a new function for each of the tests listed above, it would be more error-prone and an example of bad abstraction. We could write...
Use python to write the code You’re going to program a simulation of the following game....
Use python to write the code You’re going to program a simulation of the following game. Like many probability games, this one involves an infinite supply of ping-pong balls. No, this game is "not quite beer pong." The balls are numbered 1 through N. There is also a group of N cups, labeled 1 through N, each of which can hold an unlimited number of ping-pong balls (all numbered 1 through N). The game is played in rounds. A round...
In python Please The general elections are over, now is the time to count the votes...
In python Please The general elections are over, now is the time to count the votes and find out who will take the reins of this country. There are c (2 <= c <= 6) candidates and m (1 <= m <= 74) voting regions. Given the number of votes for each candidate in each municipality, determine which candidate is the winner (the one with the most votes). Input Format The first line of input contains an integer T (1...
Write an application in Java which includes an algorithm that takes an array of any size,...
Write an application in Java which includes an algorithm that takes an array of any size, selects the high and low integer from the array of integers with each pass and builds a new array of integers by inserting the high and low selection with each pass. Your output should show the original array of integers and the output of each pass on a new line. Note: You can assume all integers are positive, but your code must work for...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...