Skip to content

Instantly share code, notes, and snippets.

@janoulle
Created December 20, 2011 00:31
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 janoulle/1499599 to your computer and use it in GitHub Desktop.
Save janoulle/1499599 to your computer and use it in GitHub Desktop.
Problem 3 - Project Euler in Java by Jane Ullah
/**
*
* @author Jane Ullah
* @purpose Problem 3 - Project Euler
* @date 12/19/2011
*
*/
public static void main(String[] args) {
long primeNumber = 600851475143L, divisor = 0L, primeDivisor = 0L;
long controller = (long) Math.floor(Math.sqrt(primeNumber));
for (long i = 2L; i <= controller; i++ ) {
//Catching all divisors of the number
if (primeNumber % i == 0) {
divisor = i;
//Checking for prime divisors.
if (divisorTest(divisor)) {
primeDivisor = divisor;
}
}
}
System.out.println("Largest Prime Divisor: " + primeDivisor);
}
//Method to test the 'prime-ness' of the divisors
private static boolean divisorTest(long number){
long j = 0;
if (number == 2) { return true; }
for (j = 0; (j <= number/2) && ( !((number % (j+2)) == 0 )) ; j++);
return (j >= number/2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment