$gaussa Identifier

By IllogicTC on Jan 08, 2011

This is an old snippet I wrote years ago. It return the sum of a defined series of integers. Also, when pulling the description from the snippet to this box, I was too lazy to delete the semicolons. Sorry.

Changed from the original:
Addition of credit to MPDreamz for doing optimization to my snippet, and adding (now Illogic) next to my old nick.

;---Usage---
;
;$gaussa(n1,n2,n3) -- (n3 is optional)
;
;n1 and n2 define the beginning and end of a numeric series, and n3
;can be used to define the increase from number to number (if the increase
;is greater than 1.)
;
;e.g.,
;/echo Code: $gaussa(1,5,2) will return 9, since from 1 to 5, and
;increasing by 2 each time equals 9. (1 + 3 + 5)
;
;/echo Code: $gaussa(1,5) will return 15, because (1 + 2 + 3 + 4 + 5) = 15.
;
;Not specifying n3 will automatically set the increase between numbers to 1.
;If (n2 - n1) is not a multiple of n3, the script will return null.

;Gauss 1.0
;By Lime (now Illogic)
;Code Optimization by MPDreamz

alias gaussa {
  if ($3 \\ $calc($1 - $2)) return
  var %x $1 , %y $1
  while (%x < $2) {
    inc %x $3
    inc %y %x
  }
  return %y
} 

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.