Difference between revisions of "Computing Prime Numbers"

From CompSciWiki
Jump to: navigation, search
Line 32: Line 32:
 
</pre>
 
</pre>
  
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.
+
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.
  
 
<pre>
 
<pre>
Line 39: Line 39:
 
   for(int j = 1;j < i;j++)
 
   for(int j = 1;j < i;j++)
 
   {
 
   {
 +
    for(int k = 1;k <= j;k++)
 +
    {
 +
    } //for k
 +
  } //for j
 +
} //for i
 +
</pre>
  
 +
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.
 +
 +
<pre>
 +
for(int k = 1;k <= j;k++)
 +
{
 +
  if(j*k == i)
 +
  {
 +
    pr
 
   }
 
   }
 
}
 
}
Line 48: Line 62:
 
   public static void main(String args[])
 
   public static void main(String args[])
 
   {
 
   {
     boolean curPrime = true;    //check to see if it's true
+
     boolean isPrime = true;    //check to see if it's true
  
 
     //we will be printing out the numbers later
 
     //we will be printing out the numbers later
Line 58: Line 72:
 
     for(int i=2;i<=20;i++)
 
     for(int i=2;i<=20;i++)
 
     {
 
     {
       curPrime = true;          //number is innocent until proven guilty
+
       isPrime = true;          //number is innocent until proven guilty
  
 
       //loop to seek first number
 
       //loop to seek first number
Line 69: Line 83:
 
           if(j*k == i)
 
           if(j*k == i)
 
           {
 
           {
             curPrime = false;  //this criminal is NOT a prime number
+
             isPrime = false;  //this criminal is NOT a prime number
 
           } //if
 
           } //if
 
         } //for k
 
         } //for k
Line 75: Line 89:
 
        
 
        
 
       //print out each number if it is prime
 
       //print out each number if it is prime
       if(curPrime)
+
       if(isPrime)
 
       {
 
       {
 
         System.out.print(i + " ");
 
         System.out.print(i + " ");

Revision as of 10:27, 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.

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

 

Getting Started

Wiki loops01.jpg

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.

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 k = 1;k <= j;k++)
{
  if(j*k == i)
  {
    pr
  }
}

|SolutionCode=
<pre>
public class NewClass {
  public static void main(String args[])
  {
    boolean isPrime = true;    //check to see if it's true

    //we will be printing out the numbers later
    //we just don't want to have this print in the loop
    System.out.print("The prime numbers are: ");
    
    //the for loop for checking each number
    //you can omit 1 because it is not a prime number!
    for(int i=2;i<=20;i++)
    {
      isPrime = true;          //number is innocent until proven guilty

      //loop to seek first number
      for(int j=1;j<i;j++)
      {
        //loop to seek second number
        for(int k=1;k<=j;k++)
        {
          //if both numbers equal i
          if(j*k == i)
          {
            isPrime = false;   //this criminal is NOT a prime number
          } //if
        } //for k
      } //for j
      
      //print out each number if it is prime
      if(isPrime)
      {
        System.out.print(i + " ");
      }
    } //for i
    //it is good form to always end the line you print on
    System.out.println();
  }
}

Code

SolutionCode goes here. Please DO NOT put your code in <pre> tags!

Back to the Program-A-Day homepage