Solved

# Prime number counter

Posted on 2003-02-22

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