Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hr.fer.oop.lab1.prob4;
- public class PrimeFactorization {
- public static void main(String[] args) {
- if(args.length!=1) System.out.println("Invalid number of arguments!");
- else{
- System.out.printf("You requested decomposition of number %d into prime factors. Here they are:\n", Integer.parseInt(args[0]));
- decompose(Integer.parseInt(args[0]));
- }
- }
- public static void decompose(int n){
- int k=1;
- // Print the number of 2s that divide n
- while (n%2 == 0){
- System.out.printf("%d. %d\n", k++, 2);
- n = n/2;
- }
- // n must be odd at this point. So we can skip
- // one element (Note i = i +2)
- for (int i = 3; i <= Math.sqrt(n); i = i+2){
- // While i divides n, print i and divide n
- while (n%i == 0){
- System.out.printf("%d. %d\n", k++, i);
- n = n/i;
- }
- }
- // This condition is to handle the case when n
- // is a prime number greater than 2
- if (n > 2)
- System.out.printf("%d. %d\n", k++, n);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement