Q:

Write a method that calculates the largest prime factor of a given number

belongs to collection: java exercises (hard level )

0

The prime factors of 455 are 5, 7 and 13.
Write a method that calculates the largest prime factor of a given number.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

public Integer largestPrimeFactor(Integer n) {
int factor = -1;
for (int i = 2; i * i <= n; i++) {
    if (n == 1) { break; }
    if (n % i != 0) { continue; }
    factor = i;
    while (n % i == 0) {
        n /= i;
    }
}
return n == 1 ? factor : n;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

write a method that returns the minimum number of ... >>
<< Given two strings, write a method that finds the l...