troubleshooting Question

Prime number counter

Avatar of Unreal7000
Unreal7000 asked on
Math / Science
51 Comments1 Solution5848 ViewsLast Modified:
I got really bored one day so I wrote this program that counts up prime numbers. First is C, second is TI-83 language. My challege is for people to find faster ways to find Prime numbers.

// *************************************************
// File Name:prime_1.c  
// Author: Samuel Dockery
// UserID: dockesa
// Date Created: September 28, 2002  
// Purpose: Compute Prime Numbers and Display them to stdout.  
//
// Notes: Compiled using Microsoft Visual C++ 6.0
// *************************************************

// Include Files
#include <stdio.h>  

// Global Constants
// None

// Global Types
// None

// Global Variables
// None

// Function Prototypes
// None


//****************************************************
// Function name:main  
// Author: Samuel Dockery
// Purpose: Compute Prime Numbers and Display them to stdout.
//  
// Input parameters: none  
// Output parameters: none  
// Returns:0  
// Error handling: none  
// Record of changes: none
//****************************************************

int main (void)
{

long halfPrime = 1;//Used to speed up process
unsigned long currentPrime = 2;//Current Prime number computer is calculating.
unsigned long smallNumber = 2;//Number used in checking to see if currentPrime is a prime number.

for (;;)//Loop infinitly.
     {
          if ( (currentPrime % smallNumber) != 0)//Checks to see if currentPrime is not evenly divisible.
               {          
               smallNumber++;//Increment smallNumber by 1.
               halfPrime = currentPrime/2;
               if (smallNumber >= halfPrime)//If smallNumber == currentPrime then it is a prime number.
                    {                                            
                    printf("%li\n", currentPrime);//Print prime number.
                    currentPrime++;//Increment currentPrime by 1.
                    smallNumber = 2;//Reset smallNumber to 2 to begin computing next prime number.
                    }//End If
               }
          else//Do following statement becuase currentPrime is not Prime.
               {    
               currentPrime++;//Increment currentPrime by 1.
               smallNumber = 2;//Reset smallNumber to 2 to begin computing next prime number.
               }//End If
     }    
return (0);// Return a zero value from the function to indicate successful processing
} // End main



--I also translated it into the TI-83 calculator language.--

ClrHome
1.e99->M
2->C
1->S
While C is less than or equal to M
If fPart(C/S)>0
Then
S+1->S
If (S=C)
Then
Disp C
C+1->C
2->S
End
Else
C+1->C
2->S
End
End
Stop
ASKER CERTIFIED SOLUTION
modulo

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 51 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 51 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros