|
|
|
|
![]() ![]() |
Mar 23 2006, 12:46 AM
Post
#1
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 2 Joined: 9-September 05 Member No.: 8,407 |
I've been interested in learning C++ lately. I used to program in BASIC, but I've recently decided that I would begin to learn a more challenging language. I read online tutorials, and bought a book. Using only very basic C++, I wrote a one-file program that generates higher and higher prime numbers. I was always interested in how high prime numbers would get before they became very scarce.
My program tries to generate prime numbers with maximum efficiency. It uses test division on a constantly increasing integer to determine if it is prime or not, and spits out the ones that test to be truly prime. It only tests odd numbers, and therefore only has to test divide by odd numbers. It also numbers the primes. I'll post the source here. Any feedback is appreciated, but remember: I'm just a beginner. CODE #include <iostream>
int testForPrime (int n) { int p, i; p = 1; i = 3; int result = n; while (i < result) { result = n / i; if (n == i * result) { p = 0; i = n; } i = i + 2; } return (p); } int main (int argc, char * const argv[]) { int p, i, n; i = 3; n = 5; std::cout << "Initiating prime number generation sequence...\n\n1: 2\n2: 3\n"; while (1) { p = testForPrime (n); if (p == 1) { std::cout << i << ": " << n << "\n"; i++; } n = n + 2; } return 0; } This post has been edited by lalhsboard: Mar 23 2006, 12:48 AM |
|
|
|
Mar 23 2006, 02:26 AM
Post
#2
|
|
|
Techno-Necromancer Group: Members Posts: 1,018 Joined: 13-January 05 From: The Net Member No.: 2,127 |
I seehttp://www.astahost.com/style_images/1/folder_rte_images/rte_dots.gif
http://www.astahost.com/style_images/1/fol...es/rte_dots.gif only a few problems code wise with your program. The first is the type that you defined your testForPrime() function to be. Since it only returns a 1 or a zero it is essentially functioning as a boolean function, so I would define it as such. Then, you would define p to true and adjust it to false. This would simplify your if statements when you check if something is prime. You could also embed a return statement into the if statement that checks for perfect divisions to prevent the whole while loop from running and dcrease clutter. Also, you defined a while loop with one as the value, which although works, would be better defined as while(true). Also, it is general good programming practice to not code infinite loops, so I would recommend adding an exit prompt to the while statement, but that is merely a choice I would make. I would also deifne the variables that have specific meanings to words or abbreviations with meaning to help you remember what they are if you ever go back to modify this code or include it in a later program. So the revisions I have for your code would look like this: CODE #include <iostream> bool TestForPrime (int value) { int testvalue = 3; result = value; while (testvalue < value) { result = value / testvalue; if (value == testvalue * result) { return false; } testvalue += 2; } return true; } int main (int argc, char * const argv[]) { int count = 3; int value = 5; std::cout << "Initiating prine number generation sequence...\n\n1: 2\n2: 3\n"; while (true) { if (testForPrime(n)) { std::cout << count << ": " << value << "\n"; count++; } value += 2; } return 0; } Just to let you know hoever, the algorithm you are using is by far not the most efficient algorithm for finding prime numbers, and most of the efficient ones are extremely complex. If you are really interested in looking at when prime numbers become scarce, you may be interested in the Zeta function and the associated millenium problem. ~Viz |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 22nd November 2008 - 11:55 PM |