Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7632253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:34:48+00:00 2026-05-31T06:34:48+00:00

I doing another question from the Eular problems page. The sum of the primes

  • 0

I doing another question from the Eular problems page.
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.

I’ve managed to write the code below but i think somewhere along the line (namely when we get to big prime numbers) the code loses accuracy. The answer should be 142913828922 but i get 1179908154.

I dont know why im not getting the answer because the code below works for under 10.

Any help would be great. the reason im doing these problems is to get better at C.

code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* Initialise */
void CalcNumber(unsigned long number);
int isPrime(unsigned long number);

/* Functions*/

void CalcNumber(unsigned long number)
{
    unsigned long i = 1;
    unsigned long prime = 0;

    while(i != number)
    {
        i++;
        if(isPrime(i))
        {
            printf("prime: %lu\n", i);
            prime += i;
        }
    }

    printf("The sum of primes under %lu: %lu\n",number, prime);
    printf("count: %d\n", i);

}

int isPrime(unsigned long number)
{
      int i, nb, count, test,limit;
      test = count = 0;
      nb = number;
      limit = sqrt(nb) + 1;

      if(nb == 2)
      {
          return 1;
      }

      if (nb % 2 == 0)
              test = 1;
      else{
          for (i = 3 ; i < limit && ! test; i+=2, count++)
            if (nb % i == 0)
              test = 1;
      }
      if (!test)
              return 1;
      else
              return 0;
}

int main(void)
{
    unsigned long number;

    printf("Enter a number: \n");
    scanf("%ul", &number );
    CalcNumber(number);
    return EXIT_SUCCESS;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:34:49+00:00Added an answer on May 31, 2026 at 6:34 am

    Considering the length of the number you should use a data type long at least 64 bits. The newer C99 standard includes the long long (and unsigned long long) datatype that is at least 64 bits. If you need to printf them you have to use "%lld" and "%llu".

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Another newbie question from me. Just checking whether my code below is possible. I.e
Another noob regex problem/question. I'm probably doing something silly so I thought I'd exploit
I was turned on to nstrees from another question, and have been working through
So in a comment from another question , I just saw this example for
I'm looking for another way of doing the following: function call_any_function(func, parameters){ // func
I have a regex expression that I'm doing a split against another string and
How does PHP handle client connection while doing sleeps? Or put another way ...
Doing the below will reproduce my problem: New WPF Project Add ListView Name the
In another question , I asked why it takes so long to remove HTML
Another question on StackOverflow is asking how this is done, but the general question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.