Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. package hr.fer.oop.lab1.prob4;
  2.  
  3. public class PrimeFactorization {
  4.  
  5. public static void main(String[] args) {
  6. if(args.length!=1) System.out.println("Invalid number of arguments!");
  7. else{
  8. System.out.printf("You requested decomposition of number %d into prime factors. Here they are:\n", Integer.parseInt(args[0]));
  9. decompose(Integer.parseInt(args[0]));
  10. }
  11. }
  12.  
  13.  
  14.  
  15. public static void decompose(int n){
  16. int k=1;
  17. // Print the number of 2s that divide n
  18. while (n%2 == 0){
  19. System.out.printf("%d. %d\n", k++, 2);
  20. n = n/2;
  21. }
  22. // n must be odd at this point. So we can skip
  23. // one element (Note i = i +2)
  24. for (int i = 3; i <= Math.sqrt(n); i = i+2){
  25. // While i divides n, print i and divide n
  26. while (n%i == 0){
  27. System.out.printf("%d. %d\n", k++, i);
  28. n = n/i;
  29. }
  30. }
  31. // This condition is to handle the case when n
  32. // is a prime number greater than 2
  33. if (n > 2)
  34. System.out.printf("%d. %d\n", k++, n);
  35.  
  36. }
  37.  
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement