Jump to content

Any bored Recursive Custom Function gurus out there?


This topic is 6328 days old. Please don't post here. Open a new topic instead.

Recommended Posts

I'm trying to create a recursive custom function that will compute a special type of checksum digit.

I have a wrapper function that does some initial setup, and some sub-functions that are basically lookup tables, which are used in computing the checksum. All that stuff works fine.

I've go a working script that uses a loop to generate the checksum, and that is also working correctly. But I'm trying to move from a script to a recursive custom function, and I suck at thinking recursively. Anyone willing to give me a hand?

Link to comment
Share on other sites

A bit more information...

I've done some testing of my script against some presumably reliable sources, such as the CPAN implementation in Perl, and some reference tables, and the results look good.

The vCalcsum function I created in an attempt to reproduce this result is obviously broken, I just threw it together to have something in place I could experiment with.

The Verify function is pretty much the same thing as the generating function, except it includes the check digit, and if the end result is 0, it's a valid check digit, otherwise it's not.

The nice thing about this algorithm compared to others, like Luhn, etc., is that it will detect a single wrong digit, or any single transposed pair of digits, 100% of the time.

It also detects a big percentage of other common errors, like if you enter 323 instead of 232, for example, and detects many phonetic errors, like if you say ninety, but someone hears it as nineteen.

Link to comment
Share on other sites

Could you point us to the source of the algorithm?

Sure thing...

It's called the Verhoeff algorithm, and there are several good descriptions of it available online.

I started with the wikipedia entry, and then followed some of the links at the bottom of that page for source code and implementation examples.

http://en.wikipedia.org/wiki/Verhoeff_algorithm

Link to comment
Share on other sites

I have taken another look at this and noticed a problem: the input must be treated as text string instead of number, or the function will fail when the string has leading zeros.

Also, one function can accomodate both generating the check digit and verifying it.

Here's the new file - I *think* it works correctly, but it is really hard to come up with a good testing method for this.

Verhoeff.fp7.zip

Link to comment
Share on other sites

This is terrific, thanks! I'm planning to post this on Brian Dunning's site, with your permission.

Also, one function can accomodate both generating the check digit and verifying it.

True, that's one of the nice things about the algorithm, as long as the checksum is appended to the end of the number.

Here's the new file - I *think* it works correctly, but it is really hard to come up with a good testing method for this.

I'm testing by comparing to thousands of checksums generated using a known-good implementation, and it's looking good so far!

Link to comment
Share on other sites

Well, I haven't been able to come up with a test algorithm, so I guess that will have to do.

I don't think there's much else to be done, unless you got into heavy math.

As I understand it, the sequence of permutations is really arbitrarily chosen for its ability to detect typos, and you could replace it with a different P table and it would still generate a checksum, but just not detect the same sorts of errors.

Link to comment
Share on other sites

This topic is 6328 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.