Unrolling the loop and using Anonymonk's lookup trick extracts another 5%: int l[] = { 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 };
int c_luhn2( char *s ) {
int total = 0;
total += l[ s[ 0] - '0' ];
total += s[ 1] - '0';
total += l[ s[ 2] - '0' ];
total += s[ 3] - '0';
total += l[ s[ 4] - '0' ];
total += s[ 5] - '0';
total += l[ s[ 6] - '0' ];
total += s[ 7] - '0';
total += l[ s[ 8] - '0' ];
total += s[ 9] - '0';
total += l[ s[10] - '0' ];
total += s[11] - '0';
total += l[ s[12] - '0' ];
total += s[13] - '0';
total += l[ s[14] - '0' ];
total *= 9;
return total % 10;
}
I doubt there is much fat left in there :)
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
In the absence of evidence, opinion is indistinguishable from prejudice.
Suck that fhit
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|