CrazyEngineers
  • Vishal
    Vishal

    MemberSep 8, 2013

    Euler problem 50

    I was trying to solve the Euler problem number 50, and wrote the following code and got the answer as,
    no. of primes = 536
    and prime value is 958577
    But, when i search the answer for this on internet, it says 543 primes and prime is 997651
    why does my program ends even if the sum is not > than 1000000 ???


        #include
        #include
        void prime(); // generates prime
        int send_prime(long int); // used to send the generated prime
        int check_prime(long int); // checks whether obtained sum is prime or not
        long int sum = 0;
        long int p = 0; // count of primes
        int main()
        {
        prime();
        return 0;
        }
        void prime() {
        int count = 0;
        for(long int i = 2 ; i <= 1000000 ; i++) {
        count = 0;
        for(long int j = 2 ; j <= (i/2) ; j++) {
        if(i%j == 0) {
        count++;
        break;
        }
        }
        if(count == 0) {
        p++; // number of prime incremented
        int check = send_prime(i);
        if(check == 0) // to check is sum goes beyond 1 million
        break;
        }
        }
        }
        int send_prime(long int x) {
        sum = sum + x;
        if(sum > 1000000)
        return 0;
        int u = check_prime(sum);
        if(u == 1 && sum < 1000000) {
        printf("\nnumber of terms = %d\n",p);
        printf("\n%d\n",sum);
        return 1;
        }
        else
        return 1;
        }
        int check_prime(long int x) {
        int count = 0;
        for(long int i = 2 ; i <= x/2 ; i++) {
        if(x%i == 0) {
        count++;
        break;
        }
        }
        if(count == 0)
        return 1;
        else
        return 0;
        }
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Home Channels Search Login Register