luhn.py

Created by ews31415

Created on January 11, 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")

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.