Thread: Prime number generator

  1. #16
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It still tests against all even numbers greater than or equal to four.
    I.e It only goes up by two at a time in the outer loop.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #17
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    ok i've taken the suggestions in and changed the code.
    i got the scrap book out and wrote down a flow diagram and then worked out the the code, adding the square root idea and incrementing by 2 idea in.

    i try to compile but i get lots of errors still. but it's close.

    Code:
    #include <stdio.h>
    #include <math.h> /* included for sqrt() function */
    main()
    {
    int p,prevprime,root,count,max;
    
    count=0;
    p=3;
    prevprime=0;
    FILE *fwrite;
    fwrite=fopen("/home/primes.txt","a+");  /*adds to the end of file */
    
    FILE *fread;
    fread=fopen("/home/primes.txt","r");  /*reads values of file */
    
    root=sqrt(p);
    
    printf("Enter how many primes to find:");
    scanf("%d",&max);
    
    while(max<=count)
    {count++
    
    while(prevprime<=root)  /*First logic argument checks previous prime value isn't greater then root value of p, since p divided by previous prime values (no greater than root) are the only ones needed to be checked*/ 
    {if((prevprime%p)==0) /*2nd logic argument check if p is actually a prime value */ 
     {printf("not a prime:%d\n",p); /* not a prime... dang */
      fclose(fread);       /*closes file so it will read from start next time */
      prevprime=0;      
      p=p+2;            /* incrementing up by 2, primes are only odds */
      root=sqrt(p);     /* new root value when new p value*/
     }
     else{fscanf(fread, "%d" &prevprime); /* changes preprime value so it can be checked again by 1st and 2nd logic argument */ 
          printf("prevprime value:%d",prevprime); /* i just want to see the preprime value to check whats happening */ 
         }
    }
    fclose(fread);
    printf("Prime:%d\n",p);   /* YAY prime number found*/
    fprintf(fwrite,"%d\n",p);     /* calls primes.txt and adds value at end of file */
    p=p+2;         /* incrementing up by 2, primes are only odds */
    root=sqrt(p); /* new root value when new p value*/
    prevprime=0;  /* resets preprime value for logic arguments */
    }
    printf("finnished finding %d primes", max);
    
    }

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by st00ch View Post

    i try to compile but i get lots of errors still. but it's close.
    Of course, it would be nice to see what those errors were! <hint, hint>

    A few suggestions:

    1) ALWAYS int main(void) or int main(int argc, char *argv[]), and return 0 at the end of main.

    2) The whole writing to the file, while reading from the file, and re-closing and opening the file - that part I'd dump. Put the primes you find, into a file, AND IF YOU WANT TO, keeping the last (most recent) big bunch of them (maybe 1,000 or 100,000 or whatever you think is a reasonable number. It depends on how many primes you're going to be generating), into the array.

    For example, using the basic strategy outlined in your code, you'll generate the first 1,000 prime numbers, in a second or so (maybe less, depending on your system). So being practical, although using the previous array is a big speed up, how much time do you want to spend optimizing the program, to save a half a second?

    3) The number two is a prime number (one is not but two is). So don't leave out 2.

    4) So I would suggest opening the file in write mode, only. Not append, and not for reading, either.


    5) Take an example of a prime: 101 - the previous prime will be WAY more than 10, which is the square root of 101 (about). previous primes should work down starting with the square root of the number being checked, and going down, or working from a very low prime like 5, and working upward.

    I don't get the whole previous prime logic, and I can't tell what "count" is counting.

    You have the two nested loops, and the basic idea, but I can't quite tell whats up with the logic in them.

  4. #19
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I don't feel like trying to read such badly formatted code today. Don't start each line on the same line as the curly bracket, and indent each line according to how many braces it is within.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #20
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Is there a better way to find all prime numbers in the range [m, n]?
    My implementation, although it only finds new primes instead of searching all the way each time, doesn't search higher than the square root of "n" and uses the IEEE-754 floating point trick to find it 10x faster, it can't beat the time limit of 6s that an online judge has!
    Devoted my life to programming...

  6. #21
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You don't even need to calculate the square root each time. Simply start from the square root value you had last time and loop incrementing lastsqrt until lastsqrt * lastsqrt >= n.
    It's one of those very seldom loops more than once things.

    Dependong on m, Miller Rabin can be the way to go.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Just when I thought it was "safe" to go back into the prime number water, too!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime Number Generator
    By abachler in forum Contests Board
    Replies: 76
    Last Post: 12-06-2009, 05:08 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. prime number program with function
    By mackieinva in forum C Programming
    Replies: 17
    Last Post: 09-20-2007, 08:36 AM
  4. Prime Number stops after 29, but why?
    By Daveo in forum C Programming
    Replies: 22
    Last Post: 09-17-2004, 10:55 AM
  5. Random number generator
    By Caze in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2002, 08:10 AM