Sunday, March 9, 2014

Problem 1.6.1 - The 3n+1 Problem - Java

1:  package chapter1;  
2:  import java.util.Scanner;  
3:  /**Program to determine the cycle-length of integers  
4:   * Start with an integer n  
5:   * If even-divide the integer n by 2  
6:   * If odd-compute using the formula 3*n+1  
7:   * cycle-length is the number of outputs till n=1   
8:   * n ranges from (0,1000000)-open interval  
9:   * range is given a (i,j)  
10:   *   
11:   * @author mridul  
12:   * @date 03/09/2014  
13:   */  
14:  public class Problem_161 {  
15:       //constant declarations  
16:       private static int MIN_RANGE=0;  
17:       private static int MAX_RANGE=1000000;  
18:       public static void main(String[] args) {  
19:            try {  
20:                 //method variables  
21:                 Scanner scan=new Scanner(System.in);  
22:                 int min, max;  
23:                 System.out.println("Enter two integers in ascending order from "  
24:                           + "the range (0,1000000):");  
25:                 //create a problem instance  
26:                 Problem_161 problem= new Problem_161();  
27:                      System.out.print("Enter first argument : ");  
28:                      min=Integer.parseInt(scan.nextLine());  
29:                      System.out.print("\nEnter second argument : ");  
30:                      max=Integer.parseInt(scan.nextLine());  
31:                      System.out.println("\n\n");  
32:                      scan.close();  
33:                 //proceed if input is valid  
34:                 if(problem.testInput(min,max)){  
35:                      System.out.println("The output in the order min, max, "  
36:                                + "maximum cyclength is : ");                      
37:                      System.out.println(min+" "+max+" "+problem.computeCycleLength(min,max));  
38:                 }else{  
39:                      System.out.println("Invalid Input, please rerun with correct input!");  
40:                 }  
41:            }catch(Exception e){  
42:                 System.out.println("An exception was encountered as given below : ");  
43:                 e.printStackTrace();  
44:            }            
45:       }  
46:       /*private method to compute cycle length  
47:        * returns the cycle length  
48:        */  
49:       int computeCycleLength(int min,int max){  
50:            int cycleMax=0; //max cycle length  
51:            int candidate=min;  
52:            while(candidate<=max){  
53:                 int cycleLength=1; //accounting for 1  
54:                 //if even divide by 2 else compute 3n+1  
55:                 //till candidate is greater than 1  
56:                 while(candidate>1){  
57:                      if(candidate%2==0){  
58:                           candidate=candidate/2;  
59:                      }else{  
60:                           candidate=(3*candidate)+1;                           
61:                      }  
62:                      ++cycleLength;  
63:                 }  
64:                 //increment candidate to next value  
65:                 candidate=++min;       
66:                 //replace cycleMax if cycleLength is greater  
67:                 cycleMax =(cycleLength>cycleMax)? cycleLength:cycleMax;  
68:            }  
69:            return cycleMax;  
70:       }  
71:       /* private method to test input validity  
72:        * provided testLimits method returns true  
73:        * returns true if input is in ascending order   
74:        */  
75:       boolean testInput(int min, int max){  
76:            //if within limits test for order  
77:            if(testLimits(min) && testLimits(max)){  
78:                 if(min<max){  
79:                      return true;  
80:                 }  
81:            }  
82:            return false; //default return            
83:       }  
84:       /*private method to test if input is within limits  
85:        * returns true if condition is satisfied  
86:        */  
87:       boolean testLimits(int number){  
88:            if(number>MIN_RANGE && number<MAX_RANGE){  
89:                 return true;  
90:            }  
91:            return false; //default return  
92:       }  
93:  //End of Problem_161  
94:  }  

No comments:

Post a Comment