Saturday, February 18, 2017

Amusements

I ran across the following article in a math geek book I found at Barnes and Noble:




So basically, you sum up the values of ever element that is in an even position and then you take every element in an odd position and multiply it by two.  Then if it is a single digit add it to the sum.  If it is two digits, add each digit to the sum separately.  Then if the answer is evenly divisible by 10, it is a possible valid Card number.

So I came up with the following Pascal source:

For i := 1 to 16 Do
  Begin
    If (i Mod 2) <> 0 Then
      If CardArray[i] < 5 Then
        OddSum := OddSum + (CardArray[i] * 2)
      Else
      Begin
        OddSum := OddSum + 1;
        OddSum := OddSum + ((CardArray[i] * 2) - 10);
      End
    Else
      EvenSum := EvenSum + CardArray[i];
    ValidEdit.Text := IntToStr(EvenSum+OddSum);
  End;

CardArray is a table with the first sixteen elements loaded with each digit of a credit card and after running it through the above source it gave me a number evenly divisible by 10.  Except one time when I got one of the digits wrong, then it was not evenly divisible by 10.  So I have to say the algorithm works.

Cool, Huh???

No comments: