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 8858393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:49:07+00:00 2026-06-14T14:49:07+00:00

Days ago I had a job interview were they ask me how I would

  • 0

Days ago I had a job interview were they ask me how I would calculate the sum of all the numbers multiples of 2 or 5 from 1 to 10000 using a the c language. I did this:

int mult2_5()
{
     int i, sum=0;
     for(i = 1; i <= 10000; i++)
        if(i % 2 == 0 || i % 5 == 0)
          sum += i;
     return sum;  
}

I as wonder if it was any faster implementation that this one?

  • 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-06-14T14:49:08+00:00Added an answer on June 14, 2026 at 2:49 pm

    The modulus operator is inefficient. A more faster implementation would be something like this:

    int multiply2_5(int max)
    {
      int i, x2 = 0,x5 = 0,x10 = 0;
    
      for(i = 2; i < max; i+=2)    x2 += i;    // Store all multiples of 2   O(max/2)
      for(i = 5; i < max; i+=5)    x5 += i;    //  Store all multiples of 3  O(max/5)
      for(i = 10; i < max; i+=10)  x10 += i;   //   Store all multiples 10;  O(max/10)
    
      return x2+x5-x10;
    }
    

    In this solution I had to take out multiples of 10 because, 2 and 5 have 10 as multiple so on the second loop it will add multiples of 10 that already been added in the first loop; The three loops combine have O(8/10 max).

    Another even better solution is if you take a mathematical approach.

    You are trying to sum all numbers like this 2 + 4 + 6 + 8 … 10000 and 5 + 10 + 15 +20 + … 10000 this is the same of having 2 * (1 + 2 + 3 + 4 + … + 5000) and 5 * ( 1 + 2 + 3 + 4 + … + 2000), the sum of ‘n’ natural number is (n * (n + 1)) (source) so you can calculate in a constant time, as it follows:

    int multiply2_5(int max)
    {
        // x = 2 + 4 + 6 + ... = 2 * (1 + 2 + 3 +...)
        // y = 5 + 10 + 15 + ... = 5 * (1 + 2 + 3 +...)
        // The sun of n natural numbers is sn = (n (n + 1)) / 2
       int x2 = max/ 2;                      // 2 * ( 1 +2 + 3 … max/2)
       int x5 = max /5;                      // 5 * ( 1 +2 + 3 … max/5)
       int x10 = max/ 10;                  
       int sn2 = 0.5 * (x2 * (x2+1));        // (n * (n + 1)) / 2
       int sn5 = 0.5 * (x5 * (x5+1)); 
       int sn10 = 0.5 * (x10 * (x10+1));
       return (2*sn2) + (5 *sn5) - (10*sn10);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had an interview days ago and was thrown a question like this. Q:
Several days ago I had a question about removing index.php from the address bar,
Two days ago i attended an interview.I had been asked a question and i
A few days ago my friend told me about the situation, they had in
Okay, I learned about this query 2 days ago:- SELECT SUM(DTotal) AS Monthly FROM
Few days ago I had to reinstall all my Linux system, and I also
I took a Computer Graphics exam a couple of days ago which had extra
I had started working on GPGPU some days ago and successfully implemented cholesky factorization
I had a question regarding this matter some days ago, but I'm still wondering
I had an iPad stolen a few months ago. A few days ago, the

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.