Skip to main content
added 25 characters in body
Source Link
Alnitak
  • 441
  • 2
  • 7

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

private long highest;

// checks if `f` is a factor of `n`,
// returning divided `n` accordingly
private long testfactorize(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;

    // check the two simplest cases
    n = testfactorize(n, 2);
    n = testfactorize(n, 3);

    // and then all numbers in the form 6x - 1 and 6x + 1
    if (n >= 5) {
        for (long i = 5; i * i <= n; i += 6) {
            n = testfactorize(n, i);
            n = testfactorize(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

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

private long highest;

// checks if `f` is a factor of `n`,
// returning divided `n` accordingly
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;

    // check the two simplest cases
    n = test(n, 2);
    n = test(n, 3);

    // and then all numbers in the form 6x - 1 and 6x + 1
    if (n >= 5) {
        for (long i = 5; 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

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

private long highest;

// checks if `f` is a factor of `n`,
// returning divided `n` accordingly
private long factorize(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;

    // check the two simplest cases
    n = factorize(n, 2);
    n = factorize(n, 3);

    // and then all numbers in the form 6x - 1 and 6x + 1
    if (n >= 5) {
        for (long i = 5; i * i <= n; i += 6) {
            n = factorize(n, i);
            n = factorize(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

deleted 11 characters in body
Source Link
Alnitak
  • 441
  • 2
  • 7

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

private long highest;

// checks if `f` is a factor of `n`,
// returning divided `n` accordingly
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; 

    // check the two simplest cases
    n = test(n, 2);
    n = test(n, 3);

    // and then all numbers in the form 6x - 1 and 6x + 1
    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

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

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

private long highest;

// checks if `f` is a factor of `n`,
// returning divided `n` accordingly
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; 

    // check the two simplest cases
    n = test(n, 2);
    n = test(n, 3);

    // and then all numbers in the form 6x - 1 and 6x + 1
    if (n >= 5) {
        for (long i = 5; 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

deleted 11 characters in body
Source Link
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, j = 7;5; n != 1 && (i * i) <= n; i += 6, j += 6) {
            n = test(n, i);
            n = test(n, ji + 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

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, j = 7; n != 1 && (i * i) <= n; i += 6, j += 6) {
            n = test(n, i);
            n = test(n, j);
        }
    }
    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

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

added 10 characters in body
Source Link
Alnitak
  • 441
  • 2
  • 7
Loading
deleted 172 characters in body
Source Link
Alnitak
  • 441
  • 2
  • 7
Loading
Source Link
Alnitak
  • 441
  • 2
  • 7
Loading