Question

In: Computer Science

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

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 hald 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).

Examples

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

Solutions

Expert Solution

Please look at my code and in case of indentation issues check the screenshots.

------------main.py----------------


def main():
   n = int(input("Enter the integer to begin(greater than 1): "))
   count = 0               #to count the number of values in the sequence
   while n!= 1:           #if number is not equal to 1
       count = count + 1   #increase the count by 1
       if n%2 == 1:       #if the number is odd
           n = 3*n+1       #next number is 3n+1
       else:               #if number is even
           n = n/2           #next number is n/2
   count = count + 1       #to count the last number in sequence which is 1
   print(count)           #print the count


main()

--------------Screenshots-------------------

----------------------Output------------------------------------

-----------------------------------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs down.
Please Do comment if you need any clarification.
I will surely help you.

Thankyou


Related Solutions

The integers satisfy a property known as mathematical induction. This is a familiar topic in high...
The integers satisfy a property known as mathematical induction. This is a familiar topic in high school textbooks. (a) The First Principle of Mathematical Induction is stated as follows. Suppose S is a subset of N with the following properties: (i) The number 1 is in S. (ii) If n is in S, then n + 1 is in S. Using well-ordering, prove S = N. (b) The Second Principle of Mathematical Induction is stated as follows. Suppose S is...
The sequence below represents the DNA sequence of the polylinker (also called the multiple cloning site)...
The sequence below represents the DNA sequence of the polylinker (also called the multiple cloning site) on a plasmid, with the dots (...) on either side representing the continuing DNA on either side of the polylinker .............5' CACTTAAGCCTGCAGCGTTAGCGT 3'......... ..............3' GTGAATTCGGACGTCGCAATCGCA 5'.......... The plasmid is cut with the restriction endonuclease Pst1, which recognizes the following sequence: -------- -- -5' CTGCAG 3' and which cuts between the A and the G nucleotides. A. After cutting the plasmid with Pst1, how many...
The process of synthesis of ATP in the mitochondria is also known as ____
acetyl-CoA NADH carbon dioxide NADP+ NADPH fatty acidsoxidative phosphorylation glucose oxygen NAD+ pyruvate ADPwater ATP photophosphorylation reductive phosphorylationThe process of synthesis of ATP in the mitochondria is also known as ________________________________________________. Mitochondria import ________________________ and _________________________, which are used as sources of energy for subsequent reactions. __________________________ acts as the final electron acceptor in the mitochondrial electron-transport chain. __________________________ produced in the citric acid cycle donates electrons to the electron-transport chain. A molecule of __________________________ is fed into the citric...
The Boulder model of training is also known as the model of training.
 3. The Boulder model of training is also known as the model of training. clinical psychologist clinical scientist scientist-practitioner practitioner-scholar 4. Richard McFall is most closely associated with the development of the model of training. scientist- practitioner practitioner-scholar therapist-educator clinical scientist 5. The_______ typically consists of a full year of supervised clinical experience in an applied setting, and takes place before the doctoral
state Maxwell’s equations mathematical and in ur own words also mention the differentail form and the...
state Maxwell’s equations mathematical and in ur own words also mention the differentail form and the integral form
Any substance that will bind to a protein is known as its __________________.
Any substance that will bind to a protein is known as its __________________. Enzymes bind their _______________ at the __________________. The enzyme hexokinase is so specific that it reacts with only one of the two __________________ of glucose. Enzymes catalyze a chemical reaction by lowering the __________________, because they provide conditions favorable for the formation of a __________________ intermediate called the __________________. Once the reaction is completed, the enzyme releases the __________________ of the reaction.Words: free energy ligand transition state...
A mathematical approximation called the Rule of 70 tells us that the number of years it...
A mathematical approximation called the Rule of 70 tells us that the number of years it will take something that is growing to double in size is approximately equal to the number 70 divided by its percentage rate of growth. For example, if Mexico’s real GDP per person is growing at 7 percent per year, it will take about 10 years (= 70 ÷ 7) to double. Apply the Rule of 70 to solve the following problem. If real GDP...
a. Use mathematical induction to prove that for any positive integer ?, 3 divide ?^3 +...
a. Use mathematical induction to prove that for any positive integer ?, 3 divide ?^3 + 2? (leaving no remainder). Hint: you may want to use the formula: (? + ?)^3= ?^3 + 3?^2 * b + 3??^2 + ?^3. b. Use strong induction to prove that any positive integer ? (? ≥ 2) can be written as a product of primes.
A repurchase agreement, also known as a repo, is the sale of securities together with an...
A repurchase agreement, also known as a repo, is the sale of securities together with an agreement for the seller to buy back the securities at a later date. The repurchase price should be greater than   the original sale price, the difference effectively representing interest, sometimes called the repo rate. The party that originally buys the securities effectively acts as a lender. The original seller is effectively acting as a borrower, using their security as collateral for a secured cash...
A protein (also known as an immunoglobulin) that bind to specific antigens are what?
A protein (also known as an immunoglobulin) that bind to specific antigens are what? A T cell An antigen A receptor An Antibody John Snow used _______ to show that the water from the Broad Street Pump was the probable source of the London cholera epidemic of 1854  statistics  random clinical trials  laboratory analysis  sound judgement
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT