[comp.lang.c] The 12121 check digit algorithm

ken@aiai.ed.ac.uk (Ken Johnson) (12/28/89)

A few months ago there was a conversation about check digit algorithms
in these groups, and I mentioned the `12121' algorithm.  I've recently
had cause to implement the damn thing in `C', having last done it in
Cobol in about 1972 when the Examine command was the latest thing! So
here are the relevant procedures.  I expect that for most applications
you should change `int' to `long' throughout. 


------------ cut here 8< ---------------------------------------------------

/*
 * The 12121 check digit algorithm. This version Ken Johnson, AIAI,
 * Edinburgh University, 1989. You may freely use, modify or copy
 * this code, declaim it in public houses or paint it on walls.
 * But if you sell it at a profit, I want a share please.
 * 
 * Supply_check_digit returns a number which is the supplied argument
 * shifted left a place, with the check digit inserted on the left hand
 * end. (Decimal, not binary, e.g. 192 --> 1922)
 * 
 * checks returns 1 or 0 depending on whether the input number ends in
 * the correct digit or not. In any `real' system you should also check
 * the sign and the number of digits, by adding a test at the
 * beginning that says `if (number < 1000 || number > 9999) {return(0)}'
 * 
 * check_digit_for computes and returns the check digit for a given number,
 * the number having first been stripped of any check digit.
 * How it works: Assume the number has the digits abcde. Then add together
 * a+c+e+2*b+2*d. To that total add 1 if b>=5 and another 1 if d>=5. The
 * check digit is that total modulo 10.
 * 
 *  $$ Good Luck $$
 */

int supply_check_digit(number)
int number;
{
	int check_digit_for( );

	return((number * 10) + check_digit_for(number));
}

checks(number)
int number;
{
	int check_digit_for( );

	return ((number % 10) == check_digit_for(number/10) ? 1 : 0);
}

static int check_digit_for(number)
int number;
{
	int check = 0;

	while (number > 0)
	{
		check += (number % 10);
		number /= 10;

		if (number % 10 >= 5)
		{
			++check;
		}

		check += 2 * (number % 5);
		number /= 10;
	}
	return(check % 10);
}
-- 
Ken Johnson, AI Applications Institute, 80 South Bridge, Edinburgh EH1 1HN
E-mail ken@aiai.ed.ac.uk, phone 031-225 4464 extension 212
`I have read your article, Mr Johnson, and I am no wiser now than when I
started'.  -- `Possibly not, sir, but far better informed.'

doug@xdos.UUCP (Doug Merritt) (01/04/90)

In article <1498@skye.ed.ac.uk> ken@aiai.UUCP (Ken Johnson) writes:
> * The 12121 check digit algorithm. This version Ken Johnson, AIAI,
> * Edinburgh University, 1989. You may freely use, modify or copy
> * this code, declaim it in public houses or paint it on walls.
> * But if you sell it at a profit, I want a share please.

If you seriously want the legal right to control commercial usage,
you must follow the international copyright law in such matters.
Since you gave no copyright notice (which is required to say "copyright"
along with your name & year of publication), this is now public domain.

I rather like public domain offerings, but thought you might like to
know for future reference. Thanks for contributing!
	Doug
-- 
Doug Merritt		{pyramid,apple}!xdos!doug
Member, Crusaders for a Better Tomorrow		Professional Wildeyed Visionary

lwh@harpsichord.cis.ohio-state.edu (Loyde W Hales) (01/05/90)

In article <599@xdos.UUCP> doug@xdos.UUCP (Doug Merritt) writes:
>In article <1498@skye.ed.ac.uk> ken@aiai.UUCP (Ken Johnson) writes:
>> * The 12121 check digit algorithm. This version Ken Johnson, AIAI,
>> * Edinburgh University, 1989. You may freely use, modify or copy
>> * this code, declaim it in public houses or paint it on walls.
>> * But if you sell it at a profit, I want a share please.
>
>If you seriously want the legal right to control commercial usage,
>you must follow the international copyright law in such matters.
>Since you gave no copyright notice (which is required to say "copyright"
>along with your name & year of publication), this is now public domain.

