lots of exercises in java... from https://github.com/exercism/java

CollatzCalculator.java 402B

1234567891011121314151617181920
  1. class CollatzCalculator {
  2. int computeStepCount(final int start) {
  3. if (start <= 0) throw new IllegalArgumentException("Only natural numbers are allowed");
  4. if (start == 1) return 0;
  5. final int next;
  6. if (start % 2 == 0) {
  7. next = start / 2;
  8. } else {
  9. next = 3 * start + 1;
  10. }
  11. return 1 + computeStepCount(next);
  12. }
  13. }