Last active
December 19, 2015 10:19
-
-
Save dfdemar/5940027 to your computer and use it in GitHub Desktop.
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
public class Main | |
{ | |
public static List<Integer> primes = new ArrayList<Integer>(); | |
public static void main(String[] args) | |
{ | |
findPrimes(100); | |
for(int prime : primes) | |
System.out.println(prime); | |
} | |
public static void findPrimes(int maxNumber) | |
{ | |
if (maxNumber >= 2) | |
{ | |
primes.add(2); | |
int currentNumber = 3; | |
while (currentNumber <= maxNumber) | |
{ | |
boolean isPrime = true; | |
for (int prime : primes) | |
{ | |
if (currentNumber % prime == 0) | |
{ | |
isPrime = false; | |
break; | |
} | |
} | |
if (isPrime == true) | |
primes.add(currentNumber); | |
currentNumber += 2; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment