hi
i have to calculate the sum of prime numbers below 500,000
someone help me plz
hi
i have to calculate the sum of prime numbers below 500,000
someone help me plz
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?
my problem is how to determine a number is prime or not ><
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.
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); }
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 01:20 PM.
Visit my project: Online symbolic derivative calculator
can you explain them in more details plz?- 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
...
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:
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.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; }
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.
what condition i have to use to check if the numbers prime or not ?
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.
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) //
like i said:
prime number can is dividable by one and itself
correct me if i'm wrong plz
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.