fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4.  
  5. static ArrayList<Long> divisors(long n) {
  6. ArrayList<Long> list = new ArrayList<>();
  7. long limit = (long) Math.sqrt(n);
  8. for (long i = 1; i <= limit; i++) {
  9. if (n % i == 0) {
  10. list.add(i);
  11. if (n / i != i) list.add(n / i);
  12. }
  13. }
  14. return list;
  15. }
  16.  
  17. public static void main(String[] args) {
  18. ArrayList<Long> d = divisors(12);
  19. for (int i = d.size() - 1; i >= 0; i--) {
  20. System.out.print(d.get(i) + " ");
  21. }
  22. }
  23. }
  24.  
Success #stdin #stdout 0.14s 57452KB
stdin
Standard input is empty
stdout
4 3 6 2 12 1