Created
May 4, 2016 21:00
-
-
Save edalorzo/f7fabcf898b4a2dce6e3b44dacaf5bae to your computer and use it in GitHub Desktop.
Largest Factor of a Number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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