Created
December 27, 2011 04:00
-
-
Save janoulle/1522663 to your computer and use it in GitHub Desktop.
Problem 5 - Project Euler in Java by Jane Ullah
This file contains 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
/** | |
* @author Jane Ullah | |
* @purpose Problem 5 - Project Euler | |
* @date 12/26/2011 | |
* @site http://janetalkscode.com | |
*/ | |
public class Problem5 { | |
public static void main(String[] args) { | |
int sum = 1, primeNum = 0; | |
for (int i = 1; i <= 20; i++) { | |
primeNum = Prime(i); | |
if (primeNum > 0) { | |
sum *= primeNum; | |
} | |
while ( primeNum < 0 && !(sum%i == 0) ) { | |
//Multiply with 2 or 3. Between 18 and 20, the divisors to worry | |
//about are: 2, 3, 5 (5 is a prime). | |
if ((sum*2)%i == 0) { | |
sum *= 2; | |
} | |
else if ((sum*3)%i == 0) { | |
sum *= 3; | |
} | |
} | |
} | |
System.out.println("Smallest Divisible Number: " + sum); | |
} | |
//Method to find out if the argument is a prime number; | |
//Returns the argument if the number is a prime and returns -1 if the argument is not a prime. | |
private static int Prime(int num) { | |
int j; | |
if (num == 2 || num == 3) {return num; } | |
for (j = 0; (j <= num/2) && ( !((num % (j+2)) == 0 )) ; j++); | |
return (j > num/2) ? num:-1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you please explain the code, especially the while part