Thread: For one set of values answer is valid & for another set of values,answer invalid

  1. #1
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59

    For one set of values answer is valid & for another set of values,answer invalid

    /*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.
    Find the sum of all the multiples of 3 or 5 below 1000. */

    Code:
    #include<stdio.h>
    int main()
    {
         int i=1,m3=0,n;
           for(;i<10;i++)
         {
           if(((n=i*3)<10))
                 m3+=n;
           if((n=i*5)<10)
                m3+=n;
    }
         printf("res=%d\n",m3);
    return 0;
    }
    
    When I put 10 the answer is 23,when I try for 1000,output:266333,which is a wrong answer according to the eulerproject.net
    What is wrong in the program?

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    I'd imagine that it's because you're summing a lot of numbers twice. E.g. 15 is a multiple of 3 and of 5, and your code will add it twice. Since there aren't any common multiples of 3 and 5 below 10, you wouldn't see the problem there.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    Hi Shruthi,

    Your code is adding numbers which are multiples of both 3 and 5 twice.
    That's why you got wrong answer.

    Try this
    Code:
    if(((n=i*3)<10)) {
                 m3+=n;
    }
    else {
          if((n=i*5)<10)
                m3+=n;
    }
    

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Or to write less code
    Code:
    if( ( (n=i*3)<10 ) || ((n=i*5)<10)
       m3+=n;

  5. #5
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    I've used the above suggestion and still haven't got the right solution.It's not even satisfying the first condition,23.it's giving 18 because it not considering 5*1=5,it's only considered 3*1=3 and given the result 18.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your idea in the code you posted in post #1 looks like it should work, but your code is rather messy. Here's an improved version:
    Code:
    #include<stdio.h>
    
    int main(void)
    {
        int sum = 0;
        int i;
        for (i = 1; i < 1000; i++)
        {
            if (i % 3 == 0)
            {
                sum += i;
            }
            if (i % 5 == 0)
            {
                sum += i;
            }
        }
        printf("sum=%d\n", sum);
        return 0;
    }
    Notice that I got rid of the n variable, because you should not have been adding n to the sum (i.e., I fixed another bug for you, for free). However, I have left your double counting problem in the code for you to fix. It really is very easy to fix it now

    Hint: notice that for both the if statement, the body is exactly the same. Can you combine them into a single if statement?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Thank you laserlight,finally got the answer.
    Code:
    #include<stdio.h>
     int main()
    {
         int i,sum=0;
        for(i=1;i<1000;i++)
        {
            if((i%3==0) && (i%5==0))
              sum+=i;
             else
            {
                if(i%3==0)
                    sum+=i;
                if(i%5==0)
                   sum+=i;
             }
         }
              printf("sum=%d\n",sum);
         return 0;
    }
    


  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's good, though what I had in mind was:
    Code:
    #include<stdio.h>
     
    int main(void)
    {
        int sum = 0;
        int i;
        for (i = 1; i < 1000; i++)
        {
            if (i % 3 == 0 || i % 5 == 0)
            {
                sum += i;
            }
        }
        printf("sum=%d\n", sum);
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    and also you can club this in a single statement
    Code:
    if((i*3==0) || (i*5==0)
            sum+=i;

  10. #10
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Ok,got ur point

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with comparing answer to math problem and user's answer
    By Jake Garbelotti in forum C Programming
    Replies: 6
    Last Post: 07-20-2012, 10:12 PM
  2. passing int values and *double values through message queues
    By Hyp3rTension in forum C Programming
    Replies: 11
    Last Post: 05-11-2012, 05:04 AM
  3. Replies: 100
    Last Post: 06-21-2010, 02:22 AM
  4. have 5 min to answer
    By juststartedC in forum C Programming
    Replies: 7
    Last Post: 10-11-2007, 05:26 PM
  5. Where Can I Get The Answer?
    By MartinLiao in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2004, 01:19 PM