Saturday, July 19, 2008

isAlpha(Numeric)

We're going to take a break from math-related functions for a few weeks (yay!) and play with regular expressions. Regular expressions are more powerful and faster than old-fashioned string parsing.


Both ASP and PHP have a function for checking if something is numeric. How about a function for checking if something is alphabetical?


ASP

  1. function isAlpha(someString)
  2.     dim regEx
  3.     set regEx = new RegExp
  4.     with regEx
  5.         .Global = true
  6.         .IgnoreCase = true
  7.         .Pattern = "[A-Z\s_]"
  8.     end with
  9.     if regEx.test(someString) then
  10.         isAlpha = true
  11.     else
  12.         isAlpha = false
  13.     end if
  14.     set regEx = nothing
  15. end function

PHP

  1. function is_alpha($someString)
  2. {
  3.     return (preg_match("/[A-Z\s_]/i", $someString) > 0) ? true : false;
  4. }

The test pattern we are using above will allow letters of the alphabet, the underscore character, and whitespace characters. With a small tweak to the test pattern, we can also write a function to check if a string is alphanumeric.


ASP

  1. function isAlphaNumeric(someString)
  2.     dim regEx
  3.     set regEx = new RegExp
  4.     with regEx
  5.         .Global = true
  6.         .IgnoreCase = true
  7.         .Pattern = "[\w\s.]"
  8.     end with
  9.     if regEx.test(someString) then
  10.         isAlphaNumeric = true
  11.     else
  12.         isAlphaNumeric = false
  13.     end if
  14.     set regEx = nothing
  15. end function

PHP

  1. function is_alphanumeric($someString)
  2. {
  3.     return (preg_match("/[\w\s.]/i", $someString) > 0) ? true : false;
  4. }

The \w switch in the pattern includes the 26 letters of the alphabet plus the numbers zero through nine.


More regular expression fun next week!


Saturday, July 12, 2008

Fibonacci numbers

The Fibonacci numbers have many applications in computer programming. Today we're going to write a function that returns individual numbers from the Fibonacci sequence.


ASP

  1. function fib(x)
  2.     dim fibArray()
  3.     redim fibArray(x)
  4.     fibArray(0) = 0
  5.     fibArray(1) = 1
  6.     for i = 2 to x
  7.         fibArray(i) = fibArray(i - 1) + fibArray(i - 2)
  8.     next
  9.     fib = fibArray(x)
  10. end function

PHP

  1. function fib($x)
  2. {
  3.     $fibArray[0] = 0;
  4.     $fibArray[1] = 1;
  5.     for($i = 2; $i <= $x; $i++)
  6.     {
  7.         $fibArray[$i] = $fibArray[$i - 1] + $fibArray[$i - 2];
  8.     }
  9.     return $fibArray[$x];
  10. }

So, for example, fib(8) will return 21, because the Fibonacci sequence is 1, 1, 2, 3, 5, 8, 13, 21, 34, 55...


Saturday, July 5, 2008

Fermat numbers

Today we're going to write a function to generate Fermat numbers. This could be useful if you want to write your own pseudo-random number generator.


ASP

  1. function fermat(x)
  2.     fermat = 2^2^x + 1
  3. end function

PHP

  1. function fermat($x)
  2. {
  3.     return 2^2^$x + 1;
  4. }

Saturday, June 28, 2008

Factors

Back in elementary school, one exercise you had to do in math class was to factor a number. For example, given the number 12, the factors are 2*2*3. On the off chance that somebody might want a function to do this, I decided to write one. It returns the factors in an array.


ASP

  1. function factors(x)
  2.     dim i
  3.     dim arrFactors()
  4.     for i = 2 to x
  5.         if x mod i = 0 then
  6.             redim preserve arrFactors(UBound(arrFactors) + 1)
  7.             arrFactors(UBound(arrFactors)) = i
  8.         end if
  9.     next
  10.     factors = arrFactors
  11. end function

PHP

  1. function factors($x)
  2. {
  3.     for($i = 2; $i <= x; $i++)
  4.     {
  5.         if ($x % $i == 0)
  6.         {
  7.             $arrFactors[] = $i;
  8.         }
  9.     }
  10.     return $arrFactors;
  11. }

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