Saturday, June 21, 2008

AM/PM

To those of you who have been bored by the recent string of math-related articles, I apologize. I still have a lot of math-related functions to share with you, but I do have other things to share with you as well, such as validation functions, string functions, and datetime functions. This week we'll take a look at one of those datetime functions.


In ASP, time values are normally in the format HH:MM:SS followed by AM or PM. Quite often, we don't want to include the seconds portion, so we extract the hours and minutes using the Hour() and Minute() functions respectively. Unfortunately, there is no quick function for extracting the AM/PM. Let's do something about that right now.


  1. function ampm(someTime)
  2.     if hour(someTime) < 12 then
  3.         ampm = "AM"
  4.     else
  5.         ampm = "PM"
  6.     end if
  7. end function

Now we can do stuff like this:
Hour(someTime) & ":" & Minute(someTime) & " " & ampm(someTime)


Those of you who are PHP programmers are hopefully aware that the same result can be achieved in PHP using the built-in date() function and specifying the letter A or a in the format string for uppercase or lowercase respectively:
date("g:i A", $timestamp)


View this code on Snipplr

Saturday, June 14, 2008

Factorial

This week we're going to delve into some discrete math, starting with factorial. The factorial of a number is the product of that number and all the numbers smaller than it. For example, the factorial of 3 is 1 x 2 x 3 = 6.


ASP

  1. function factorial(x)
  2.     dim result
  3.     result = 1
  4.     if x > 1 then
  5.         for i = 2 to x
  6.             result = result * i
  7.         next
  8.     end if
  9.     factorial = result
  10. end function

PHP

  1. function factorial($x)
  2. {
  3.     $result = 1;
  4.     if ($x > 1)
  5.     {
  6.         for ($i = 2; $i <= $x; $i++)
  7.         {
  8.             $result *= $i;
  9.         }
  10.     }
  11.     return $result;
  12. }

Now that we have a function for factorial, we can also do combinatorial. Combinatorial tells us the number of combinations, without regard to order, of y items that can be made from a pool of x items.


ASP

  1. function combinatorial(x, y)
  2.     if (x >= y) and (y > 0) then
  3.         combinatorial = factorial(x) / factorial(y) / factorial(x - y)
  4.     else
  5.         combinatorial = 0
  6.     end if
  7. end function

PHP

  1. function combinatorial($x, $y)
  2. {
  3.     return (($x >= $y) && ($y > 0)) ? factorial($x) / factorial($y) / factorial($x - $y) : 0;
  4. }

We can also get the number of permutations of y items that can be made from a pool of x items.


ASP

  1. function permutations(x, y)
  2.     permutations = factorial(x) / factorial(x - y)
  3. end function

PHP

  1. function permutations($x, $y)
  2. {
  3.     return factorial($x) / factorial($x - $y);
  4. }

Saturday, June 7, 2008

Pythagorean Theorem

When you're dealing with right-angled triangles, trigonometry is not required to calculate the length of the hypotenuse. Our PHP programmers friends have a function called hypot() which solves for c in the equation: a^2 + b^2 = c^2


  1. function hypot(a, b)
  2.     hypot = sqr(a^2 + b^2)
  3. end function

Using the classic 3-4-5 triangle as an example, for a = 3 and b = 4, the function will return 5.


View this code on Snipplr