luhn.py

Created by ews31415

Created on January 10, 2021

375 Bytes

Lunh Algorithm - tests an integer, like a credit card, to see if the Lunh algorithm. This is used to check to see if a credit card number is valid.

Source: “Lunh algorithm” https://www.geeksforgeeks.org/luhn-algorithm/ GeeksforGeeks Retrieved January 1. 2021


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")