I hope its okay to post this here, the forum section is simply "C++ programming" so I hope it is ok here with all the other (much more advanced) topics. There didn't seem to be a 'newbie' area.

This is probably laughable to the vast majority of you, but hey?

I enjoyed doing it and it seems to work just fine :)

If anyone wants to tell me how I SHOULD have done it, absolutely fine. If noone wants to bother reading it (I appreciate, that it is probably akin to writing ones name for most of you), also absolutely fine :) I just thought I would post it and see if anyone wants to comment.

Code:`/* A Simple Program for caluclating prime numbers`

(my first attempt at programming beyond the "Hello World" phase)

USAGE

*****

Defines and counts prime numbers between 1 and upper_limit, where upper_limit is set by the user.

LIMITATIONS

***********

Presumably, there must be some? I did not test with a truly huge upper limit

so at present I am unaware as to what they are.

Notes

*****

This is my very first attempt to code something of entirely my own work. I

am approximately 200 pages into my 1st C++ primer (Practical C++ by Steve Ouline - second hand no less!) and was getting a little bored with his programming exercises. I therefore set my own for this evening.

I have never seen any source code for a program to calculate prime numbers. I have no idea if this way is efficient or inefficient (given my lack of knowledge and experience one would assume the latter!).

If this program/source code ever sees happens to see the light of day any comments - critical, constuctive or otherwise would be very gratefully recieved.

Author

******

Stewart Robertson (Iclestu)

iclestu@supanet.com

Version

*******

0.1 2nd November 2005

Licence??

*********

Yeah right!!??!!

If anything is of any remote use to anyone (it IS possible - perhaps it's so badly written that a lecturer or tutor wants it as an example of what not to do!), please feel free to copy, distribute, amend, butcher, destroy, recreate, emulate and re-copy to your heart's content.

*/

#include <iostream.h>

long int upper_limit; //highest number progrm will go up to (inputed by user)

long int current; //current number 'on test'

int counter; //keeps count of number of primes

int prime_test(long int candidate)//Function returns 1 if candidate is prime, 0 otherwise

{

int divisor; //stores current divisor

int return_value; //stores return value (should be 1 if no divisor is found) otherwise 0

return_value = 1; // will only be amended to 0 if a divisor is found

divisor = 2; // initialise

if ((candidate != 1) && (candidate != 2)) // no need to test these 2 and they don't quite fit into the main loop

{

while (divisor <= (candidate/2))

{

if ((candidate % divisor) == 0)

{

return_value = 0;

break; //no sense continuing the WHILE loop if even 1 divisor is found

}

else

{

divisor++; // It seems long-winded to test every number up to "candidate/2", but how else?

continue;

}

}

}

return (return_value);

}

int main()

{

/*Intro*/

cout << "Prime Version 0.1\n";

cout << "Author: Stewart Robertson (Iclestu)\n";

cout << "Comments to: iclestu@supanet.com\n\n";

/*This Section to obtain upper limit from user*/

cout << "Please enter upper limit to calculate to:";

cin >> upper_limit;

cout << "\n";

/*This section is main loop*/

current = 1; //might as well start at the beginning :)

while (current <= upper_limit)

{

if (prime_test(current) == 1) //test if prime

{

cout << current << "\n"; //if so output number

counter++; // and increment counter

}

current++;

}

/* this section to close and display info*/

cout << "\n" << counter << " prime numbers found up to " << upper_limit;

cout << ".\n\n\n";

cout << "Prime Version 0.1\n";

cout << "Author: Stewart Robertson (Iclestu)\n";

cout << "Comments to: iclestu@supanet.com\n\n";

}