“Elfproef” algorithm in C# for the Dutch “Burgerservicenummer”

In the Netherlands everyone has a unique personal number called “burgerservicenummer“.

It’s a 9 digit number that satisfies to an alternative on the so-called elfproef: if the burgerservicenummer is represented by ABCDEFGHI, then 9*A + 8*B + 7*C + 6*D + 5*E + 4*F + 3*G + 2*H + (-1*I) must be a multiple of 11.

The following is a fast integer-based algorithm in C# to validate a burgerservicenummer:

public static bool ValidateElfProef(int value)
{
int divisor = 1000000000;
int total = 0;
int result = value;
for (int i = 9; i > 1; i--)
total += i * Math.DivRem(result, divisor /= 10, out result);
int rest;
Math.DivRem(total, 11, out rest);
return result == rest;
}
public static bool ValidateElfProef(string value)
{
return ValidateElfProef(Convert.ToInt32(value));
}

This entry was posted on Thursday, September 25th, 2008 at 07:09 and is filed under .NET. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

5 Responses to “Elfproef” algorithm in C# for the Dutch “Burgerservicenummer”

It will also work with zero’s in front. When a string with a BSN, like “012345678”, is converted to an int it will get the int value 12345678. The algorithm can also validate nummbers shorter than nine digits.

Thanks for the sourcecode!
I tried your algorithm with the number 755490975, which did not work.
It needs to be for (int i = 9; i > 0; i–) for it to work,.

You said it works for numbers shorter than 9 digits, but since it starts with a fixed divisor and digits, it can’t really work if the number is shorter.
I adjusted it to work also with a different amount than 9 numbers to support general 11-proof, not just for IBAN.

public static bool ValidateElevenProef(int value)
{
int total = 0;
int result = value;
int digits = value.ToString().Length;
int divisor = (int)Math.Pow(10, digits);

for (int i = digits; i > 0; i–)
total += i * Math.DivRem(result, divisor /= 10, out result);

Thank you for your contribution. However my algorithm is a specific version of the 11-proof for the Dutch social security number called BSN, not to be confused with bank account numbers like IBAN. The number 755490975 is not a valid BSN indeed (see also this online BSN check).
I think your algorithm is the general 11-proof which is suitable for bank account numers.

But it can start with a zero so an int is not usable and you assume the length is correct.

Hi Roel,

It will also work with zero’s in front. When a string with a BSN, like “012345678”, is converted to an int it will get the int value 12345678. The algorithm can also validate nummbers shorter than nine digits.

Thanks for the sourcecode!

I tried your algorithm with the number 755490975, which did not work.

It needs to be for (int i = 9; i > 0; i–) for it to work,.

You said it works for numbers shorter than 9 digits, but since it starts with a fixed divisor and digits, it can’t really work if the number is shorter.

I adjusted it to work also with a different amount than 9 numbers to support general 11-proof, not just for IBAN.

public static bool ValidateElevenProef(int value)

{

int total = 0;

int result = value;

int digits = value.ToString().Length;

int divisor = (int)Math.Pow(10, digits);

for (int i = digits; i > 0; i–)

total += i * Math.DivRem(result, divisor /= 10, out result);

int rest;

Math.DivRem(total, 11, out rest);

return result == rest;

}

Hello Markus,

Thank you for your contribution. However my algorithm is a specific version of the 11-proof for the Dutch social security number called BSN, not to be confused with bank account numbers like IBAN. The number 755490975 is not a valid BSN indeed (see also this online BSN check).

I think your algorithm is the general 11-proof which is suitable for bank account numers.

Hi

I see, sorry for the confusion and thanks for clearing things up!