Friday, February 17, 2012

Recitation 4 Programs

From last week:

Write a program that:

  1. Reads in a number, and prints "Correct" if it is 7453047.
     Number.java 

  1  public class Number {
  2      public static void main(String[] args) {
  3          int x = IO.readInt();
  4          if (x == 7453047) {
  5              System.out.println("Correct");
  6          }
  7          else {
  8              System.out.println("Incorrect");
  9          }
 10      }
 11  }


More using conditionals:

  1. Because 11^2 > 100, a number < 100 is prime if it is not divisible by 2, 3, 5 or 7. Is 93 prime?
     Prime93.java 

  1  public class Prime93 {
  2      public static void main(String[] args) {
  3          int n = 93;
  4  
  5          if (n%2 != 0 && n%3 != 0 && n%5 != 0 && n%7 != 0) {
  6              System.out.println("Prime");
  7          }
  8          else {
  9              System.out.println("Not prime");
 10          }
 11      }
 12  }

Not prime
  1. Test if an arbitrary number < 100 is prime.
     Prime.java 

  1  public class Prime {
  2      public static void main(String[] args) {
  3          int n = IO.readInt();
  4  
  5          if (n%2 != 0 && n%3 != 0 && n%5 != 0 && n%7 != 0) {
  6              System.out.println("Prime");
  7          }
  8          else if (n==2 || n==3 || n==5 || n==7) {
  9              System.out.println("Prime");
 10          }
 11          else {
 12              System.out.println("Not prime");
 13          }
 14      }
 15  }


Java operators

  1. What do the following print?
  1  System.out.println(2 + 2);
  2  System.out.println(2 * 2);
  3  System.out.println(2 / 2);
  4  System.out.println(2 % 2);
  5  System.out.println(2 > 2);
  6  System.out.println(2 >= 2);
  7  System.out.println(2 < 2);
  8  System.out.println(2 <= 2);
  9  System.out.println(2 == 2);
 10  
 11  System.out.println(2 < 2 || 2 == 2);
 12  System.out.println(2 < 2 && 2 == 2);

     Operators.java 

  1  public class Operators {
  2      public static void main(String[] args) {
  3          System.out.println("2 + 2 = " + (2 + 2));
  4          System.out.println("2 * 2 = " + (2 * 2));
  5          System.out.println("2 / 2 = " + (2 / 2));
  6          System.out.println("2 % 2 = " + (2 % 2));
  7          System.out.println("2 > 2 = " + (2 > 2));
  8          System.out.println("2 >= 2 = " + (2 >= 2));
  9          System.out.println("2 < 2 = " + (2 < 2));
 10          System.out.println("2 <= 2 = " + (2 <= 2));
 11          System.out.println("2 <= 2 = " + (2 == 2));
 12  
 13          System.out.println("2 < 2 || 2 == 2 = " + (2 < 2 || 2 == 2));
 14          System.out.println("2 < 2 && 2 == 2 = " + (2 < 2 && 2 == 2));
 15      }
 16  }

2 + 2 = 4
2 * 2 = 4
2 / 2 = 1
2 % 2 = 0
2 > 2 = false
2 >= 2 = true
2 < 2 = false
2 <= 2 = true
2 <= 2 = true
2 < 2 || 2 == 2 = true
2 < 2 && 2 == 2 = false
  1. Rewrite this as 1 if statement:
  1  if (x > 3) {
  2      if (y == 2) {
  3          System.out.println("Hi");
  4      }
  5  }

  1  if (x > 3 && y == 2) {
  2      System.out.println("Hi");
  3  }

Java types

  1. What type should go in the ? for each of these variable declarations? (e.g., if ? x = 7, it should be int).
  1  ? x = 2;
  2  ? x = -7;
  3  ? x = 11.75;
  4  ? x = "Hello";
  5  ? x = true;
  6  
  7  int a = 3;
  8  int b = 4;
  9  double c = 10.5;
 10  ? x = a + b;
 11  ? x = a / b;
 12  ? x = a + b + c;

  1  int x = 2;
  2  int x = -7;
  3  double x = 11.75;
  4  String x = "Hello";
  5  boolean x = true;
  6  
  7  int a = 3;
  8  int b = 4;
  9  double c = 10.5;
 10  int x = a + b;
 11  int x = a / b;
 12  double x = a + b + c;

