|
|
|
| Web Hosting |
![]() ![]() |
Prime Number Generator |
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 |
|
|
|
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
4 | Captain Jerry | 1,362 | 12th November 2008 - 11:32 PM Last post by: Guest |
|||
![]() |
15 | CrazyPensil | 1,706 | 14th July 2008 - 10:02 AM Last post by: iGuest |
|||
![]() |
0 | TavoxPeru | 421 | 13th May 2008 - 06:52 AM Last post by: TavoxPeru |
|||
![]() |
10 | ganeshn11 | 1,723 | 21st April 2008 - 11:15 AM Last post by: Jared |
|||
![]() |
8 | yordan | 1,736 | 19th April 2008 - 11:37 PM Last post by: Jared |
|||
![]() |
3 | Feelay | 504 | 1st March 2008 - 03:51 AM Last post by: TavoxPeru |
|||
![]() |
1 | CaptainRon | 2,570 | 18th January 2008 - 06:02 AM Last post by: iGuest |
|||
![]() |
2 | TavoxPeru | 638 | 25th November 2007 - 01:18 AM Last post by: TavoxPeru |
|||
![]() |
4 | vujsa | 626 | 9th November 2007 - 11:40 PM Last post by: vizskywalker |
|||
![]() |
0 | al-rafideen | 953 | 19th October 2007 - 09:53 AM Last post by: al-rafideen |
|||
![]() |
2 | Jimmy89 | 579 | 16th October 2007 - 08:28 PM Last post by: tansqrx |
|||
![]() |
1 | mtnbluet | 1,453 | 21st July 2007 - 01:53 AM Last post by: TavoxPeru |
|||
![]() |
6 | miCRoSCoPiC^eaRthLinG | 1,711 | 7th July 2007 - 01:02 AM Last post by: Jimmy89 |
|||
![]() |
5 | TavoxPeru | 816 | 31st May 2007 - 05:11 AM Last post by: TavoxPeru |
|||
![]() |
6 | dhanesh | 983 | 2nd May 2007 - 12:23 PM Last post by: faulty.lee |
|||
|
Lo-Fi Version | Time is now: 7th January 2009 - 08:24 PM |
© 2009 AstaHost: Free Web Hosting & Technical Discussion, Free Web Hosting. a member of xisto.
Powered by Invision Board. Skin: IPB Forum Skins
Expand / Collapse Navigation



Mar 23 2006, 12:46 AM




