Write a program that reads in a number from the user

Assignment Help Programming Languages
Reference no: EM131251209

Douglas Hofstadter's Pulitzer-prize-winning book Gödel, Escher, Bach contains many interesting mathematical puzzles. In Chapter XII, Hofstadter mentions a wonderful problem that is well within the scope of the control statements in Java. The problem can be expressed as follows: Pick some positive integer and call it n. If n is even, divide it by two. If n is odd, multiply it by three and add one. Continue this process until n is equal to 1. Hofstadter illustrates this process with the following example, starting with the number n = 15:

15 is odd, so I make 3n+1: 46
46 is even, so I take half: 23
23 is odd, so I make 3n+1: 70
70 is even, so I take half: 35
35 is odd, so I make 3n+1: 106
106 is even, so I take half: 53
53 is odd, so I make 3n+1: 160
160 is even, so I take half: 80
80 is even, so I take half: 40
40 is even, so I take half: 20
20 is even, so I take half: 10
10 is even, so I take half: 5
5 is odd, so I make 3n+1: 16
16 is even, so I take half: 8
8 is even, so I take half: 4
4 is even, so I take half: 2
2 is even, so I take half: 1

As you can see from this example, the numbers go up and down, but eventually-at least for all numbers that have ever been tried-come down to end in 1. In some respects, this process is reminiscent of the formation of hailstones, which get carried upward by the winds over and over again before they finally descend to the ground. Because of this analogy, this sequence of numbers is usually called the Hailstone sequence, although it goes by many other names as well.

Write a program that reads in a number from the user and then displays the Hailstone sequence for that number, followed by a line showing the number of steps taken to reach 1.

Reference no: EM131251209

Questions Cloud

New customers they sign up for new magazine subscriptions : Outbound sales representatives receive a $20 commission on all new customers they sign up for new magazine subscriptions. Each outbound sales representative works 40 hours. Because these employees are paid solely on commission, the employer must ensu..
Write a program to compute the total bonus amount earned : An employee has committed to save $2000 per month. Write a program to compute the total bonus amount earned by these two employees in the year.
Analyze the prioritizing process : Analyze the prioritizing process at D. D. Williamson. Suggest two (2) recommendations to improve the prioritizing process. Create a scenario where the implemented process at D. D. Williamson would not work.
Deliver package to customer before traveling home : Nadine is an administrative assistant for her employer. At the end of her shift one day, her employer requires her to deliver a package to a customer before traveling home. She spends two hours driving to the customer site, and then three hours drivi..
Write a program that reads in a number from the user : Write a program that reads in a number from the user and then displays the Hailstone sequence for that number, followed by a line showing the number of steps taken to reach 1.
Create the code for a login system : SGA1789 Build Dynamic Websites Workbook Assignment. Using PHP session management features create the code for a login system. This will be used for security and access control of the database component you developed in Assessment Task. Ensure there..
Analyze the prioritizing process : Analyze the prioritizing process at D. D. Williamson. Suggest two (2) recommendations to improve the prioritizing process. Create a scenario where the implemented process at D. D. Williamson would not work.
Sole proprietor who performs excavating services : In July of 2009, Mr. Mann, a sole proprietor who performs excavating services, purchased and put to use for business a piece of heavy equipment for $36,500. This piece of equipment was depreciated using the MACRS 150% DB method of depreciation over t..
Corporate social responsibility : 1. In terms of corporate social responsibility, discuss three ways that a company can focus on the needs of its stakeholders without neglecting its shareholder obligations.

Reviews

Write a Review

Programming Languages Questions & Answers

  Find the position s of a body in linear motion

How many miles will the car go during the period of acceleration from zero to 60 mph? Use the given program to answer this question - Prepare code to declare variables for s0, v0, a, and t, and then write the code to compute s on the basis of these ..

  Analyze the need for waterfall and agile methodologies

Analyze the need for waterfall and agile methodologies. Explain the advantages of extreme programming (XP) and analyze the advantages of its application in high-budget short-time projects.

  Create link invisible to begin-display after submit button

Create link invisible to begin but display it after Submit button has been clicked. Confirmation page should say "Thank you for your order".

  Aggregate planning for a bottling companya bottling company

aggregate planning for a bottling companya bottling company has decided to introduce a new line of premium bottled

  Solution-p1mygrade

a) Name this Solution "p1MyGrade" and this project "MyGrade01". b) Write a simple program that does the following:

  Program to compute percentage and final grade for students

Write a program which will permit teacher to compute percentage and final grade for students in a class.

  Outline and critique how samsung has introduced and

outline and critique how samsung has introduced and developed a holistic system of tqm management and customer focused

  Write a program to generate a patient bill

Write programs in Visual Basic while being guided by the six steps of the program development cycle: analyze the problem, design a solution, choose the interface, write code, test and debug your solution, and document code.

  Discuss the ajax model in relation to good software design

Discuss the AJAX model in relation to good software design, and the essence of the quoted question. Will AJAX further promote the development of Web based software, or is it just another fad?

  Create j2ee application

Business-tier components run on the J2EE server. 1. Create session bean to represent business logic - CalculatorClient-tier components run on the client machine. 2. Create an application client to access the Calculator

  Write a perl program that calculates the at and gc content

Write a Perl program that calculates the AT and GC content (i.e. the percentage of G and C, and the percentage of A and T) in a given sequence.

  Write program to compute and outputs the average gpa

Write a program that computes and outputs the average GPA for both female and male students. Format your results to two decimal places. Your program should use the functions.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd