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
Please follow the insturctions and solve it by C++ ASAP. The hailstone sequence is a series...
Please follow the insturctions and solve it by C++ ASAP. The hailstone sequence is a series of numbers that can be generated with an algorithm that takes an input number n. The sequence is defined as: If n is 1, the sequence stops. If n is even, the next number is n / 2. If n is odd, the next number is 3n + 1. For example, if we start with n = 10, the sequence is: 10, 5, 16,...
python a) Write a function, hailstone(), that takes an integer value n as a parameter, and...
python a) Write a function, hailstone(), that takes an integer value n as a parameter, and displays the hailstone sequence for the given integer. The hailstone sequence is determined as follows: if the value is even, divide by 2 (floor division) or if the value is odd, calculate 3 * n + 1. The function should display each value and continue updating the value until it becomes 1. b) Write a program to display the hailstone sequence of all integers...
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...
Write a script that will generate seven random integers in the range [-100:200] inclusive and then...
Write a script that will generate seven random integers in the range [-100:200] inclusive and then print the value of each integer and whether it is positive or negative and whether it is even or odd. An output from this program might be: -57 is negative and odd. 26 is positive and even. If zero is one of the random integers generated, remember that zero is even and neither negative or positive. Hint: even numbers are divisible by 2 with...
Assembler lanaguage program   The program is to prompt the user to enter a number between 2...
Assembler lanaguage program   The program is to prompt the user to enter a number between 2 and 100. Reject any invalid user inputs and terminate the program if invalid inputs are entered. Print each even number from 2 to the user entered number. Sum these even numbers and print the sum. Print each odd number from 1 to the user entered number. Sum these odd numbers and print the sum. Exit the program when the output is complete. The output...
/* 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...
Python: Write a program that prompts the user to enter a positive integer value, and compute...
Python: Write a program that prompts the user to enter a positive integer value, and compute the following sequence: • If the value is even, halve it. • If it's odd, multiply by 3 and add 1. • Repeat this process until the value is 1, printing out each value. • Then print out how many of these operations you performed. If the input value is less than 1, print a message containing the word Error and exit the program....
Section 2: Using the MARS or SPIM simulator develop a program that will implement the following...
Section 2: Using the MARS or SPIM simulator develop a program that will implement the following conditional statement. If ( n is even) { n = n / 2; } else { n = 3 * n + 1; } In this case, n is to be input by the user (assume they input a non-negative value), the conditional is performed, and the resulting n is to be output. Again, use the system calls for input, output, and exiting the...
We are given a sequence of numbers: 1, 3, 5, 7, 9, . . . and...
We are given a sequence of numbers: 1, 3, 5, 7, 9, . . . and want to prove that the closed formula for the sequence is an = 2n – 1.          What would the next number in the sequence be? What is the recursive formula for the sequence? Is the closed formula true for a1? What about a2? What about a3? Critical Thinking How many values would we have to check before we could be sure that the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT