View the Most Wanted LQ Wiki articles.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org sum the numbers between and including a and b
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 02-11-2010, 10:39 PM #1 palpankaj LQ Newbie   Registered: Jul 2009 Posts: 3 Rep: sum the numbers between and including a and b Please help me to write a script in bash or shell to add the numbers between and including two numbers Thanks in Advance Last edited by palpankaj; 02-11-2010 at 10:41 PM.
 02-12-2010, 12:11 AM #2 tuxdev Senior Member   Registered: Jul 2005 Distribution: Slackware Posts: 2,014 Rep: We aren't going to do your homework, but here's some good reading material (and throw away whatever it is you're reading now): http://mywiki.wooledge.org/BashGuide...til_and_for.29 http://mywiki.wooledge.org/ArithmeticExpression
 02-12-2010, 02:17 PM #3 jschiwal Guru   Registered: Aug 2001 Location: Fargo, ND Distribution: SuSE AMD64 Posts: 15,733 Rep: Actually, this problem reminds me of when I derived the formula on my own in high school. Before there was such a thing as home computers. Sometimes the way you look at the problem is what provides the solution. Code: ``` 0 1 2 3 4 5 6 7 15 14 13 12 11 10 9 8``` Maybe a better hint is that since the average = total / items, it follows that the total is the average * items. That should help you derive the formula. You can either derive it directly or subtract the formula for a 1-m series from a 1-n series and reduce the formula for the formula of the sum of a m-n series. Last edited by jschiwal; 02-13-2010 at 11:35 AM.
 02-18-2010, 09:52 AM #4 pixellany LQ Veteran   Registered: Nov 2005 Location: Annapolis, MD Distribution: Arch/XFCE Posts: 17,802 Rep: Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.