Compute sum of primes?

Discussion in 'Programming' started by chee, Oct 23, 2006.

  1. #1
    Hi,

    What is the algorithm to compute the sum of primes?

    Found this challenge at rankk.org.

    Got stuck :(

    Could someone help?
     
    chee, Oct 23, 2006 IP
  2. alkoxy

    alkoxy Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    int n; //the number till which u need to compute primes
    int factors; //counts the number of factors
    for(int i=0;i<n;i++)
    {
    for(int j=1;j<i;j++)
    {
    if(i%j==0)
    {
    factors++;
    }
    if(factors==2)
    {
    break;
    }
    }
    factors=0;
    sum+=i;
    }
    cout<<sum;


    hope this works for you
    ps- the code is written in c++....did not have the patience to write the algo
     
    alkoxy, Oct 25, 2006 IP
  3. chee

    chee Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Many thanks, alkoxy :)
     
    chee, Nov 2, 2006 IP