Recursive Syracuse: Testing for termination - Java Object Oriented Design

Java examples for Object Oriented Design:Method

Description

Recursive Syracuse: Testing for termination

Demo Code

public class Main {
  public static double Syracuse(int n) {
    if (n == 1)/*from   w  w w  .j a  v a2s .c om*/
      return 1;
    else if (n % 2 == 0)
      return 1 + Syracuse(n / 2); // even
    else
      return (1 + Syracuse(3 * n + 1) / 2);
  }

  public static void main(String[] args) {
    for (int i = 1; i <= 10000; i++) {
      System.out.println("Test termination for " + i);
      Syracuse(i);
    }

  }
}

Result


Related Tutorials