To be clearer, I suppose that

L("XXYY") = ( L("XX") + L("YY") ) % 10

for all even length substrings XX and YY

=> a divide and conquer could be applied

=> computation complexity replaced with memory complexity.

> The OPed question posited 15 digits; that's a really long string.

One would do 3 lookups, leading zeros of the left most chunk shouldn't influence the checksum.

L("XX") = L("0XX") = L("00XX") ) = ...

> where do the 1 million entries come from?

6 digits sound like a good compromise, you'd need even amount and 100 Meg for 8 digits sound exaggerated. (A Perl array has an factor 10 overhead IIRC)

Anyway a C algorithm which fits into the line cache of the processor might still be faster.

Not sure if a 10k string for a 4 digit partition would always fit into the L1 cache.

update

the best approach might be a C program which dynamically benchmarks different chunk sizes to test the effect of line caches before building the lookup table.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice


In reply to Re^4: Faster Luhn Check Digit Calculation? by LanX
in thread Faster Luhn Check Digit Calculation? by kschwab

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.