daily pastebin goal
11%
SHARE
TWEET

Untitled

a guest Jan 14th, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package threenplusone;
  2.  
  3. public class HailstoneLengthQuick {
  4.     static int max;
  5.    
  6.     public static void main(String[] args) {
  7.         //double start = System.nanoTime();
  8.        
  9.         HailstoneLengthQuick hlq = new HailstoneLengthQuick();
  10.         // TODO: validate input so we can assume input is in the form 'a, b' where b>=a and b,a>0
  11.  
  12.  
  13.        
  14.         // parse the input into lower and upper bounds for the range on which we want to compute.
  15.         long lower = 1;
  16.         long upper = 2;
  17.         try {
  18.             lower = Integer.parseInt(args[0]);
  19.             upper = Integer.parseInt(args[1]);
  20.         }  
  21.         catch (java.lang.ArrayIndexOutOfBoundsException ex) { }
  22.    
  23.         // the outer loop
  24.         for (long n = upper; n >= lower; n--) {
  25.             long workingN = n;
  26.             int count = 2;
  27.             // the inner loop
  28.             while (workingN!=2) {
  29.                 count++;
  30.                 workingN = ((workingN&1) == 0) ? workingN>>1 : (3*workingN) +1;        
  31.             }
  32.             if (count > max) {
  33.                 max = count;
  34.             }
  35.         }
  36.        
  37.         System.out.println(lower + " " + upper + " " + (hlq.max));
  38.         //max = hlq.max;
  39.        
  40.         //double elapsedTimeInSec = (System.nanoTime() - start) * 1.0e-9;
  41.         //System.out.println(elapsedTimeInSec);
  42.     }
  43.    
  44.  
  45.    
  46.    
  47. }
RAW Paste Data
Top