Skip to content

Instantly share code, notes, and snippets.

@dfdemar
Last active December 19, 2015 10:19
Show Gist options
  • Save dfdemar/5940027 to your computer and use it in GitHub Desktop.
Save dfdemar/5940027 to your computer and use it in GitHub Desktop.
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