Saturday, January 17, 2009

Triangular Numbers

A triangular number is the sum of the n natural numbers from 1 to n.


ASP

  1. function triangularNumber(someNumber)
  2.     dim i
  3.     dim result
  4.     result = 0
  5.     for i = 1 to someNumber
  6.         result = result + i
  7.     next
  8.     triangularNumber = result
  9. end function

PHP

  1. function triangularNumber($number)
  2. {
  3.     for($i = 1; $i <= $number; $i++)
  4.     {
  5.         $result += $i;
  6.     }
  7.     return $result;
  8. }

No comments: