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. public static ArrayList<Long> printDivisors(long n) {
  11. ArrayList<Long> divisors = new ArrayList<>();
  12.  
  13. for (long i = 1; i <= Math.sqrt(n); i++) {
  14. if (n % i == 0) {
  15. if (n / i == i) {
  16. divisors.add(i);
  17. } else {
  18. divisors.add(i);
  19. divisors.add(n / i);
  20. }
  21. }
  22. }
  23.  
  24. return divisors;
  25. }
  26. public static void main (String[] args) throws java.lang.Exception
  27. {
  28. long n = 15;
  29. ArrayList<Long> divisors = printDivisors(n);
  30.  
  31.  
  32. Collections.sort(divisors, Collections.reverseOrder());
  33.  
  34.  
  35. for (long divisor : divisors) {
  36. System.out.print(divisor + " ");
  37. }
  38. }
  39. }
Success #stdin #stdout 0.09s 55652KB
stdin
Standard input is empty
stdout
15 5 3 1