Skip to content

Instantly share code, notes, and snippets.

@edalorzo
Created May 4, 2016 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edalorzo/f7fabcf898b4a2dce6e3b44dacaf5bae to your computer and use it in GitHub Desktop.
Save edalorzo/f7fabcf898b4a2dce6e3b44dacaf5bae to your computer and use it in GitHub Desktop.
Largest Factor of a Number
public class Factors {
public static long factors(long n) {
long max = 0;
long d = 2;
while (n > 1) {
while (n % d == 0) {
max = Math.max(max, d);
n /= d;
}
d = d + 1;
if (d * d > n) {
if (n > 1) {
max = Math.max(max, n);
}
break;
}
}
return max;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment