# greatest prime factor

• 07-23-2010, 01:29 PM
java_prgr
greatest prime factor
hi, i have seen ur post of gpf, just have a few ques. is the following approach ok: first find all the factors, then test which of the factors r prime, and then find the largest of those factors??

hope to hear from you soon.
• 07-23-2010, 04:23 PM
JosAH
Quote:

Originally Posted by java_prgr
hi, i have seen ur post of gpf, just have a few ques. is the following approach ok: first find all the factors, then test which of the factors r prime, and then find the largest of those factors??

hope to hear from you soon.

Why not find prime factors only? Then the greatest factor you'll find will also be the greatest prime factor.

kind regards,

Jos
• 07-23-2010, 08:28 PM
java_prgr
i have found all the prime factors. r u saying that the last prime factor will be the largest prime factor? my code is:
import java.util.Scanner;

public class GPF {
public static void main(String[] srgs) {
Scanner keyboard = new Scanner(System.in);

int num,prifac,count=0;

System.out.print("Enter a number: ");
num = keyboard.nextInt();

for(int i=1;i<num;i++){
if(num%i == 0){
for(prifac=2;prifac<i;prifac++){
if(i%prifac == 0){
count++;
}
}
if(count<=0){

System.out.print(i+"\t");
}
count =0;
}
}
}
}

wat shud i add to ensure that the last prime factor is the largest and shud be printed?
thnx