Third problem from project Euler.
Quite easy this one. Feedback on better ways to do these are most welcome!
Question:
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?.
Answer:
public class problem3 {
 private static final long largenumber = 600851475143L;
 public static void main(String[] args) {
  problem3 p = new problem3();
  for(int i = 2; i < 10000; i++){
   if(p.isPrime(i)){
    if(largenumber % i == 0){
     System.out.println(i);
    }
   }
  }
 }
 
 boolean isPrime(int n) {
     for(int i=2; 2*i< n; i++)
         if(n%i==0)
             return false;
     }
     return true;
 }
}
No comments:
Post a Comment