how do i calculate prime number

This is a discussion on how do i calculate prime number within the C Programming forums, part of the General Programming Boards category; i'm a beginner using C.. i have a project on creating a souce that can detect prime number. for example ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    25

    Smile how do i calculate prime number

    i'm a beginner using C.. i have a project on creating a souce that can detect prime number.
    for example
    if the user enter :10
    they will print out all the prime nummber within 10
    which mean it will print out:7,5,3,1.
    can anybody give some tips on doing that ?
    any help will be appreciate..

  2. #2
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Try looking at this http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

    There's a faster algorithm, but I think it's much more complicated

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    i'm using Dev-C++
    i looking at that link u give but i can't get anything from it..
    anyway to help? like code for the program? i have to due the project in 2 days!
    pls help

  4. #4
    Banned
    Join Date
    Nov 2007
    Posts
    678
    *lol*
    Why everyone comes seeking help for completing their homework?

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    lol.. i'm a beginner..and i'm stuck for 4hrs! and i can't figure out what wrong !

  6. #6
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    This is how the sieve works:

    1. create a table of numbers, starting with 2
    2. take the first element (let's call it "i"), and take out all of its multiples (eg. 2+2, 2+2+2, 2+2+2+2)
    3. when you're done, whatever i is is a prime number.

    then you just repeat the process for whatever number comes after i.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    i understand what it's talking about..
    however i can't get it into the programming code.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by pimp View Post
    lol.. i'm a beginner..and i'm stuck for 4hrs! and i can't figure out what wrong !
    THAT we could help with. What are you stuck ON? Post up whatever it is (and use the code tags so it looks good). ([code*] your code here [/code*] Just remove the asterisks.

    Look up the "%" operator. That's the "mod" operator, and it's the key operator for a prime number test. If the mod result is zero, then the number being tested, is not prime number.

    You know that 1, 2, and 3 are primes, so print them up first, maybe.

    Then maybe use a while () or for() loop to test all the other numbers above 3 and see if they're prime or not.

    Do you know how to put a loop inside another loop? That could be handy.

    So get to it, and post up some code, and tell us whatever you're stuck on.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    how can i attach file?
    cause the program itself is not just print prime number but there are more.
    source 1
    Code:
     #include<conio.h>
    #include<stdlib.h>
    #include<stdio.h> 
     
     
     int main()
     {
         int minusONE,top,reminder,flag,value;
     
       do
         {
            for(minusONE = top - 1; minusONE > 0; minusONE--)
            {
                reminder = top % minusONE;
                printf("\n%d / %d reminder is %d", top, minusONE, reminder);
                _sleep(200);
            
            }
            
         }while(minusONE >=3&&flag !=0);
         {
                         minusONE--;
                         flag=value%minusONE;
         }
       
         
     }
    and source 2
    Code:
    #include<stdlib.h>
    #include<conio.h>
    #include<stdio.h>
    int main()
    {
        int value,remainder,counter;
        int numArray[100],numIndex,flag=0; 
        
        printf("pls enter limit");
        scanf("%d",&value);
        
        for(counter=1;counter;counter++)
        {
            remainder = value % (value-counter);
            
            printf("value of remainder is %d\n",remainder);
            printf("value of value is %d\n",value);
            printf("value of moduler is %d\n",value-counter);
            
            _sleep(1000);
            printf("value of counter %d\n",counter);
            if(remainder ==0)
            {
                value--;
                flag=1;
                system("pause");
            }
            /*else
            {
                for(numIndex=0;numIndex<=value;numIndex++)
                {
                  numArray[numIndex]=value;
                }    
            }*/    
               
        }
             
        system("pause");
        return 0;     
    }
    i'm stuck with these.. i can't continue writing..

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There's no reason to "attach file". Finding a prime number is a very small program.

    That code has several errors in it. Junk it, and let's start fresh.

    Also, it's a lot easier to check your code out, if you start the numbers (both being tested, and the number
    doing the testing), at the small end of the scale, not the top end.
    Last edited by Adak; 01-11-2008 at 07:39 AM.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    adak can u help me with the code?
    any suggestion?

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yeah, sent you a PM, did you read it yet?

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    manv and adak thk for the guide.. i will figure it out and post it back..

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    i still can't get it work! omg

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The problem with private concelling is we have no how far you have got and where you are stuck. It is pointless to simply state that you are stuck.

    There are several approaches.

    You may use the Sieve. The link has a step-by-step explanation of the algorithm, pseudo-code and even an animation to demonstrate how it works. So you should be able to put at least something together that you can show us.

    Or you could simply loop from 2 to input (note that 1 is not a prime) and perform the naive primeness test on each value (trial divisions to determine if it is divisible by any value). This approach is not as efficient but it is simpler to code.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help, trying to find prime number
    By vampirekid.13 in forum C++ Programming
    Replies: 19
    Last Post: 05-02-2009, 04:29 PM
  2. Prime number algorithm
    By Akkernight in forum C Programming
    Replies: 9
    Last Post: 04-19-2009, 02:50 PM
  3. how to calculate billion number
    By mrmamon in forum C++ Programming
    Replies: 9
    Last Post: 10-16-2005, 07:34 AM
  4. prime number finder.
    By Aalmaron in forum C++ Programming
    Replies: 2
    Last Post: 03-05-2004, 04:56 PM
  5. Replies: 3
    Last Post: 01-14-2003, 10:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21