It's raining hailstones! Download

Christina Cacioppo

if statements, while loops, logical operators Student approved


Introduction

The Hailstone sequence is a famous sequence that mathematicians have observed but haven't quite yet explained. Can you write a script for it?


Task

Here's the run-down:

  1. Start with a whole number x.
  2. While x isn't 1 do the following: a. If x is even, divide it by 2. b. If x is odd, multiply it by 3 and add 1. c. Print x.
  3. Repeat step 2 until x is 1.

Though it appears x will always equal 1 eventually, no one has yet figured out why.


Starter code

class Hailstone{
	public static void main(String [] args){
		hailstones();
	}

	static void hailstones(){
		int x = 15;	// change this number if you like!
		/*		Your code here		*/
	}
}


Expected output

If you start at 15, your program should display:

15
46
23
70
35
106
53
160
80
40
20
10
5
16
8
4
2
1

One solution


Hints



Useful tools to solve the problem


Related links