Commit 3be9177

mokha <mokha@cisco.com>
2019-05-05 20:13:14
complete section 7
1 parent 98abbdc
Changed files (3)
assignments
assignment1
src
main
java
ca
mokhan
test
java
ca
mokhan
assignments/assignment1/src/main/java/ca/mokhan/assignment1/HailstoneSequence.java
@@ -0,0 +1,16 @@
+package ca.mokhan.assignment1;
+
+import java.util.ArrayList;
+
+public class HailstoneSequence {
+  public static ArrayList<Integer> getHailstoneSequence(int n) {
+    ArrayList<Integer> items = new ArrayList<Integer>();
+
+    while (n != 1) {
+      items.add(n);
+      n = (n % 2 == 0) ? n / 2 : (n * 3) + 1;
+    }
+    items.add(1);
+    return items;
+  }
+}
assignments/assignment1/src/test/java/ca/mokhan/assignment1/HailstoneSequenceTest.java
@@ -0,0 +1,39 @@
+package ca.mokhan.assignment1;
+
+import java.util.ArrayList;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class HailstoneSequenceTest extends TestCase {
+  public HailstoneSequenceTest(String testName) {
+    super(testName);
+  }
+
+  public static Test suite() {
+    return new TestSuite(HailstoneSequenceTest.class);
+  }
+
+  public void testIsRightTriangle() {
+    ArrayList<Integer> results = HailstoneSequence.getHailstoneSequence(15);
+
+    assertTrue(15 == results.get(0));
+    assertTrue(46 == results.get(1));
+    assertTrue(23 == results.get(2));
+    assertTrue(70 == results.get(3));
+    assertTrue(35 == results.get(4));
+    assertTrue(106 == results.get(5));
+    assertTrue(53 == results.get(6));
+    assertTrue(160 == results.get(7));
+    assertTrue(80 == results.get(8));
+    assertTrue(40 == results.get(9));
+    assertTrue(20 == results.get(10));
+    assertTrue(10 == results.get(11));
+    assertTrue(5 == results.get(12));
+    assertTrue(16 == results.get(13));
+    assertTrue(8 == results.get(14));
+    assertTrue(4 == results.get(15));
+    assertTrue(2 == results.get(16));
+    assertTrue(1 == results.get(17));
+  }
+}
assignments/assignment1/README.md
@@ -160,3 +160,43 @@ b. a=84, c=91  b = 35
 II. Determine if the following triangles are right-angled triangles:
 a. a=45, b=55, c=75
 b. a=28, b=45, c=53
+
+7. 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.