Thread: Need help with find prime program

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    12

    Need help with find prime program

    Hello I need some help with my findprime program which is suppose to find a prime number between n (a number from the user) and 2 * n.
    Code:
    int isPrime(int n)
    {
    int k;
            if (n < 2) {
            return 0;
                       }
            for (k=2; k > n; k++)
            {
            if ((n % k)==0)
            return 0;
            }
            return 1;
            }
    
    
    /* Implement your findPrime function here. */
    
    int findPrime(int n)
    {
    int nn;
    int p;
    
    p = n * 2; /* Maximum range of finding a prime number */
    
    
            for(nn=n+1; p > nn; nn++)  /* As long as nn isnt bigger than doulbe n, add one */
            {
            isPrime(nn) ;
            }
    
    }
    Above the findprime is the isprime which finds out if its a prime number or not (it works)..

    Anyways I'm really confused for the findprime and if anyone can grant me some insight that would be great.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you're looking for all the primes for N, then the greatest prime number possible for it, will be sqrt(N) (include math.h).

    You just want to calculate that square root one time, for any N.

    Code:
    For every N in the set of numbers: {
       good=1
       get the sqrRoot of N
       for(i=3;i<sqrRoot;i+=2) { //after 2 all primes are odd
           if(N mod i ==0) {
               good=0;
               break;
           }
       }
       if(good) {
           the number is prime.
           add it to the primesArray[]
       }
    }
    In your for loop, you have >n, and I believe you wanted <n, but use the above logic for better results. This is called "trial by division", btw.

    You can put the inner for loop in another function and call it. However you want to arrange it.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    i can write the program which finds all the prime numbers between n and 2n (n-2n)
    but i cant write a program which find a prime number between this integers.Because i dont know randomizing in c;and i cant choose any prime number,
    if you need i can do.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rac1 View Post
    i can write the program which finds all the prime numbers between n and 2n (n-2n)
    but i cant write a program which find a prime number between this integers.Because i dont know randomizing in c;and i cant choose any prime number,
    if you need i can do.
    Adak is pretty good at this stuff... listen to his advice.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    i looked his post but i didnt understand also i wrote it , it didnt worked.where am i wrong?

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    
        int main()
        {
         int y=1;
         int i;
         int n;
         int a=0;
         int c[100];
         int t=0;
         printf("Enter a number:");
         scanf("%d",&n);
         a=sqrt(n);
         printf("prime numbers:");
        
            for(i=3;i<a;i+=2)
            {
                if (n%i==0) {
                    y=0;
                    break;
                }
    
    
           
                if (y)
                { 
                    c[t]=i;
                    t++;
                }
             }
    
    
            for(i=0;i<t;i++)    printf("%d ,",c[i]);
        
            
            getchar();
         return 0;
        }
    Last edited by rac1; 11-15-2011 at 12:50 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In Trial by Division, and most other ways of finding a prime number, you need TWO for loops, not counting the for loop to print them out.

    Your code only has ONE for loop.

    Did you notice that the code I posted had TWO for loops? :

    Code:
    For every N in the set of numbers: {
       good=1
       get the sqrRoot of N
       for(i=3;i<sqrRoot;i+=2) { //after 2 all primes are odd
           if(N mod i ==0) {
               good=0;
               break;
           }
       }
       if(good) {
           the number is prime.
           add it to the primesArray[]
       }
    }
    N represents the Number being tested to see if it's prime. i is in the loop that generates the numbers that will be used to test N, by the modulus operator ( % ), to see if it will divide evenly into N.

    If you can find every prime number, then you should look over that code. Because what you are doing here, is searching for every prime number between n and 2n EXCEPT, you will stop your search, when you have found your first prime.

    That means your outer for loop will not start at N=2. It will start at the number that the user enters for n, and will stop at 2n, or before.

    So in your code, let's see what it still needs:

    Code:
         printf("prime numbers:");
         //adjust an even numbered n, to make it odd
        if(n%2==0) 
           ++n;
    
        //add the second for loop here:
        for(N=n;N<2*n;N+=2) {  //all numbers to be tested, will now be odd
            
            a=sqrt(N);    //delete your other sqrt() code above this
                                 
            for(i=3;i<=a;i+=2) //need <= here All testing numbers will also be odd
            {
                if (N%i==0) { //you're testing N, not n here
                    y=0; 
                    break;
                }
    
    
           
                if (y)
                { 
                    c[t]=i;
                    t++;
                }
             }
    If you only want ONE prime number between n and 2n, then if (y) should print out the prime number you have just found, and return (end the program). You're done.

    Put those changes into your program, and see how it does. I have not tried to run it. It's at least "close", however.
    Last edited by Adak; 11-15-2011 at 02:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-27-2010, 12:57 PM
  2. help, trying to find prime number
    By vampirekid.13 in forum C++ Programming
    Replies: 19
    Last Post: 05-02-2009, 03:29 PM
  3. Find first 250 Prime Number
    By dummies in forum C Programming
    Replies: 4
    Last Post: 01-12-2007, 09:18 AM
  4. for loop to find prime numbers
    By lsecrease in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 01:58 AM
  5. for loop to find prime numbers
    By 1rwhites in forum C Programming
    Replies: 11
    Last Post: 10-21-2005, 10:37 AM