Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package threenplusone;
- public class HailstoneLengthQuick {
- static int max;
- public static void main(String[] args) {
- //double start = System.nanoTime();
- HailstoneLengthQuick hlq = new HailstoneLengthQuick();
- // TODO: validate input so we can assume input is in the form 'a, b' where b>=a and b,a>0
- // parse the input into lower and upper bounds for the range on which we want to compute.
- long lower = 1;
- long upper = 2;
- try {
- lower = Integer.parseInt(args[0]);
- upper = Integer.parseInt(args[1]);
- }
- catch (java.lang.ArrayIndexOutOfBoundsException ex) { }
- // the outer loop
- for (long n = upper; n >= lower; n--) {
- long workingN = n;
- int count = 2;
- // the inner loop
- while (workingN!=2) {
- count++;
- workingN = ((workingN&1) == 0) ? workingN>>1 : (3*workingN) +1;
- }
- if (count > max) {
- max = count;
- }
- }
- System.out.println(lower + " " + upper + " " + (hlq.max));
- //max = hlq.max;
- //double elapsedTimeInSec = (System.nanoTime() - start) * 1.0e-9;
- //System.out.println(elapsedTimeInSec);
- }
- }
Add Comment
Please, Sign In to add comment