Learning about the Lhon’s Algorithm

Pilahi Moran
2 min readJun 29, 2021

Like many of my blogs start out, I recently learned this during my Python learning through Udemy.

Lhon’s Algorithm, one of many methods used to validate credit card information. More specifically it helps determine if a credit card number is valid. It was exciting both in better understanding algorithms but also because it was cool to see a basic way that this happens in our world of online shopping and how it could potentially used in future projects, personal or work related.

Now, today will talk about how the algorithm is broken down. With how much goes into the algorithm, it also gave me great practice on how to look at algorithms piece by piece. Like in my Flatiron School labs, take it one part at a time and once it works, on to the next.

With a standard 16 digit credit card number, there are 7 steps in finding if the number is valid or not.

  1. Break the numbers down to a list with and index.
  2. Gather the odd numbers and find the sum
  3. Gather the even numbers and find their doubles (example: 5 => 10, 2 =>4 etc.)
  4. For any with a double digit (10 and up) break that number down to single digits and find the sum (10 would break down to 1 + 0 = 1, 11 as 1 + 1 = 2 and so on)
  5. Now find the sum of evens
  6. Find the sum of the collection of odds and the collection of evens
  7. Divide by 10. If there is a remainder of 0 the card is valid.

It seems like a lot but also complicated with how much is needed to be done. Next post we will go into the simplified code and how to think it out to get through the algorithm.

--

--

Pilahi Moran

Full Stack Software Engineer, Artist, Cook, and Illustrator. Mother of 2 cockatiels and 2 cats.