Skip to content

Instantly share code, notes, and snippets.

@oziguerra
Last active August 29, 2015 14:26
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 oziguerra/86fd02cac326154666c2 to your computer and use it in GitHub Desktop.
Save oziguerra/86fd02cac326154666c2 to your computer and use it in GitHub Desktop.
public class PrimeNumbers
{
public final static int MIN = 1;
public final static int MAX = 1000;
public static void main(String[] args)
{
int count = primeRange(MIN, MAX);
System.out.println(count + " prime numbers found in the range from " + MIN + " to " + MAX);
}
public static int primeRange(int min, int max)
{
int counter = 0;
for (int number = 1; number < max; number += 2)
{
if (isPrime(number))
{
counter++;
}
}
return counter;
}
private static boolean isPrime(int n)
{
final int limit = n / 2;
for (int i = 3; i < limit; i += 2)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment