fork(1) 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. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. int maxWidth = 72;
  13. int[] properDivisors = {
  14. 1,2,3,4,6,7,12,13,14,21,26,28,39,42,52,78,79,84,91,
  15. 156,158,182,237,273,316,364,474,546,553,948,1027,
  16. 1092,1106,1659,2054,2212,3081,3318,4108,6162,6636,
  17. 7189,12324,14378,21567,28756,43134,
  18. };
  19. String prefix = "...proper divisors: ";
  20. // Same number of chars as prefix, just all spaces.
  21. String emptyPrefix = prefix.replaceAll(".", " ");
  22. for (int i = 0; i < properDivisors.length; ++i) {
  23.  
  24. // Take as many of the items in the array as you can, without exceeding the
  25. // max line length.
  26. StringBuilder sb = new StringBuilder(prefix);
  27. for (; i < properDivisors.length; ++i) {
  28. int lengthBefore = sb.length();
  29.  
  30. // Append the next item, and comma, if it would be needed.
  31. sb.append(properDivisors[i]);
  32. if (i + 1 < properDivisors.length) sb.append(",");
  33.  
  34. if (sb.length() > maxWidth) {
  35. // Truncate back to the length before appending.
  36. sb.setLength(lengthBefore);
  37. break;
  38. }
  39. }
  40. System.out.println(sb);
  41.  
  42. // Blank out the prefix, so you will print leading spaces on next line.
  43. prefix = emptyPrefix;
  44. }
  45. }
  46. }
Success #stdin #stdout 0.04s 2184192KB
stdin
Standard input is empty
stdout
...proper divisors: 1,2,3,4,6,7,12,13,14,21,26,28,39,42,52,78,79,84,91,
                    158,182,237,273,316,364,474,546,553,948,1027,1092,
                    1659,2054,2212,3081,3318,4108,6162,6636,7189,12324,
                    21567,28756,43134