+ 5
Is this code is giving proper output ?
#include <iostream> using namespace std; int main() { int number,sum; sum=0; number=1; while(number<=1000) { if(number%2==0) { sum=sum+number; } number=number+1; } cout<<"Sum of even numbers in between 1 to 1000 is:"<<sum; return 0; }
13 Answers
+ 7
uuuuuuuuu
thanks ☺
+ 6
+ 5
AK-47
got it !
+ 4
You're effectively wasting cpu power for 500 cycles of loop. If the program's goal is extracting and adding all even numbers in the range (which covers half of the range) why just not covering that range?
number = 2;
while (number <= 1000) {
sum += number;
number += 2;
}
BTW, number += 2; for example, directly expresses the idea of adding 2 to the variable rather than number = number + 2; which tells us how you are going to do that step-by-step.
+ 2
uuuuuuuuu
" I just added my answer as an insight on how you can calculate the result in a more efficient manner with a bit of math."
The effeciency should be relatative to the contexet for which it applies. Doubtlessly, there would be a dozens of tricks to make the result more efficient but with no good for the program's goal. For example, in
sum = ((a1 + an) * n) / 2
you might change that like the following
sum = ((a1 + an) * n) >> 1
to squeez a very nominal performance (which usually done by optimizer at compilation time), but all of sudden you discover that the goal isn't just finding even numbers and their grand total. Instead doing some more complex operation/calculation with each of them is desireable*. In such case, changing the program structure is mandatory**. In other word, first, design should fit the requirement, second, optimization should fit the design and/or implementation.
________
* Refers to design flexibility and ease of maintanance.
** In large scale systems would be costly.
+ 2
AK-47 What you wrote about is true and very important, and of course, I was using the question's very specific context to use bit of an optimization. I wasn't trying to encourage premature optimization or neglection of good design in favor of performance, I just wanted to give an idea about the possibilities of using a bit of thinking and/or mathematical knowledge to gain speed, instead of just straightforwardly coding the solution that comes to our mind the first time. That a thing like this is applicable or not is up to the programmer to identify.
+ 2
uuuuuuuuu
" [...]. I wasn't trying to encourage premature optimization or neglection of good design in favor of performance, I just wanted to give an idea about the possibilities of using a bit of thinking and/or mathematical knowledge to gain speed [...] "
In fact, I appreciate that you provided such succinct method with minimized scope to the cause by which the OP could consider another variation of the same problem. I just wanted to draw the attention to a bigger picture that you are cutely aware of.
" [...] That a thing like this is applicable or not is up to the programmer to identify. "
True. And also the importance of having sufficient tools (algorithms) in his arsenal in order to decide which one applicable best to the problem at hand.
+ 2
AK-47 By the way, I hope it didn't come off as me trying to devaluate your comment on my answer. I wasn't in any way trying to do that. I'm actually glad that you pointed out aspects that I did not.
+ 2
uuuuuuuuu
No, not at all, dear. The beauty of a discussion reveals itself when different points of view (even conflicting ones) are being expressed. Sometimes, folks' feelings might hurt a little bit because they find out that they weren't good enough as they thought they are. They endure some sort of technical humiliation, but they learn how to get better. Believe me, I've been in those situations a lot. Being attacked by a bunch of professionals for my bad English, bad programming style, immature use of language facilities, etc.
I think, the day's lesson beside our regular strive towards perfection can be the ability to tolerate all kinds of negative feedbacks which is part of all serious moves.
Well, enough waxing philosophical! ;-)
+ 2
uuuuuuuuu
It's my pleasure, dear. I Hope to have a chance to participate in another round of discussions again. Thank you.
+ 1
You can calculate the sum in a much more performance friendly way (especially for larger amount of numbers). Since this is basically an arithmetic sequence, you can calculate the result without a loop, like this:
sum = ((a1 + an) * n) / 2;
, where the variables hold the following:
a1: First element of the summated part of the sequence (2 in this case, the first even number in the 1-1000 range)
an: Last element of the summated part of the sequence (1000 in this case, the last even number in the 1-1000 range. If you want to take other numbers as your range start and end, you have to take into consideration what kind of numbers you are summing up.)
n: The number of elements taken from the sequence for summation, which can be calculated as: (an - a1) / d + 1, where d is the common difference term of the arithmetic sequence (in this case, 2, since the difference between consecutive even numbers is 2).
You can read more about this on Wikipedia: https://en.m.wikipedia.org/wiki/Arithmetic_progression
+ 1
By the way, I just added my answer as an insight on how you can calculate the result in a more efficient manner with a bit of math.
+ 1
AK-47 It seems like we share the same view about the topics you touched. Thanks for the pleasant exchange of thoughts and for sharing your wisdom. :)