Technically, this is true.  No one should release something the rights of
which they want to keep without the ``copyright'' mark.  You should also
include your name.

Despite that, there is some legal protection offered in the above statement.
Not being a lawyer, I can't give you exact details.  BUT, if you can convince
a court that the statement has AUTHOR, DATE, and a STATMENT OF PROTECTION,
you may be able to hold your rights.  If you want to have the protection,
though, you do need to act.  Once your claim is publically challenged, you
have to take action to protect it, as I understand the law.

One other thing to note:  if you do plan to keep a copyright, you are well
advised to file a copyright form with your country's ``copyright'' office.
If you don't want to spend the filing fee, at least take measures to
demonstrate the date, like mailing a copy to yourself (certified) or having
everyone in your office sign a statement of date/release.

Reason: while you have a copyright just by writing ``(C) 1990 by Johnson,''
it may not be enforcable if you cannot demonstrate the date.  I know all
about the international agreement for patents/copyrights, but US law does
limit rights under many odd circumstances.  The safest way to protect
yourself is the copyright office.

Afterall, if I claim I wrote this code in 1986...

According to the US Office of Copyrights and Patents, you should minimally
print the following information on any copyrighted material, preferably as
close to quoting this as possible:

	Copyright 1990 by Ken Johnson
	All Rights Reserved by Author

Hope this is helpful to someone....



-=-

                                Department of Computer and Information Science
Loyde W. Hales, II                      The Ohio State University
lwh@cis.ohio-state.edu          2036 Neil Avenue Mall, Columbus, Ohio  43201

lwh@harpsichord.cis.ohio-state.edu (Loyde W Hales) (01/05/90)

> According to the US Office of Copyrights and Patents, you should minimally
> print the following information on any copyrighted material, preferably as
> close to quoting this as possible:

>        Copyright 1990 by Ken Johnson
>        All Rights Reserved by Author

To correct this, I should mention that the last line assumes you are holding
ALL rights.  In the case of the 12121 algorithm, follow it by your statement
of what rights you are keeping.


-=-

                                Department of Computer and Information Science
Loyde W. Hales, II                      The Ohio State University
lwh@cis.ohio-state.edu          2036 Neil Avenue Mall, Columbus, Ohio  43201

tat@pccuts.pcc.amdahl.com (Tom Thackrey) (01/05/90)

In article <599@xdos.UUCP> doug@xdos.UUCP (Doug Merritt) writes:
 >In article <1498@skye.ed.ac.uk> ken@aiai.UUCP (Ken Johnson) writes:
 >> * The 12121 check digit algorithm. This version Ken Johnson, AIAI,
 >> * Edinburgh University, 1989. You may freely use, modify or copy
 >> * this code, declaim it in public houses or paint it on walls.
 >> * But if you sell it at a profit, I want a share please.
 >
 >If you seriously want the legal right to control commercial usage,
 >you must follow the international copyright law in such matters.
 >Since you gave no copyright notice (which is required to say "copyright"
 >along with your name & year of publication), this is now public domain.
 >
 >I rather like public domain offerings, but thought you might like to
 >know for future reference. Thanks for contributing!
I just completed a class on software contracts.  We talked at length about
copyrights, patents, etc.  As I understand it, since the US has recently
agreed to follow the international copyright laws (Berne convention), it
is no longer a requirement to put a copyright notice in material to have
it considered copyrighted.  In fact, everything written has an implied
copyright with or without a notice.
-- 
Tom Thackrey sun!amdahl!tat00

[ The opinions expressed herin are mine alone. ]

wsmith@mdbs.UUCP (Bill Smith) (01/06/90)

 >From: doug@xdos.UUCP (Doug Merritt)
 >In article <1498@skye.ed.ac.uk> ken@aiai.UUCP (Ken Johnson) writes:
 >> * The 12121 check digit algorithm. This version Ken Johnson, AIAI,
 >> * Edinburgh University, 1989. You may freely use, modify or copy
 >> * this code, declaim it in public houses or paint it on walls.
 >> * But if you sell it at a profit, I want a share please.
 >
 >If you seriously want the legal right to control commercial usage,
 >you must follow the international copyright law in such matters.
 >Since you gave no copyright notice (which is required to say "copyright"
 >along with your name & year of publication), this is now public domain.
 >
 >I rather like public domain offerings, but thought you might like to
 >know for future reference. Thanks for contributing!

