You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Farrukh Ahmad edited this page Dec 15, 2015
·
1 revision
Lets list out all prime factors of a given number
public class PrimeFactors {
public static void main(String args[]){
int n=123;
System.out.println(getAllPrimeFactors(n));
}
static List<Integer> getAllPrimeFactors(int n){
List<Integer> result = new ArrayList<>();
if(n==1) {
result.add(n);
return result;
}
int i = 2;
while(n > 1)
{
while( i <= n)
{
if( n % i == 0)
{
result.add(i);
n = n / i;
continue;
}
else
i++;
}
n = n / i;
}
return result;
}
}