Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private static bool IsValidNumber(string number)
{
    int[] DELTAS = new int[] { 0, 1, 2, 3, 4, -4, -3, -2, -1, 0 };
    int checksum = 0;
    char[] chars = number.ToCharArray();
    for (int i = chars.Length - 1; i > -1; i--)
    {
       int j = ((int)chars[i]) - 48;
       checksum += j;
       if (((i - chars.Length) % 2) == 0)
           checksum += DELTAS[j];
    }

    return ((checksum % 10) == 0);
}


Can someone tell me, why we are using

int[] DELTAS = new int[] { 0, 1, 2, 3, 4, -4, -3, -2, -1, 0 };


whats the use of above code?

In last code

checksum += DELTAS[j];




we are basically adding , credit card number with these number { 0, 1, 2, 3, 4, -4, -3, -2, -1, 0 } one by one.

This seems more like a mathematical question rather than coding question.

But, i am stuck in this part.

Thanks in advance
Posted
Comments
PIEBALDconsult 22-Apr-15 12:51pm    
Looks odd to me, and "char[] chars = number.ToCharArray()" is pointless so I suspect that that code was written by an amateur and shouldn't be trusted.

DELTAS is used to perfom this step (source Wikipedia[^]) :
From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if the product of this doubling operation is greater than 9 (e.g., 8 × 2 = 16), then sum the digits of the products (e.g., 16: 1 + 6 = 7, 18: 1 + 8 = 9).


indeed the DELTAS array items represent the difference of the 'sum of digits of doubled number' and the number itself:

0 => 0 * 2 = 0 => DELTAS[0] = 0
1 => 1 * 2 = 2 => DELTAS[1] = 1 (because 2 - 1 = 1)
...
6 => 6 * 2 = 12 => 1 + 2 = 3 => DELTAS[6]=-3 (because 3 - 6 = -3)
...
 
Share this answer
 
v3
That code is definitely obfuscated; someone trying to be clever and missing.
Notice that simply making the DELTAS array static is a huge improvement.


Consider this code:

C#
public static int
Luhn
(
  string Value
)
{
  int sum = 0 ;
  int mul = 1 ;

  for ( int i = Value.Length - 1 ; i >= 0 ; i-- )
  {
    int val = (int) Value [ i ] - 48 ;

    if ( val >= 0 && val < 10 )
    {
      val *= mul ;

      sum += val / 10 + val % 10 ;

      mul ^= 3 ;
    }
  }

  return ( sum % 10 ) ;
}


Also, consider what happens with the code you posted when a number is passed in with SPACEs or HYPHENs.


Some benchmarking results:

1000000 in 00:00:00.2791342 -- What you posted
1000000 in 00:00:00.2380398 -- What you posted, without the ridiculous ToCharArray
1000000 in 00:00:00.6797738 -- What you posted, with char[] chars = number.Where ( x => x >= '0' && x <= '9' ).ToArray();
1000000 in 00:00:00.0848604 -- What you posted, with static DELTAS
1000000 in 00:00:00.0689142 -- What you posted, with static DELTAS, without the ridiculous ToCharArray
1000000 in 00:00:00.0681335 -- What you posted, with static two-dimensional DELTAS, without the ridiculous ToCharArray
1000000 in 00:00:00.0951645 -- What I posted
1000000 in 00:00:00.0818445 -- What I posted, without the test for non-digits
 
Share this answer
 
v7

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900