Skip to main content
4 of 6
deleted 11 characters in body
Alnitak
  • 441
  • 2
  • 7

Here's my effort, which solves in ~240µs on an i7 iMac:

private long highest;

private long test(long n, long f) {
    if (n < f) return n;
    while (n % f == 0) {
        n /= f;
        if (f > highest) {
            highest = f;
        }
    }

    return n;
}

public long find(long n) {
    highest = 1;
    n = test(n, 2);
    n = test(n, 3);
    if (n >= 5) {
        for (long i = 5; n != 1 && (i * i) <= n; i += 6) {
            n = test(n, i);
            n = test(n, i + 2);
        }
    }
    return (n == 1) ? highest : n;
}

This is a corrected version of @thepace's algorithm with special case tests for 2, 3, and then 6n - 1 and 6n + 1

Alnitak
  • 441
  • 2
  • 7