Q:

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

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)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now