fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4.  
  5. static HashMap<Long, Long> primeFactors(long n) {
  6. HashMap<Long, Long> map = new HashMap<>();
  7.  
  8. while (n % 2 == 0) {
  9. map.put(2L, map.getOrDefault(2L, 0L) + 1);
  10. n /= 2;
  11. }
  12.  
  13. long lim = (long) Math.sqrt(n);
  14. for (long i = 3; i <= lim; i += 2) {
  15. while (n % i == 0) {
  16. map.put(i, map.getOrDefault(i, 0L) + 1);
  17. n /= i;
  18. }
  19. }
  20.  
  21. if (n > 2) map.put(n, map.getOrDefault(n, 0L) + 1);
  22.  
  23. return map;
  24. }
  25.  
  26. public static void main(String[] args) {
  27. long n = 18;
  28. HashMap<Long, Long> res = primeFactors(n);
  29. for (Map.Entry<Long, Long> e : res.entrySet()) {
  30. System.out.println(e.getKey() + " " + e.getValue());
  31. }
  32. }
  33. }
  34.  
Success #stdin #stdout 0.18s 55740KB
stdin
Standard input is empty
stdout
2 1
3 2