Tuesday, January 8, 2013

Problem 3: Largest prime factor


Q)The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

Solution: This java code gives the solution.


public class Prob3 {

 
 public static void main(String[] args) {
  long n;
  long icopy= 2;
  n = 600851475143L;
  boolean flag = true ;
  
for(long i =3 ; i < Math.sqrt(n) ; i++ )
{
 if(n % i == 0 )
 {
  for(long j = 2; j <= Math.sqrt(i) ; j++)
  {
   if (i%j==0)
    flag = false;
  }
 if (flag)
 {
  
  icopy = i;
  
 }
}
 
}
System.out.println(icopy); 
  }
  

 }



No comments:

Post a Comment