We have all been there, right?
How does the computer know the card number is invalid before you key in the rest? How does it know you hold a Visa card? Is someone watching? Or have your details been stolen?
Now before you spiral into a panic or realise you have never wondered š, I assure you this is not a conspiracy. I recently learnt about the Luhn algorithm which is a bedrock to card payment processing.
A quick aside: I am taking the CS50 course, conducted by Harvard Professor David Malan, over the next weeks while in-between jobs.
Week 1ās course work Credit was to implement a program in C that prompts the user for a credit card number, and reports whether it is a valid AMEX, Visa or MasterCard card number.
Iām unashamed to admit it took me hours to crack this, but that is beside the point of this post.
To me, the challenge served as a reminder of how oneās fixation on the first idea that comes to mind can stifle the development of often simpler alternatives.
The Luhn algorithm is best illustrated with an example whereby every second digit is āextractedā from the number. The notion of splitting the odd and even digits was glued to my mind. Ultimately, it became a burdensome baggage I dragged around while attempting for solution.
Determined to adhere to academic honesty, I dwelt in solitude, resisting the temptations of Google & stack overflow forums. Hours passed, and yet I couldnāt figure out how to make my program calculate the summation while āskipping overā every other digit. I tunnel visioned.
On the blink of resignation, my husband cheered me on.
āYouāre nearly there. Why donāt you take a quick look at how others have done this?ā
I succumbed to his suggestion, but continued to reject advanced solutions that utilised knowledge beyond the Week 1 syllabus. It was then I stumbled across an elegant use of the boolean expression, which I overlooked earlier. That was my light bulb moment š” - It had been there the whole time, I just didnāt see it.
āFirst impressions lastā carries weight. In this instance, it nearly caused my defeat.
Note to future self, be aware of
Anchoring bias, which explains why we tend to overweight the first piece of information we receive; and
Complexity bias because simplicity often requires hard work
Let me know what you think by leaving a comment.
For more posts like this, sign up with your email.
If you are so keen , here are my final code.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
//prompt input + reject incorrect format/ negative numbers
long n;
do
{
n = get_long("Number: ");
}
while (n <= 0);
//work out number of digits in total
int d;
long new_n = n;
for (d = 0; new_n > 0; d++)
{
new_n = new_n / 10;
}
//printf("Number of digits: %i\n", d);
if (d < 13 || d == 14 || d > 16)
{
printf("INVALID\n");
}
else
{
//run luhn's algo check
long temp_n = n;
int sum = 0;
bool flag = true;
while (temp_n > 0)
{
int digit = temp_n % 10;
int product;
if (flag == false)
{
product = 2 * digit;
if (product > 9)
{
product = 1 + (product % 10);
}
}
else
{
product = digit;
}
//printf("product: %i\n", product);
sum = sum + product;
//printf("sum: %i\n", sum);
temp_n = floor(temp_n / 10);
flag = !flag;
}
//check sum that ends with zero
if (sum % 10 == 0)
{
//check for AMEX ie 15 digits + starting w/ 34 or 37
if ((n >= 34 * pow(10, 13) && n < 35 * pow(10, 13)) || (n >= 37 * pow(10, 13) && n < 38 * pow(10, 13)))
{
printf("AMEX\n");
}
//check for Visa ie 13 digits or 16 digits starting w/ 4
else if (d == 13 || (n >= 4 * pow(10, 15) && n < 5 * pow(10, 15)))
{
printf("VISA\n");
}
//check for Master ie 16 digits starting w/ 51-55
else if (n >= 51 * pow(10, 14) && n < 56 * pow(10, 14))
{
printf("MASTERCARD\n");
}
else
{
printf("INVALID\n");
}
}
else
{
printf("INVALID\n");
}
}
}
After reading the computer stuff, my brain is full of dumplings. Yet I know it is only used to demonstrate a point. First impressions are always dominating which is analogous to love at first sight. If the first sighting does not impress then there won't be love. Ration will then outweigh passion.