Difference between revisions of "Computing Prime Numbers"

From CompSciWiki
Jump to: navigation, search
Line 1: Line 1:
 
{{1010PrAD|ProblemName=Computing Prime Numbers
 
{{1010PrAD|ProblemName=Computing Prime Numbers
  
|Problem= It is often useful to compute prime numbers (e.g. Encryption).  Compute prime numbers up to 20 by use of nested loops.  This is a high order calculation, meaning that if you are calculating a lot of prime numbers you will have to wait some time for it to complete.
+
|Problem= It is often useful to compute prime numbers (e.g. Encryption).  Compute prime numbers up to 20 by use of nested loops.  This is a high order calculation, meaning that if you are calculating a lot of prime numbers you will have to wait some time for it to complete.  However, you can experiment with your program and see how long it takes to compute prime numbers up to 1,000, 10,000, or even 1,000,000.
  
 
Output should look like this:<BR>
 
Output should look like this:<BR>
Line 11: Line 11:
 
[[Image:Wiki_loops01.jpg|center]]<BR>
 
[[Image:Wiki_loops01.jpg|center]]<BR>
  
|Solution=You start by using a for loop that will count from 2 to 20.  We start at 2 for the simple fact that we know 1 is not a prime number but the product of 1 and itself is 1.  Everything we do inside this loop will be done on each number.
+
|Solution=Start by declaring a boolean that will determine if a given number is prime or not. Use a for loop that will count from 2 to 20.  Start at 2 for the simple fact that we know 1 is not a prime number but the product of 1 and itself is 1.  Everything we do inside this loop will be done on each number.
  
 
<pre>
 
<pre>
 +
boolean isPrime;
 +
 
forIint i = 2;i <= 20;i++)
 
forIint i = 2;i <= 20;i++)
 
{
 
{
Line 49: Line 51:
  
 
<pre>
 
<pre>
for(int k = 1;k <= j;k++)
+
for(int i = 2;i <= 20;i++)
 
{
 
{
   if(j*k == i)
+
   isPrime = true;          //number is innocent until proven guilty
 +
 
 +
  for(int j = 1;j < i;j++)
 
   {
 
   {
     pr
+
     for(int k = 1;k <= j;k++)
 +
    {
 +
      isPrime = false;    //this criminal is NOT a prime number
 +
    } //for k
 +
  } //for j
 +
} //for i
 +
</pre>
 +
 
 +
At this point we are almost done.  We just need to decide where to print each individual number.  Storing them in an array won't work since we, technically, don't know how many there are.  We must print them directly in the loop.  This is why I mentioned to label the for loops, when closing braces get further apart it can get hard where one level of scope ends in relation to another.
 +
 
 +
<pre>
 +
    } //for k
 +
  } //for j
 +
     
 +
  //print out each number if it is prime
 +
  if(isPrime)
 +
  {
 +
    System.out.print(i + " ");
 
   }
 
   }
}
+
} //for i
 +
</pre>
  
 
|SolutionCode=
 
|SolutionCode=
 
<pre>
 
<pre>
public class NewClass {
+
public class PrimeNumbers {
 
   public static void main(String args[])
 
   public static void main(String args[])
 
   {
 
   {

Revision as of 10:43, 8 April 2010

Back to the Program-A-Day homepage

Problem

It is often useful to compute prime numbers (e.g. Encryption). Compute prime numbers up to 20 by use of nested loops. This is a high order calculation, meaning that if you are calculating a lot of prime numbers you will have to wait some time for it to complete. However, you can experiment with your program and see how long it takes to compute prime numbers up to 1,000, 10,000, or even 1,000,000.

Output should look like this:
The prime numbers are: 2 3 5 7 11 13 17 19

 

Getting Started

Wiki loops01.jpg

Solution

Start by declaring a boolean that will determine if a given number is prime or not. Use a for loop that will count from 2 to 20. Start at 2 for the simple fact that we know 1 is not a prime number but the product of 1 and itself is 1. Everything we do inside this loop will be done on each number.

boolean isPrime;

forIint i = 2;i <= 20;i++)
{

}

Since any number that is only the product of 1 and itself we need a way to find the sum of all numbers that are less then itself. This will be to ensure that there are no other products that equal to that number. We can do this by use of a loop. J will always be less than i since we know that the product of i and 1 is to only way to have a product of i.

for(int i = 2;i <= 20;i++)
{
  for(int j = 1;j < i;j++)
  {

  }
}

The next step is to add a third for loop that will represent the second value of the product. To avoid repeating calculations we can say that the new value in the loop k must be less than or equal to k. Since we now have several levels of scope it would also be a good idea if we labeled our closing braces just so we know which is which.

for(int i = 2;i <= 20;i++)
{
  for(int j = 1;j < i;j++)
  {
    for(int k = 1;k <= j;k++)
    {
    } //for k
  } //for j
} //for i

In the for k loop we simply place an if statement in regards to each iteration of the loop. Remember, we want to check if our current value of i is the product of any two values that are described by the j and k loops.

for(int i = 2;i <= 20;i++)
{
  isPrime = true;          //number is innocent until proven guilty

  for(int j = 1;j < i;j++)
  {
    for(int k = 1;k <= j;k++)
    {
      isPrime = false;    //this criminal is NOT a prime number
    } //for k
  } //for j
} //for i

At this point we are almost done. We just need to decide where to print each individual number. Storing them in an array won't work since we, technically, don't know how many there are. We must print them directly in the loop. This is why I mentioned to label the for loops, when closing braces get further apart it can get hard where one level of scope ends in relation to another.

    } //for k
  } //for j
      
  //print out each number if it is prime
  if(isPrime)
  {
    System.out.print(i + " ");
  }
} //for i

Code

Solution Code

Back to the Program-A-Day homepage