Sunday, February 14, 2021

HP Prime and Numworks: Luhn Algorithm

 HP Prime and Numworks: Luhn Algorithm


The Luhn Algorithm:   Is Your Credit Card Number Valid?


The Luhn algorithm, created by IBM scientist Hans Peter Luhn in 1954, also known as the modulus 10 algorithm, is a checksum algorithm used to check the validity certain numbers used in financial transactions, such as identification numbers and credit card numbers. The algorithm is in public domain and serves as a basic check.  


The general steps of the Luhn Algorithm:


1.  Starting at the rightmost digit, double every other digit (2nd digit from the rightmost digit, 4th digit from right, 6th digit from right, and so on)


2.  If the digit that is doubled is at least 10, add the digits together.  


Replace 10 with 1.

Replace 12 with 3.

Replace 14 with 5.

Replace 16 with 7.

Replace 18 with 9.


3.  Take the sum of all the digits.


4.  If the sum is a multiple of 10 (sum modulo 10 is equal to zero), the number is considered valid.


Example:  4366357


For illustration purposes, let's separate each of the digits:


4  3  6  6  3  5  7


There are 7 digits.  


2nd digit from the most right:  5.  Double it to 10.  1 + 0 = 1


4  3  6  6  3  1  7


4th digit from the most right:  6.  Double it to 12.  1 + 2 = 3


4  3  6  3  3  1  7


6th digit from the most right:  3.  Double it to 6.  


6  6  3  3  1  7


Now add the digits:  4 + 6 + 6 + 3 + 3 + 1 + 7 = 30


10 divides into 30 evenly, or 30 mod 10 = 0.   The integer 4366357 is valid according to the Luhn algorithm.  


HP Prime Program:  LUNH


EXPORT LUHN()

BEGIN

// Luhn Algorithm - HP Prime

// strings are used to accommodate

// string: type 2

LOCAL s,l,k,c,t,n;


INPUT({{s,[[2]]}},

"Luhn Algorithm",

{"N?"},{"Integer"});

l:=DIM(s);

k:=l;


// total

t:=0;


// loop

WHILE k>0 DO

n:=EXPR(MID(s,k,1));

IF FP((l-k)/2)==0.5 THEN

IF n≠0 THEN

n:=(2*n-1) MOD 9+1;

END;

END;

t:=t+n;

k:=k-1;

END;


// final results

IF t MOD 10==0 THEN

RETURN "VALID";

ELSE

RETURN "NOT VALID";

END;


END;


Numworks Python Script:  luhn.py


from math import *

# Luhn Algorithm

# 2021-01-10 EWS

# Numworks Python


# strings are used

s=str(input('Integer? '))

l=len(s)

k=l


# total

t=0


# loop

while k>0:

  n=float(s[k-1])

  f=(l-k)/2

  if f-int(f)==0.5:

    if n!=0:

      n=fmod(2*n-1,9)+1

  t=t+n

  k=k-1


# final results

if fmod(t,10)==0:

  print("VALID")

else:

  print("NOT VALID")



Examples


38348 returns NOT VALID


14956 returns NOT VALID


4382 returns VALID



Source


"Luhn algorithm" GeeksforGeeks. Noida, Uttar Pradesh.  https://www.geeksforgeeks.org/luhn-algorithm/  Retrieved January 1, 2021.


"Luhn algorithm".  Wikipedia.  Last Updated December 23, 2020.  https://en.wikipedia.org/wiki/Luhn_algorithm  Retrieved January 1, 2021.  



Eddie


All original content copyright, © 2011-2021.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.