Skip to content

Instantly share code, notes, and snippets.

@jdrew1303
Created November 24, 2012 00:23
Show Gist options
  • Save jdrew1303/4137815 to your computer and use it in GitHub Desktop.
Save jdrew1303/4137815 to your computer and use it in GitHub Desktop.
Calculating Primes
/**************************************************
* Basic program to check for primes (no optimisation)
*
* @author James Drew <[email protected]>
* @version 1.0
* @since 10-11-2012
*************************************************/
class Prime
{
public static void main (String[] args)
{
int number = 1000000;
//Prints all primes between 2 and 100
// 3, 5, 7, 9,....,97
for(int i = 3; i <= number; i++)
{
if (isPrime(i))
{
System.out.println(i);
}
}
}
/**************************************************
* Method to check for primes
*
* @param int number Takes an integer number as input
* @return boolean Returns a boolean that indicates if the numeber is prime
**************************************************/
//Is prime function
public static boolean isPrime(int number)
{
int i = 2;
if (number == 2)
{
return true;
}
while (i < number)
{
if ((number % i) == 0)
{
return false;
}
i++;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment