Largest prime factor - Java Algorithm

Java examples for Algorithm:Number

Introduction

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

Demo Code


package number;/*ww  w  . ja  va2  s. co m*/

public class LargestPrimeFactor {
  public static long largestPrimeFactor(long n){
    int i = 2;
    while(i != n){
      if(n%i == 0)
        n = n/i;
      else 
        i++;
    }
    return n;
  }
  public static void main(String[] args) {
    System.out.println(largestPrimeFactor(600851475143L));
  }
}

Related Tutorials