fork download
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. class AchaPrimos {
  5.  
  6. public static boolean primo(int num) {
  7. for (int i = 2; i < num; i++) {
  8. if (num % i == 0) return false;
  9. }
  10.  
  11. return true;
  12. }
  13.  
  14. public static List<Integer> fatorar(int x) {
  15. List<Integer> numeros = new ArrayList<Integer>();
  16. int aux = x, s = (int) Math.sqrt(x);
  17.  
  18. for (int i = 2; i <= s; i++) {
  19. if (primo(i)) {
  20. while (aux % i == 0) {
  21. aux /= i;
  22. s = (int) Math.sqrt(aux);
  23. numeros.add(i);
  24. }
  25. }
  26. }
  27.  
  28. if (aux != 1 || numeros.isEmpty()) numeros.add(aux);
  29. return numeros;
  30. }
  31.  
  32. public static void main(String[] args) {
  33. System.out.println("36: " + fatorar(36));
  34. System.out.println("60: " + fatorar(60));
  35. System.out.println("120: " + fatorar(120));
  36. System.out.println("144: " + fatorar(144));
  37. System.out.println("97: " + fatorar(97));
  38. System.out.println("128: " + fatorar(128));
  39. System.out.println("15: " + fatorar(15));
  40. System.out.println("2: " + fatorar(2));
  41. System.out.println("7: " + fatorar(7));
  42. System.out.println("1: " + fatorar(1));
  43. System.out.println("0: " + fatorar(0));
  44. }
  45. }
Success #stdin #stdout 0.05s 4386816KB
stdin
Standard input is empty
stdout
36: [2, 2, 3, 3]
60: [2, 2, 3, 5]
120: [2, 2, 2, 3, 5]
144: [2, 2, 2, 2, 3, 3]
97: [97]
128: [2, 2, 2, 2, 2, 2, 2]
15: [3, 5]
2: [2]
7: [7]
1: [1]
0: [0]