## Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

# Obtain the largest prime factor of a given number in Java

Obtain the largest prime factor of a given number - Java:

``````
import java.io.*;

public class LargestPrimeFactor {
/**
* This method Obtain the largest prime factor of a given number.
* @param number the number to find the largest prime factor.
* @return int Obtains the largest prime factor of a given number.
*/
public int largestPrimeFactor(int number) {
//Variable to store the largest prime factor
int largestPrimeFactor = 0;
//Variable to store the current prime factor
int currentPrimeFactor = 0;
//Variable to store the current number
int currentNumber = 0;
//Variable to store the current remainder
int currentRemainder = 0;
//Variable to store the current divisor
int currentDivisor = 0;
//Variable to store the current quotient
int currentQuotient = 0;
//Variable to store the current remainder
int currentRemainder = 0;
//Variable to store the current divisor
int currentDivisor = 0;
//Variable to store the current quotient
int currentQuotient = 0;
//Variable to store the current remainder
int currentRemainder = 0;
//Variable to store the current divisor
int currentDivisor = 0;
//Variable to store the current quotient
int currentQuotient = 0;

//Set the current number to the given number
currentNumber = number;

//Set the current prime factor to 2 (the smallest prime factor)
currentPrimeFactor = 2;

//While the current number is greater than 1, continue looping.
while (currentNumber > 1) {

//Set the current remainder to the modulus of the current number and the current prime factor.
currentRemainder = currentNumber % currentPrimeFactor;

//If the remainder is equal to zero, then the number is divisible by the prime factor.  Set the largest prime factor to the prime factor.  Set the current number to the quotient of the number and the prime factor.  Set the current prime factor to 2 (the smallest prime factor).  Continue looping.
if (currentRemainder == 0) {

largestPrimeFactor = currentPrimeFactor;

currentNumber = currentNumber / currentPrimeFactor;

currentPrimeFactor = 2;

continue;

}

//Set the current divisor to 2 (the smallest divisor) and set the current quotient to 1.  Continue looping.
currentDivisor = 2;

currentQuotient = 1;

//While the quotient is greater than 1, continue looping.  Set the quotient to modulus of the quotient and the divisor.  Set the divisor to add 1 to the divisor.  Continue looping.
while (currentQuotient > 1) {

//Set the quotient to modulus of the quotient and the divisor.  Set the divisor to add 1 to the divisor.  Continue looping.
currentQuotient = (currentQuotient % currentDivisor);

currentDivisor++;

}

//Set the remainder to modulus of the remainder and divisor.  Set the divisor to add 1 to the divisor.  Continue looping.
currentRemainder = (currentRemainder % (currentDivisor));

currentDivisor++;

//If remainder is equal to zero, then set largest prime factor to divisor and set remainder and number to quotient and divisor respectively.  Continue looping.
if (currentRemainder == 0) {

largestPrimeFactor = currentDivisor;

currentRemainder = currentQuotient;

currentNumber = currentDivisor;

continue;

}

//Set the prime factor to add 1 to prime factor and continue looping.
currentPrimeFactor++;

}

return largestPrimeFactor;

}

/**
* One Example
*/
public static void main(String args[]) throws IOException {

LargestPrimeFactor  largestPrimeFactor= new LargestPrimeFactor ();
//Example
int largestPrimeFactorOfNumber = largestPrimeFactor.largestPrimeFactor(600851475143);
//Output
System.out.println("Largest Prime Factor of 600851475143 is :" + largestPrimeFactorOfNumber); //6857

}
}
``````