|
|||||||||||||||||||||||||||||
| Using Prime Numbers for Category Exclusivity by Greg O'Lone, Stretched Out Software Inc. Updated March 2005 Background: We had a customer not too long ago who wanted to create a vendor directory on his web site. He wanted the standard stuff, of course (Name, Address, Tel, URL, etc) and he also wanted to be able to choose a category (as with a telephone directory, for example) for each of the vendors. The catch was this: he wanted the ability to file any vendor in multiple categories, and he wanted the ability to add/edit/remove categories at will, without needing to call someone to do it for him. The Problem:
Our solution: After looking at several options (including just having a field which listed the categories), we settled on a system using prime numbers.
Here's approximately how it goes... Make a table to hold the categories with three fields:
Each item in this table would be assigned the next consecutive available prime number starting with 2.
When refering to a category in this list, use the CatPrime instead of the autonumber and multiply them together. If you wanted to file something under Carpets and Employment, you would use a value of 55 (5 * 11). Architects, Banquets and Dentists would be 42 (2*3*7). Make a table to hold the vendor information:
Now here's the real magic of this system...to find out if one of the vendors is in a specific category, you would compare the vendor's CatValue (55) against an individual category (lets say Carpets, which has a value of 5) using the modulo operator. (The modulo operator detects whether a number is evenly divisible by a specified value, and provides what is known in mathematics as a "remainder".) If you get a result of zero, the vendor would be in this category. If you get anything else, they're not. 55 modulo 5 equals zero. If we try this same check with another category like Dentists (7), 55 modulo 7 equals 6.
In VBScript you would use mod. Note that the modulo operator is available in an SQL statement. You can use it to filter data just like any other operator. There ARE some limitations to this theory: If you decide to work with an unsigned long integer (max 4294967295) for the CatValue, and the customer wishes to select all of the categories, you are limited to 9 categories because 2*3*5*7*11*13*17*19*23*25 overflows the long integer. You can however use a double floating point number (max ????) to extend your range a bit...to a maximum of 131 categories. It is important to remember that the overflow values vary between operating systems, and you should check this limit before using this theory. Another thing to remember is that if you set the CatValue to 1, the modulo operation will always return 1 and CatValue of 0 will always return 0. How do you find the next prime number in a sequence? Glad you asked. To speed things up a bit, we decided to pass the last maximum prime number in use to the function, but you'll get the idea:
Conclusion: We've used this technique in several capacities including resource directories (like the example above), custom computer system builders (matching motherboards with CPU's & Hard Drives), Mailing list categories, etc. March 2005 - UPDATE: Another project came up which required us to fine-tune the routines to find the next prime number. I made an assummption above which I shouldn't have, but the fix makes the routine even more efficient! If you look at the lines where the for-next loops are defined, the upper limit should be Lets say we are checking to see if 65 is prime (which it is not, but any number will do), lets see how the new algorithm would check the number...
This number is not prime because 65 / 5 = 13, but our old system would have told us this in 32 steps instead of 3, which for the most part is not a really big deal... now lets check a bigger number that is prime... 997
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Home - Services - Portfolio - History - Contact - Tech Articles --- Legal Notices - E-Pay Invoices