LeetCode: Prime Factors

/**
Given a number n, the task of the programmer is to print the factors of the number in such a way that they occur in pairs. A pair signifies that the product of the pair should result in the number itself;

Examples:

Input : 24
Output : 1*24
         2*12
         3*8
         4*6

Input : 50
Output : 1*50
         2*25
         5*10
*/
import java.util.*;

public class PrimeFactors{

//prints only two factors (e.g.,20= 4*5, instead of 20=2*2*5)
public static void printTwoFactors(int n){

for(int i=1; i*i<n; i++){
if(n%i==0){
System.out.println(i+"*"+(n/i));
}
}
}

public static void main(String args[]){
printTwoFactors(24);
printTwoFactors(50);
}
}

No comments:

Post a Comment