Thread: sum of prime numbers

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    15

    Post sum of prime numbers

    hi

    i have to calculate the sum of prime numbers below 500,000

    someone help me plz

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What have you tried? Try tackling the problem in smaller chunks. Can you loop from 1 to 500,000? Can you determine whether a number is prime? Can you add numbers?

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    15
    my problem is how to determine a number is prime or not ><

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Have you Googled it? There are tons of tutorials on this all over the web on this very topic, no sense in me repeating what others have said so clearly and succinctly. Do you know what a prime number is? Can you determine whether a number is prime by hand, with paper and pencil? If not, you must learn that first. It's impossible to code a solution when you don't even know the solution.

    Then, make a serious effort to write the code, and if you have trouble, post the code you do have, along with some specific questions or a description of what isn't working with the code.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    15
    of course, i have googled it but i can't understand it
    yeah i know what prime number is by hand

    and this was my last try :
    Code:
    # include <stdio.h>
    int main(void)
    {
        int x;
        int j;
        int sum;
        
        for(x=2;x<=10;x++)
        {
            for(j=2;j<=x;j++)
                if ( (j%x) == 0)
                    sum=sum+j;
        }
        printf("sum is : %d",sum);
        return(0);
    }
    

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I will reiterate:
    Quote Originally Posted by anduril462 View Post
    post the code you do have, along with some specific questions or a description of what isn't working with the code.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Bonn, Germany
    Posts
    16
    Your code is wrong in almost every way ...
    - for every x there will be a j so that j%x==0, which is obvious because j goes from 2 to x
    - you sum up the wrong value (j instead of x)
    - you sum up in the wrong case (when your "primality test" (if it was one) says "no prime")
    - you count the same number multiple times
    ...
    Last edited by TomasRiker; 05-23-2012 at 12:20 PM.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    15
    - you sum up in the wrong case (when your "primality test" (if it was one) says "no prime")
    - you count the same number multiple times
    ...
    can you explain them in more details plz?

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Remember, I said work on one small piece at a time. Start with determining whether a number is prime. Write a function called isPrime that takes an integer and returns true if it is a prime number, false if it is not. Create a main function simply prints out the result of that function for a few different numbers, something like:
    Code:
    int isPrime(int x)
    {
        // you write this part
    }
    
    int main(void)
    {
        printf("5 is %s\n", isPrime(5) ? "prime" : "composite");
        printf("10 is %s\n", isPrime(10) ? "prime" : "composite");
        // some more test cases that you make up, check "special" cases, like 0, 1, 2 also
        return 0;
    }
    Once you get your isPrime function working and tested, then add a loop that goes from one to MAX_NUMBER. Start with MAX_NUMBER being small, under 100, so it's easy to test. Loop from 1 to MAX_NUMBER, and simply print out the prime numbers you encounter. Once you have that working, and know you are printing all the prime numbers and none of the composite ones, then work on implementing the sum part.

    NOTE: If you haven't seen the ?: syntax before, it's a little bit like a shorthand if-else statement. If isPrime is true, it gives "prime", otherwise it gives "composite". That string ("prime" or "composite") is put in place of the %s in your printf.

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    15
    what condition i have to use to check if the numbers prime or not ?

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That's what you're supposed to figure out. I'm not just going to give you the answer. Use the code I gave you in post #9 and try to write the body of the isPrime function. You are close with your for loop and remainder check. This is one of the most common beginner problems out there, Google for some tutorials and examples.

  12. #12
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by vin_320 View Post
    ... yeah i know what prime number is by hand ...
    Quote Originally Posted by vin_320 View Post
    what condition i have to use to check if the numbers prime or not ?
    So how would you do it by hand with paper and pencil?

    For example, how would you know if 17571 is prime or not by using paper and pencil?

    What is the definition of a prime?

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    15
    okay , i need loop to check it whether it's prime or not

    prime number can is dividable by one and itself

    so i think i should use loop

    for (i=2;i<=10;i++) // i'm testing the prime numbers which less than 10
    for (x=2;x<=10;x++)
    if (i%2!=0&&i%x==0) //

  14. #14
    Registered User
    Join Date
    May 2012
    Posts
    15
    like i said:

    prime number can is dividable by one and itself


    correct me if i'm wrong plz

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yes, you are right: a prime number is only divisible by 1 and itself. Therefore, if a number is divisible by something other than 1 or itself, it is not prime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-16-2012, 02:07 AM
  2. non prime numbers or composite numbers program.help plz!
    By danishzaidi in forum C Programming
    Replies: 10
    Last Post: 11-15-2011, 11:10 AM
  3. prime numbers
    By tdoctasuess in forum C++ Programming
    Replies: 1
    Last Post: 05-11-2004, 10:54 PM
  4. Help with prime numbers
    By Clean Killa in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2002, 04:42 PM
  5. Prime Numbers
    By Korn1699 in forum C++ Programming
    Replies: 7
    Last Post: 11-03-2001, 09:52 PM