Keep in mind that there is a mathematical way to do this.

Adding all digits 1-100:
Code:
//echo -a $calc((100 + 1) * (100 - 1 + 1) / 2)


(ending digit + beginning digit) * (ending digit - beginning digit + 1) / 2 = sum

You use a similar thing for using a step other than 1 (0-100 by 10):
Code:
//echo -a $calc((100 / 10) * (10 * (100 / 10) + 10) / 2)


This is Gauss's formula.

(ending digit / step) * (step * (ending digit / step) + step) / 2 = sum

(ending digit / step) = number of steps

Here's another example (0-20 by 2):
Code:
//echo -a $calc((20 / 2) * (2 * (20 / 2) + 2) / 2)



Note that this works from 0-N. It won't work from 10-N or something else unless you do more work. If you want to do a different starting number than 0, then you'll do the equation twice...

For 10-20 by 2:
Code:
//echo -a $calc(((20 / 2) * (2 * (20 / 2) + 2) / 2) - ((8 / 2) * (2 * (8 / 2) + 2) / 2))



(ending digit / step) * (step * (ending digit / step) + step) / 2 - ((beginning digit - step) / step) * (step * ((beginning digit - step) / step) + step) / 2 = sum

What you're doing is subtracting the sum of all the numbers prior to your starting number from the sum of the total.

You can make a $gauss identifier for this:
Code:
alias gauss {
  return $calc((($2 / $3) * ($3 * ($2 / $3) + $3) / 2) - ((($1 - $3) / $3) * ($3 * (($1 - $3) / $3) + $3) / 2))
}

USE: $gauss(beginning number,ending number,step)
EXAMPLE: $gauss(10,100,10)
NOTE: You should make sure that there is an even number of steps (1-4 step 2 wouldn't work well, but 0-4 step 2 would)

Last edited by Riamus2; 19/12/06 04:23 PM.

Invision Support
#Invision on irc.irchighway.net