Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Prime Number Generator
lalhsboard
post Mar 23 2006, 12:46 AM
Post #1


Newbie [ Level 1 ]
Group Icon

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
Go to the top of the page
 
+Quote Post
vizskywalker
post Mar 23 2006, 02:26 AM
Post #2


Techno-Necromancer
Group Icon

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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. What Is: World's Largest Known Prime Number(20)
  2. Any Tool/software For Php Code Generation ?(3)
  3. How Can I Create A "number Of Visitors" Script(8)
  4. Need Drivers For Intraserver SCSI Adapter(2)
  5. Neopets News(14)
  6. Finding The Current Line Number In A Text Box(1)
  7. Where Can I Find A Rhythm Generator Program(3)
  8. Fibonacci Number Program Problem(5)
  9. Decrementing Number Of Digits Ofter Point(15)
  10. Thumbnail Generator For Windows(7)
  11. Get The Most From Your Post(20)
  12. Visitor Hit Counter Script?(7)
  13. Count The Number Of Records Of A Table Group By A Foreign Key(0)
  14. Yeppee ! Post Number One Thousand !(5)
  15. Random Name Generator(2)
  1. Reliable Hardware Serial Number For Software Protection?(10)
  2. Terregan(2)
  3. Auto-number Help In Access Db & Vb .net(6)
  4. Web Form Generator(5)
  5. How To Limit The Number Of Pages In Ms Word?(6)
  6. Lightbox Js Web Gallery Generator(1)
  7. Statistics Generator(2)
  8. Necklace Problem(8)
  9. Maximum Number Of Decimal Places In Perl Language(0)
  10. What Is The Best Way To Merge An Unknown Number Of Arrays?(4)
  11. Generator(2)
  12. Mysql Question(inserting Number From A Textfield)(3)
  13. Xml-echo Google Sitemap Generator(0)


 



- Lo-Fi Version Time is now: 22nd November 2008 - 11:55 PM