Difference between revisions of "Computing Prime Numbers"

From CompSciWiki
Jump to: navigation, search
Line 33: Line 33:
  
 
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.
 +
 +
<pre>
 +
for(int i = 2;i <= 20;i++)
 +
{
 +
  for(int j = 1;j < i;j++)
 +
  {
 +
 +
  }
 +
}
  
 
|SolutionCode=
 
|SolutionCode=
Line 61: Line 70:
 
           {
 
           {
 
             curPrime = false;  //this criminal is NOT a prime number
 
             curPrime = false;  //this criminal is NOT a prime number
           }
+
           } //if
         }
+
         } //for k
       }
+
       } //for j
 
        
 
        
 
       //print out each number if it is prime
 
       //print out each number if it is prime
Line 70: Line 79:
 
         System.out.print(i + " ");
 
         System.out.print(i + " ");
 
       }
 
       }
     }
+
     } //for i
 
     //it is good form to always end the line you print on
 
     //it is good form to always end the line you print on
 
     System.out.println();
 
     System.out.println();

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

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

  }
}

|SolutionCode=
<pre>
public class NewClass {
  public static void main(String args[])
  {
    boolean curPrime = 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++)
    {
      curPrime = 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)
          {
            curPrime = false;   //this criminal is NOT a prime number
          } //if
        } //for k
      } //for j
      
      //print out each number if it is prime
      if(curPrime)
      {
        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