Collatz - / -/ projects/collatz/Collatz.java / Copyright...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
// ----------------------------- // projects/collatz/Collatz.java // Copyright (C) 2010 // Glenn P. Downing // ----------------------------- import java.util.HashMap; final class Collatz { public static HashMap<Long, Long> cache = new HashMap<Long, Long>(); // ---- // read // ---- /** * reads an int into a[0] and a[1] * @return true if that succeeds, false otherwise */ public static boolean read (Reader r, int[] a) { if (!r.hasNextInt()) return false; a[0] = r.nextInt(); a[1] = r.nextInt(); assert a[0] > 0; assert a[1] > 0; return true;} // ---- // eval // ---- /** * computes the max cycle length in the range [i, j] * and stores the result in v, and returns v * @param a * @return maximum cycle length */ public static int eval (int[] a) { assert a[0] > 0; assert a[1] > 0; /* <your code> */ int v = 1; long cycle; for (long i = Math.min(a[0], a[1]); i <= Math.max(a[1], a[0]); i++) { cycle = process(i); if (cycle > v) v = Long.valueOf(cycle).intValue(); } assert v > 0; return v; } /** * Calculates the cycle length of the current integer
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/05/2010 for the course CS 373 taught by Professor Downing,glennp during the Fall '08 term at University of Texas at Austin.

Page1 / 2

Collatz - / -/ projects/collatz/Collatz.java / Copyright...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online