fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. private static List<Integer> getHailstoneSeq(int n) {
  12. ArrayList<Integer> results = new ArrayList<Integer>();
  13. results.add(n);
  14. if (n == 1) return results;
  15.  
  16. int next;
  17. if (n % 2 == 0) next = n / 2;
  18. else next = 3*n + 1;
  19. results.add(next);
  20.  
  21. while (next != 1) {
  22. if (next % 2 == 0) next = next / 2;
  23. else next = 3*next + 1;
  24. results.add(next);
  25. }
  26. return results;
  27. }
  28.  
  29.  
  30. public static void main (String[] args) throws java.lang.Exception
  31. {
  32. int n = 15;
  33. System.out.println(getHailstoneSeq(n));
  34.  
  35. }
  36. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
[15, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1]