1. Write Python code which computes the Hailstone path length of a positive integer. The Hailstone...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
1. Write Python code which computes the Hailstone path length of a positive integer. The Hailstone function is defined as: h(n) n/2, (3n + 1, = : {3n The Hailstone path for an integer n is the sequence of numbers generated by the Hailstone function, beginning at and ending at 1. The Hailstone path length is the number of integers that appear on the Hailstone path, which is 1 plus the number of applications of the Hailstone function required to reach h(n) = 1. For example, the Hailstone path for 12 is 1263105168421 n is even nis odd Thus, the path length is 10. The function was applied 9 times. It is conjectured that all positive integers eventually reach 1 after repeated application of the Hailstone function. Although the conjecture is believed to be true, it has not yet been proven. Hint 1: use a while loop Hint 2: Use integer division: // the double division slash Hint 2: The input function returns a string - you need to convert it to an integer using the int function - like this: n = input('Enter a value for n ') n int (n) Your output should look like this (for an input of 12): Enter a value for n 12 12 6 3 10 5 16 8 4 2 1 The path length is 10 steps Challenge: along with the path length, also find the largest number on the path. 1. Write Python code which computes the Hailstone path length of a positive integer. The Hailstone function is defined as: h(n) n/2, (3n + 1, = : {3n The Hailstone path for an integer n is the sequence of numbers generated by the Hailstone function, beginning at and ending at 1. The Hailstone path length is the number of integers that appear on the Hailstone path, which is 1 plus the number of applications of the Hailstone function required to reach h(n) = 1. For example, the Hailstone path for 12 is 1263105168421 n is even nis odd Thus, the path length is 10. The function was applied 9 times. It is conjectured that all positive integers eventually reach 1 after repeated application of the Hailstone function. Although the conjecture is believed to be true, it has not yet been proven. Hint 1: use a while loop Hint 2: Use integer division: // the double division slash Hint 2: The input function returns a string - you need to convert it to an integer using the int function - like this: n = input('Enter a value for n ') n int (n) Your output should look like this (for an input of 12): Enter a value for n 12 12 6 3 10 5 16 8 4 2 1 The path length is 10 steps Challenge: along with the path length, also find the largest number on the path.
Expert Answer:
Related Book For
Introduction to Algorithms
ISBN: 978-0262033848
3rd edition
Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest
Posted Date:
Students also viewed these programming questions
-
Portray in words what transforms you would have to make to your execution to some degree (a) to accomplish this and remark on the benefits and detriments of this thought.You are approached to compose...
-
Let A, B be sets. Define: (a) the Cartesian product (A B) (b) the set of relations R between A and B (c) the identity relation A on the set A [3 marks] Suppose S, T are relations between A and B, and...
-
I have this data frame with the years and values, can you make a model to predict the value of meats,fish,fruits and vegetables and grains? based on the current values of the table and historical...
-
Why is an insurable interest required for the enforcement of an insurance contract?
-
Solve the following equations: +20 10-2(x-4)=4(x+2)-10 x+5=4-2(2x+3)
-
Why does the earned income tax credit increase the labor force participation rate of targeted groups?
-
A faulty model rocket moves in the xy-plane (the positive y direction is vertically upward). The rocket's acceleration has components ax (t) = at2 and ay (t) = B - yt where a = 2.50 m/s4, B = 9.00...
-
Discuss a literature review for the topic " managing health and safety in the military" . Include citation with a short explication.
-
CASH PAID FOR MERCHANDISE Guild Company's cost of goods sold for 20-2 was $500,000. Compute the amount of cash paid for merchandise in 20-2. The December 31, 20-2 and 20-1, balances of Merchandise...
-
Aqua gym ltd is boutique gym chai that operates in Sydney area. Aqua gym offers an annual membership at a price of $12,000. After costumers pays an upfront cash payment of the annual membership fee...
-
It is July 30, 2021. The cheapest-to-deliver bond in a September 2021 Treasury bond futures contract is a 13% coupon bond, and delivery is expected to be made on September 30, 2021. Coupon payments...
-
What advantages do futures contracts have over forward contracts?
-
What are the advantages and disadvantages of using an options contract rather than a futures contract?
-
What are the advantages and disadvantages of mandatory disclosure when dealing with conflicts of interest?
-
What are the advantages and disadvantages of using interest-rate swaps?
-
Lawn Master Company, a manufacturer of riding lawn mowers, has a projected income for 2010 as follows: Required 1. Determine the breakeven point in sales dollars. 2. Determine the required sales in...
-
If |62x|>9, which of the following is a possible value of x? A. 2 B. 1 C. 0 D. 4 E. 7
-
Explain under what circumstances, if any, redundant DISK-READ or DISK-WRITE operations occur during the course of executing a call to B-TREE-INSERT. (A redundant DISK-READ is a DISK-READ for a page...
-
The reduction algorithm F in the proof of Lemma 34.6 constructs the circuit C = f (x) based on knowledge of x, A, and k. Professor Sartre observes that the string x is input to F, but only the...
-
Equal keys pose a problem for the implementation of binary search trees. a. What is the asymptotic performance of TREE-INSERT when used to insert n items with identical keys into an initially empty...
-
How did the Fed hit its target for the federal funds rate in the pre-2008 scarce-reserves regime?
-
Draw a demand and supply graph for the federal funds market to show the demand and supply of reserves in the current ample-reserves regime. How can the Fed increase the federal funds rate? Show the...
-
What is the zero lower bound in monetary policy? What tools can the Fed use if it wants to implement an expansionary monetary policy at the zero lower bound?
Study smarter with the SolutionInn App