fork(1) download
  1. import java.util.HashSet;
  2. import java.util.Scanner;
  3. import java.util.Set;
  4.  
  5. class PrimeFactorisation {
  6.  
  7. /**
  8.   * Code obtained from studyalgorithms.com
  9.   */
  10.  
  11. public static void main(String[] args) {
  12.  
  13. Scanner scanner = new Scanner(System.in);
  14. long N = scanner.nextLong();
  15. Set<Long> primeFactors = new HashSet<>();
  16.  
  17. while (N % 2 == 0) {
  18. N /= 2;
  19. primeFactors.add(2L);
  20. }
  21.  
  22. for (long j = 3; j <= Math.sqrt(N); j += 2) {
  23. while (N % j == 0) {
  24. N /= j;
  25. primeFactors.add(j);
  26. }
  27. }
  28.  
  29. if (N > 2)
  30. primeFactors.add(N);
  31.  
  32. for (Long primeFactor : primeFactors) {
  33. System.out.print(primeFactor + " ");
  34. }
  35.  
  36. }
  37. }
  38.  
Success #stdin #stdout 0.07s 4386816KB
stdin
45
stdout
3 5