Find the sum of all the multiples of 3 or 5 below 1000

前端 未结 16 939
时光说笑
时光说笑 2020-12-10 16:41

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. I have the following code but the answer do

相关标签:
16条回答
  • 2020-12-10 17:07

    You might start by iterating from 3 to 1000 in steps of 3 (3,6,9,12,etc), adding them to the sum like,

    int i = 3, sum = 0;
    for (; i < 1000; i += 3) {
      sum += i;
    }
    

    Then you could iterate from 5 to 1000 by 5 (skipping multiples of 3 since they've already been added) adding those values to the sum as well

    for (i = 5; i < 1000; i += 5) {
      if (i % 3 != 0) sum += i;
    }
    

    Then display the sum

    printf("%d\n", sum);
    
    0 讨论(0)
  • 2020-12-10 17:11
    #include<stdio.h>
    #include<time.h>
    int main()
    {
        int x,y,n;
        int sum=0;
        printf("enter the valeus of x,y and z\n");
        scanf("%d%d%d",&x,&y,&n);
        printf("entered   valeus of x=%d,y=%d and z=%d\n",x,y,n);
        sum=x*((n/x)*((n/x)+1)/2)+y*((n/y)*((n/y)+1)/2)-x*y*(n/(x*y))*((n/(x*y))+1)/2;
        printf("sum is %d\n",sum);
        return 0;
    }
    // give x,y and n  as 3 5 and 1000
    
    0 讨论(0)
  • 2020-12-10 17:13

    Rather than using range/loop based solutions you may wish to leverage more math than brute force.

    There is a simple way to get the sum of multiples of a number, less than a number.

    For instance, the sum of multiples of 3 up to 1000 are: 3 + 6 + 9 + ... + 999 Which can be rewritten as: 3* ( 1 + 2 + 3 + ... + 333)

    There is a simple way to sum up all numbers 1-N:

    Sum(1,N) = N*(N+1)/2
    

    So a sample function would be

    unsigned int unitSum(unsigned int n)
    {
        return (n*(n+1))/2;
    }
    

    So now getting all multiples of 3 less than 1000 (aka up to and including 999) has been reduced to:

    3*unitSum((int)(999/3))
    

    You can do the same for multiples of 5:

    5*unitSum((int)(999/5))
    

    But there is a caveat! Both of these count multiples of both such as 15, 30, etc It counts them twice, one for each. So in order to balance that out, you subtract once.

    15*unitSum((int)(999/15))
    

    So in total, the equation is:

    sum = 3*unitSum((int)(999/3)) + 5*unitSum((int)(999/5)) - 15*unitSum((int)(999/15))
    

    So now rather than looping over a large set of numbers, and doing comparisons, you are just doing some simple multiplication!

    0 讨论(0)
  • 2020-12-10 17:13

    Using the stepping approach, you can make a version:

    #include <stdio.h>
    
    int main ( void ) {
    
        int sum = 0;
    
        for (int i = 0; i < 1000; i += 5) {
            sum += i;
        }
        for (int i = 0; i < 1000; i += 3) {
            if (i % 5) sum += i;  /* already counted */
        }
        printf("%d\n", sum);
        return 0;
    }
    

    which does a whole lot fewer modulo computations.

    In fact, with a counter, you can make a version with none:

    #include <stdio.h>
    
    int main ( void ) {
    
        int sum = 0;
        int cnt = 6;
    
        for (int i = 0; i < 1000; i += 5) {
            sum += i;
        }
        for (int i = 0; i < 1000; i += 3) {
            if (--cnt == 0) cnt = 5;
            else sum += i;
        }
        printf("%d\n", sum);
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-10 17:18

    Perhaps you should do

    sum += i // or sum = sum + i
    

    instead of

    sum = sum + 1
    

    Additionally, be careful when printing long unsigned ints with printf. I guess the right specifier is %lu.

    0 讨论(0)
  • 2020-12-10 17:18
    int main(int argc, char** argv)
    {
        unsigned int count = 0;
        for(int i = 1; i < 1001; ++i)
            if(!(i % 3) && !(i % 5))
                count += i;
        std::cout << i;
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题