According to information disseminated by my University, US copyright law 
does not require a copyright notice to assert copyright (although it helps).

Certainly, lack of a copyright notice does not imply public domain in the
US.

Bill Smith
pur-ee!mdbs!wsmith
(not my employer's opinion.  not legal counsel.)

adrian@mti.mti.com (Adrian McCarthy) (01/06/90)

In article <75288@tut.cis.ohio-state.edu> Loyde W Hales
<lwh@cis.ohio-state.edu> writes:

>Reason: while you have a copyright just by writing ``(C) 1990 by Johnson,''
>it may not be enforcable if you cannot demonstrate the date.  I know all
>about the international agreement for patents/copyrights, but US law does
>limit rights under many odd circumstances.  The safest way to protect
>yourself is the copyright office.

Please note that "(c)" and "(C)" are *not* legitimate copyright symbols.  The
copyright symbol is the letter "c" inside a circle.  If you are in a
situation where you can't generate a true copyright symbol, you should
explicitly use the work "copyright".

Disclaimer:  I'm not an attorney; I'm just trying to be helpful.

Aid.

dts@quad.uucp (David T. Sandberg) (01/07/90)

In article <1473@mdbs.UUCP> wsmith@mdbs.UUCP (Bill Smith) writes:
: >From: doug@xdos.UUCP (Doug Merritt)
: >Since you gave no copyright notice (which is required to say "copyright"
: >along with your name & year of publication), this is now public domain.
:
:According to information disseminated by my University, US copyright law 
:does not require a copyright notice to assert copyright (although it helps).

Mr. Smith is correct here: until recently a copyright notice was required
to assert copyright, but that was changed last year by the terms of the
Berne Convention.

(I'm not a lawyer, just someone who has a lot of copyrighted material to
be concerned about...)

-- 
David Sandberg             dts@quad.uucp or ..uunet!rosevax!sialis!quad!dts
"What's the difference between 12-bit and 16-bit?  A lot more than 4 bits!"

staylor@pmdvax.UUCP (Scott Taylor) (01/08/90)

in article <1498@skye.ed.ac.uk>, ken@aiai.ed.ac.uk (Ken Johnson) says:
>  * check_digit_for computes and returns the check digit for a given number,
>  * the number having first been stripped of any check digit.
>  * How it works: Assume the number has the digits abcde. Then add together
>  * a+c+e+2*b+2*d. To that total add 1 if b>=5 and another 1 if d>=5. The
>  * check digit is that total modulo 10.

With all the copyright crap aside (why do you people bother arguing?)
a much better method, used by the Visa and MasterCard people, and heavily
in the COBOL world, is the following (which ken's seems to be a variant of):

	a + SUM(b) + c + SUM(d) + e

	Where:  SUM(x) :: The sum of the digits of x added to itself,
				e.g.  1 = 1+1 = 2
				      2 = 2+2 = 4
				      3 = 3+3 = 6
				      4 = 4+4 = 8
				      5 = 5+5 = 10 = 1 + 0 = 1
				      6 = 6+6 = 12 = 1 + 2 = 3
						You got the idea...

	Again, the check digit is MOD 10, however it will be the difference
of 10 less the check digit MOD 10 ( 9 -> 10 - 9 = 1).  This method is very
accurate at determining transposed digits too.  Try it on your Visa or MC.
For 16 digit cards, perform SUM on the odd number digits; for 13
digit Visa numbers, the even digits.  The last digit is the Check so
don't include it. 

I have algorithms in C and COBOL if anyone needs them.  I would have posted
them here already, however I had already posted the article.
-- 
Scott G. Taylor                                    Pmd Resources  (818) 991-0068
{wlbr,mahendo}!snidely!staylor                     31230 Cedar Valley Dr.
                                                   Westlake Village, CA  91362
		        "Vienoti Latvijai!"