Page 2 of 3<123> Show 40 post(s) from this thread on one page

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Find the sum of all the prime numbers (http://www.go4expert.com/articles/sum-prime-t2049/)

 shabbir 29Nov2006 18:02

Re: Find the sum of all the prime numbers

There is other method also which does not require sqrt.

Quote:
 Originally Posted by wiki One need not actually calculate the square root; once one sees that the quotient is less than the divisor, one can stop

Re: Find the sum of all the prime numbers

Interesting, so we will find the factor of n within any of the number in sqrt(n) if not then its a prime number.

 friendsforniraj 2Dec2006 17:57

Re: Find the sum of all the prime numbers

let me tell you the logic now
it is very simple
look if k is any no.
then k can be represented as k=i*j
where i and are k's factor
now i varies from 1to k
and j varies from k to 1
but for prime no. we check for i=2 to k/2
acordingly j will varies from (k/2) to 2
now if 2 is a factor of k
then automatically k/2 is also a factor and so on
that is when i increases, j decreases
so we continue like this then there is condition that both i and j becomes equal
so i*i=k
so i=sqrt(k)
but if k is not a square of any no. then i increases upto a integer just less than sqrt iof k
thats all what i did
now try this problem
for a given no. print all its prime factors that is
if input is 12 out put should be 2*2*3
using the hint that the lowest factor of any no. other than one is always prime
try it

 shabbir 4Dec2006 10:45

Re: Find the sum of all the prime numbers

friendsforniraj thats a good logic explained.

 friendsforniraj 5Dec2006 17:40

Re: Find the sum of all the prime numbers

thank u sir
but ma name is niraj

 shabbir 6Dec2006 11:13

Re: Find the sum of all the prime numbers

Quote:
 Originally Posted by friendsforniraj thank u sir but ma name is niraj
I will try putting that from now on.

 rahul.mca2001 6Mar2008 13:53

Re: Find the sum of all the prime numbers

why do we need a square root

Re: Find the sum of all the prime numbers

Quote:
 Originally Posted by friendsforniraj let me tell you the logic now it is very simple look if k is any no. then k can be represented as k=i*j where i and are k's factor now i varies from 1to k and j varies from k to 1 but for prime no. we check for i=2 to k/2 acordingly j will varies from (k/2) to 2 now if 2 is a factor of k then automatically k/2 is also a factor and so on that is when i increases, j decreases so we continue like this then there is condition that both i and j becomes equal so i*i=k so i=sqrt(k) but if k is not a square of any no. then i increases upto a integer just less than sqrt iof k thats all what i did now try this problem for a given no. print all its prime factors that is if input is 12 out put should be 2*2*3 using the hint that the lowest factor of any no. other than one is always prime try it
Excellent explaination. Now any one can understand either he is new or experience

 oleber 8Mar2008 12:51

Re: Find the sum of all the prime numbers

for(j=2;j<=sqrt(i);j++) :cryin:

is similar to

for(j=2;j*j <= i;j++) :p

so, no sqrt in here since it is really slow. At least if was some years ago.

 oleber 8Mar2008 12:54

Re: Find the sum of all the prime numbers

for getting the prime number try

http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

No devisions in here

 All times are GMT +5.5. The time now is 14:21. Page 2 of 3<123> Show 40 post(s) from this thread on one page