I recently wrote a quick code to find if a numeric array is ordered i.e sorted in a ascending or descending order. I needed it to check a sort algorithm I had written. The problem is that the following code has a worst-case running time of *O(N)*. Can we make the running time logarithmic.

(more…)

# algorithms

# Luhn algorithm for validating credit cards

The Luhn algorithm also known as the “modulus 10” or “mod 10” algorithm, is a checksum formula which can be used to validate credit card numbers. Developed in the 1950’s by IBM scientist Hans Peter Luhn and described in U.S. Patent 2,950,048. A PHP implementation is shown below.

function LuhnCheck($strDigits) { $sum = 0; $alt = false; for($i = strlen($strDigits) - 1; $i >= 0; $i--) { if($alt) { $temp = $strDigits[$i]; $temp *= 2; $strDigits[$i] = ($temp > 9) ? $temp = $temp - 9 : $temp; } $sum += $strDigits[$i]; $alt = !$alt; } return $sum % 10 == 0; } |

# Linked List implementation in PHP

Data structures are one of the core elements of computer science and they are also fun to program. But being a web developer the opportunity for implementing them in any application is quite rare. But then I thought, What the heck! I can just code for the joy of it, and it will also help me brush up on my DS skills. So here it is, a single linked list implementation in PHP for whoever may care. I will be posting other data structure and algorithm implementations here, so keeping watching. The code is given below.

(more…)