Using loops:

  1. How long does it take the computer to do Math.sin(2)? You can use System.currentTimeMillis() to get the current time:

      1  long time = System.currentTimeMillis();
      2  System.out.println("The time is " + time);
    
    

    The answer is small but not 0.

     Timing.java 

  1  public class Timing {
  2      public static void main(String[] args) {
  3  
  4          // take time at start
  5          long start = System.currentTimeMillis();
  6          // do something 1000 times
  7          int count = 1;
  8          while (count <= 1000000) {
  9              Math.sin(2);
 10              count = count + 1;
 11          }
 12          // take time at end
 13          long end = System.currentTimeMillis();
 14          // subtract them
 15          System.out.println((end - start)*1000.0 + " nanoseconds");
 16      }
 17  }

71000.0 nanoseconds
  1. Which of Math.sin(2), Math.cos(2), Math.sqrt(2), Math.acos(2) or any other Math.something(2) is the slowest?
     Timing2.java 

  1  public class Timing2 {
  2      public static void main(String[] args) {
  3          long start, end;
  4          int count;
  5  
  6          start = System.currentTimeMillis();
  7          count = 1;
  8          while (count <= 1000000) {
  9              Math.sin(2);
 10              count = count + 1;
 11          }
 12          end = System.currentTimeMillis();
 13          System.out.println("sin(2): " + ((end - start)*1000.0) + " nanoseconds");
 14  
 15          start = System.currentTimeMillis();
 16          count = 1;
 17          while (count <= 1000000) {
 18              Math.cos(2);
 19              count = count + 1;
 20          }
 21          end = System.currentTimeMillis();
 22          System.out.println("cos(2): " + ((end - start)*1000.0) + " nanoseconds");
 23  
 24          start = System.currentTimeMillis();
 25          count = 1;
 26          while (count <= 1000000) {
 27              Math.sqrt(2);
 28              count = count + 1;
 29          }
 30          end = System.currentTimeMillis();
 31          System.out.println("sqrt(2): " + ((end - start)*1000.0) + " nanoseconds");
 32  
 33          start = System.currentTimeMillis();
 34          count = 1;
 35          while (count <= 1000000) {
 36              Math.acos(2);
 37              count = count + 1;
 38          }
 39          end = System.currentTimeMillis();
 40          System.out.println("acos(2): " + ((end - start)*1000.0) + " nanoseconds");
 41      }
 42  }

sin(2): 73000.0 nanoseconds
cos(2): 63000.0 nanoseconds
sqrt(2): 0.0 nanoseconds
acos(2): 30000.0 nanoseconds
  1. The "3n+1" sequence is defined as:

    Start with a number n.

    To get the next number,

    1. If n is even, divide it by 2.
    2. If n is odd, multiple by 3 and add 1.

    So for example, starting with 10 you get

    10, 5, 16, 8, 4, 2, 1, 4, 2, 1, ...
    

    The Collatz Conjecture is an old unsolved problem, which says: no matter what number you start with, you will eventually hit 1.

    Test the Collatz Conjecture for some large starting numbers.

     Collatz.java 

  1  public class Collatz {
  2      public static void main(String[] args) {
  3          System.out.println("Enter the starting number:");
  4          int x = IO.readInt();
  5          if (x <= 0) {
  6              System.out.println("No good");
  7              return;
  8          }
  9  
 10          while (x != 1) {
 11              System.out.println(x);
 12              if (x % 2 == 0) {
 13                  x = x / 2;
 14              }
 15              else {
 16                  x = 3*x + 1;
 17              }
 18          }
 19          System.out.println();
 20          System.out.println(x);
 21      }
 22  }


  1. Test the Collatz Conjecture for every number less than 1000.
     Collatz2.java 

  1  public class Collatz2 {
  2      public static void main(String[] args) {
  3          int count = 1;
  4          int x;
  5          while (count < 1000) {
  6              x = count;
  7              count = count + 1;
  8              while (x != 1) {
  9                  System.out.println(x);
 10                  if (x % 2 == 0) {
 11                      x = x / 2;
 12                  }
 13                  else {
 14                      x = 3*x + 1;
 15                  }
 16              }
 17              System.out.println();
 18              System.out.println(x);
 19          }
 20      }
 21  }