Luhn or Luhn formula - also known as modulus 1 or mod 1.

Aravindh_Navision
Member Posts: 258
Hi Friends,
Does anyone worked in Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm (in Navision)?
Luhn algorithm is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, barcodes etc., Also see this link: http://en.wikipedia.org/wiki/Modulus_10
Am totally blank about this. Can anyone please provide me with how to handle this in Navision or with links which addresses this topic?
Thanks,
Aravindh
Does anyone worked in Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm (in Navision)?
Luhn algorithm is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, barcodes etc., Also see this link: http://en.wikipedia.org/wiki/Modulus_10
Am totally blank about this. Can anyone please provide me with how to handle this in Navision or with links which addresses this topic?
Thanks,
Aravindh
0
Comments
-
below i've put some sample code on a way to do this checksum. I've put this in a function with a text parameter(Ptxt_String) and a boolean return value.
Ltxt_resultstring is also a text, all the other variables are integers.clear(LInt_total); clear(LInt_TempInt); FOR i := 1 TO STRLEN(PTxt_String) - 1 DO BEGIN EVALUATE(LInt_TempInt,COPYSTR(PTxt_String,i,1)); IF (i MOD 2) <> 0 THEN BEGIN Ltxt_ResultString := Ltxt_ResultString + FORMAT(LInt_TempInt); END ELSE BEGIN LInt_TempInt *= 2; Ltxt_ResultString := Ltxt_ResultString + FORMAT(LInt_TempInt); END; END; EVALUATE(LInt_TempInt,COPYSTR(PTxt_String,STRLEN(PTxt_String),1)); Ltxt_ResultString := Ltxt_ResultString + FORMAT(LInt_TempInt); FOR i := 1 TO STRLEN(Ltxt_ResultString) DO BEGIN EVALUATE(LInt_TempInt,COPYSTR(Ltxt_ResultString,i,1)); LInt_total += LInt_TempInt; END; EXIT((LInt_total MOD 10) = 0);
0 -
geronimo wrote:below i've put some sample code on a way to do this checksum. I've put this in a function with a text parameter(Ptxt_String) and a boolean return value.
Ltxt_resultstring is also a text, all the other variables are integers.clear(LInt_total); clear(LInt_TempInt); FOR i := 1 TO STRLEN(PTxt_String) - 1 DO BEGIN EVALUATE(LInt_TempInt,COPYSTR(PTxt_String,i,1)); IF (i MOD 2) <> 0 THEN BEGIN Ltxt_ResultString := Ltxt_ResultString + FORMAT(LInt_TempInt); END ELSE BEGIN LInt_TempInt *= 2; Ltxt_ResultString := Ltxt_ResultString + FORMAT(LInt_TempInt); END; END; EVALUATE(LInt_TempInt,COPYSTR(PTxt_String,STRLEN(PTxt_String),1)); Ltxt_ResultString := Ltxt_ResultString + FORMAT(LInt_TempInt); FOR i := 1 TO STRLEN(Ltxt_ResultString) DO BEGIN EVALUATE(LInt_TempInt,COPYSTR(Ltxt_ResultString,i,1)); LInt_total += LInt_TempInt; END; EXIT((LInt_total MOD 10) = 0);
0 -
thanks for the hint MBerger, another new thing learned0
-
STRCHECKSUM won't work, because it sums up the weighted digits, not the cross sum thereof.
The following example is from WikipediaAssume an example of an account number "4992739871" that will have a check digit added, making it of the form 4992739871x:Account number 4 9 9 2 7 3 9 8 7 1 x Double every other 4 18 9 4 7 6 9 16 7 2 x Sum together all numbers 64 + x
To make the sum divisible by 10, we set the check digit (x) to 6, making the full account number 49927398716.
The account number 49927398716 can be validated as follows:- Double every second digit, from the rightmost: (1×2) = 2, (8×2) = 16, (3×2) = 6, (2×2) = 4, (9×2) = 18
- Sum all the individual digits (digits in parentheses are the products from Step 1): 6 + (2) + 7 + (1+6) + 9 + (6) + 7 + (4) + 9 + (1+8) + 4 = 70
- Take the sum modulo 10: 70 mod 10 = 0; the account number is probably valid.
geronimo's Code assumes an odd number of digits in it's Ptxt_String parameter to work correctly.1 -
This is the description of the algorithm from Wikipedia as above:
- Counting from the check digit, which is the rightmost, and moving left, double the value of every second digit.
- Sum the digits of the products (eg, 10 = 1 + 0 = 1, 14 = 1 + 4 = 5) together with the undoubled digits from the original number.
- If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; else it is not valid.
geronimo's code starts from the left.
Therefore it matters whether you have an even or odd number of digits to evaluate.
The code may easily be anhanced, though.0 -
vaprog wrote:This is the description of the algorithm from Wikipedia as above:
- Counting from the check digit, which is the rightmost, and moving left, double the value of every second digit.
- Sum the digits of the products (eg, 10 = 1 + 0 = 1, 14 = 1 + 4 = 5) together with the undoubled digits from the original number.
- If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; else it is not valid.
geronimo's code starts from the left.
Therefore it matters whether you have an even or odd number of digits to evaluate.
The code may easily be anhanced, though.
ah i must have read past that part it is indeed fairly easy to adapt the code however0 -
Hai all,
Thank you guys for joining in this discussion and providing solutions and your ideas. It was useful. The client had changed the logic now which is given below.
I need to find the verification digit to the following value: 3070841326319000361243033388633240620117 (Length - 39 char)
1. Sum odd positions [3, 7, 8, 1, 2, 3, 9, 0, 3, 1, 4, 0, 3, 8, 6, 3, 4, 6, 0, 1]. (SumOdd = 72)
2. Multiply SumOdd * 3. (Mult = 72 * 3 = 216)
3. Sum even positions [0, 0, 4, 3, 6, 1, 0, 0, 6, 2, 3, 3, 3, 8, 3, 2, 0, 2, 1]. (SumEven = 47)
4. Sum = Mult + SumEven, (Sum = 216 + 47 = 263)
5. Verification digit 7. (We need to add the integer value 7 to 263 so the nearest higher value which should divisible by 10 and the remainder remains 0.)
To find the lowest number (verification digit)...:
VeriDigit = 10 - (Sum - (int(Sum/10) * 10))
VeriDigit = 10 - (263 -(int(263/10) * 10))
= 10 - (263 - (26 * 10))
= 10 - (263 - 260) = 10 - 3
VeriDigit = 7
Can anyone please help me in solving this?
Thanks in advance,
Aravindh0 -
Aravindh_Navision wrote: »Hi Friends,
Does anyone worked in Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm (in Navision)?
Luhn algorithm is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, barcodes etc., Also see this link: http://en.wikipedia.org/wiki/Modulus_10
Am totally blank about this. Can anyone please provide me with how to handle this in Navision or with links which addresses this topic?
Thanks,
Aravindh
Please find my solution for NAV C/AL in the following post >> https://forum.mibuso.com/discussion/77912/luhn-algorithm-modulus-10-in-dynamics-nav-c-al-code-creating-a-check-digit/p1?new=1
This can easily be converted to work in Business Central AL code0 -
Aravindh_Navision wrote: »Hai all,
Thank you guys for joining in this discussion and providing solutions and your ideas. It was useful. The client had changed the logic now which is given below.
I need to find the verification digit to the following value: 3070841326319000361243033388633240620117 (Length - 39 char)
1. Sum odd positions [3, 7, 8, 1, 2, 3, 9, 0, 3, 1, 4, 0, 3, 8, 6, 3, 4, 6, 0, 1]. (SumOdd = 72)
2. Multiply SumOdd * 3. (Mult = 72 * 3 = 216)
3. Sum even positions [0, 0, 4, 3, 6, 1, 0, 0, 6, 2, 3, 3, 3, 8, 3, 2, 0, 2, 1]. (SumEven = 47)
4. Sum = Mult + SumEven, (Sum = 216 + 47 = 263)
5. Verification digit 7. (We need to add the integer value 7 to 263 so the nearest higher value which should divisible by 10 and the remainder remains 0.)
To find the lowest number (verification digit)...:
VeriDigit = 10 - (Sum - (int(Sum/10) * 10))
VeriDigit = 10 - (263 -(int(263/10) * 10))
= 10 - (263 - (26 * 10))
= 10 - (263 - 260) = 10 - 3
VeriDigit = 7
Can anyone please help me in solving this?
Thanks in advance,
Aravindh
Hello Aravindh,
Here is my take on a solution for your problem. It is similar to the solution I posted earlier.
I was initially confused by the results but I realized I had to take the last digit out of the number before putting it into the algorithm
But I must ask why the sum of odd numbers is multiplied by 3 specifically and is it always 3?
Anyways, here is the code below:
Variables:
Name DataType Subtype Length
i Integer
LengthOfDigit Integer
CodeCharacter Code 250
Product BigInteger
SumProduct BigInteger
SingleDigit Integer
SumOfOddNumbers Integer
SumOfEvenNumbers IntegerOrigCode := '307084132631900036124303338863324062011'; LengthOfDigit := STRLEN(OrigCode); CLEAR(SumOfOddNumbers); CLEAR(Product); CLEAR(SumProduct); FOR i := 1 TO LengthOfDigit DO BEGIN CodeCharacter[i] := COPYSTR(OrigCode,i,1); // Get the individual digit in that position //Check of the position of the digit is an odd number. All odd numbers will be (i MOD 2 = 1) or more // If Odd, the multiply by 2, else multiply by 1 //------------------------------------------------------------- IF i MOD 2 <> 0 THEN BEGIN //Odd numbers EVALUATE(SingleDigit[i],CodeCharacter[i]); //Convert code to an integer for computation SumOfOddNumbers := SumOfOddNumbers + SingleDigit[i]; END ELSE BEGIN EVALUATE(SingleDigit[i],CodeCharacter[i]); //Even Numbers SumOfEvenNumbers := SumOfEvenNumbers + SingleDigit[i]; END; END; Product := SumOfOddNumbers * 3; SumProduct := Product + SumOfEvenNumbers; //We can use the MOD function to get the number you mentioned in your comment. This is much faster than the formula of "VeriDigit = 10 - (Sum - (int(Sum/10) * 10))" IF SumProduct MOD 10 <> 0 THEN VerificationDigit := 10 - (SumProduct MOD 10) ELSE VerificationDigit := 0;
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions