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:
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