Proposal: IntExtension "Crossfoot"

Mar 23, 2011 at 4:51 PM

Due to a challenge on Pex4Fun.com some collegues and I solved a challenge called "Easy Peasy". It is basically about the calculation of the crossfoot of an integer.

Example:
Crossfoot of 123 = 6 (1+2+3)

Our solution evolved from complex for loops over Linq-approaches back to a simple mathematical for-loop that goes like this:

int sum = 0;
for (; i > 0; sum += i % 10, i /= 10) ;
return sum;
The loop iterates through all digits of the number by using the modulu operator and sums everything up. My suggestion was putting this into an extension method:
public static int CrossFoot(this int i)   {
  int sum = 0;
  for (; i > 0; sum += i % 10, i /= 10) ;
  return sum;
}
Wouldn't this one fit nicely into the .NET Extension Library?