Example of Java data structure and algorithm: Collatz Conjecture

  • 2020-04-01 03:56:51
  • OfStack


 
package al; 
public class CollatzConjecture { 
  private int i = 1; 
  public static void main(String[] args) { 
    long l = 9999999; 
    System.out.println("l is "+ l); 
    CollatzConjecture cc = new CollatzConjecture(); 
    cc.Collatz(l); 
  } 
   
   
  private void Collatz(long param) {    
    System.out.println("After " + i + " time we get result " + param); 
    if(param > 1) { 
      i++; 
      if (param%2 == 0) { 
        Collatz(param/2);         
      } else { 
        Collatz(3*param + 1); 
      } 
    } 
  } 
} 


Related articles: