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.
+
|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:<BR>
 +
The prime numbers are: 2 3 5 7 11 13 17 19
  
 
|SideSectionTitle=Getting Started
 
|SideSectionTitle=Getting Started
Line 9: 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 1 to 20.  Everything we do inside this loop will be done on each number.
+
|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.
  
 
<pre>
 
<pre>
forIint i = 1;i <= 20;i++)
+
forIint i = 2;i <= 20;i++)
 
{
 
{
  
Line 18: Line 20:
 
</pre>
 
</pre>
  
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.
+
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.
  
 
<pre>
 
<pre>
for(int i = 1;i <= 20;i++)
+
for(int i = 2;i <= 20;i++)
 
{
 
{
   for(int j = 1;j<i;j++)
+
   for(int j = 1;j < i;j++)
 
   {
 
   {
  
Line 30: Line 32:
 
</pre>
 
</pre>
  
The next step is to add a third for loop
+
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.
  
 
|SolutionCode=
 
|SolutionCode=
Line 53: Line 55:
 
       {
 
       {
 
         //loop to seek second number
 
         //loop to seek second number
         for(int k=0;k<=j;k++)
+
         for(int k=1;k<=j;k++)
 
         {
 
         {
 
           //if both numbers equal i
 
           //if both numbers equal i

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

Code

Solution Code

Back to the Program-A-Day homepage