Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hostilefork/887afd1866c3d5288700 to your computer and use it in GitHub Desktop.
Save hostilefork/887afd1866c3d5288700 to your computer and use it in GitHub Desktop.
Test log of "zero-aware" consecutive digit counting algorithm
// "Fast way" results from code by Pham Trung
// ( as of this revision http://stackoverflow.com/revisions/26422842/6 )
// "Slow way" results from the following brute-force C++ code
long FSlowCore(int N, int K, int D, vector<int> & digits) {
if (N == 0) {
auto startPos = find_if(digits.begin(), digits.end(), [](int x) {return x != 0;});
if (startPos != digits.begin())
return 0;
if (search_n(startPos, digits.end(), K, D) != end(digits)) {
return 1;
} else
return 0;
}
long total = 0;
digits.push_back(0);
for (int curDigit = 0; curDigit <= 9; curDigit++) {
total += FSlowCore(N - 1, K, D, digits);
digits.back()++;
}
digits.pop_back();
return total;
}
long FSlow(int N, int K, int D) {
vector<int> digits;
return FSlowCore(N, K, D, digits);
}
//////////////////////////////////////////////////////////////////////////////////
when N = 0 and K = 0 and D = 6:
Fast way gives 1
Slow way gives 0
when N = 0 and K = 1 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 0 and K = 2 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 0 and K = 3 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 0 and K = 4 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 0 and K = 5 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 0 and K = 6 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 0 and K = 7 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 0 and K = 8 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 0 and K = 9 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 1 and K = 0 and D = 6:
Fast way gives 18
Slow way gives 9
when N = 1 and K = 1 and D = 6:
Fast way gives 1
Slow way gives 1
when N = 1 and K = 2 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 1 and K = 3 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 1 and K = 4 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 1 and K = 5 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 1 and K = 6 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 1 and K = 7 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 1 and K = 8 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 1 and K = 9 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 2 and K = 0 and D = 6:
Fast way gives 261
Slow way gives 90
when N = 2 and K = 1 and D = 6:
Fast way gives 18
Slow way gives 18
when N = 2 and K = 2 and D = 6:
Fast way gives 1
Slow way gives 1
when N = 2 and K = 3 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 2 and K = 4 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 2 and K = 5 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 2 and K = 6 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 2 and K = 7 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 2 and K = 8 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 2 and K = 9 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 3 and K = 0 and D = 6:
Fast way gives 3420
Slow way gives 900
when N = 3 and K = 1 and D = 6:
Fast way gives 261
Slow way gives 252
when N = 3 and K = 2 and D = 6:
Fast way gives 18
Slow way gives 18
when N = 3 and K = 3 and D = 6:
Fast way gives 1
Slow way gives 1
when N = 3 and K = 4 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 3 and K = 5 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 3 and K = 6 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 3 and K = 7 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 3 and K = 8 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 3 and K = 9 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 4 and K = 0 and D = 6:
Fast way gives 42300
Slow way gives 9000
when N = 4 and K = 1 and D = 6:
Fast way gives 3420
Slow way gives 3168
when N = 4 and K = 2 and D = 6:
Fast way gives 261
Slow way gives 261
when N = 4 and K = 3 and D = 6:
Fast way gives 18
Slow way gives 18
when N = 4 and K = 4 and D = 6:
Fast way gives 1
Slow way gives 1
when N = 4 and K = 5 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 4 and K = 6 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 4 and K = 7 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 4 and K = 8 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 4 and K = 9 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 5 and K = 0 and D = 6:
Fast way gives 504000
Slow way gives 90000
when N = 5 and K = 1 and D = 6:
Fast way gives 42300
Slow way gives 37512
when N = 5 and K = 2 and D = 6:
Fast way gives 3420
Slow way gives 3411
when N = 5 and K = 3 and D = 6:
Fast way gives 261
Slow way gives 261
when N = 5 and K = 4 and D = 6:
Fast way gives 18
Slow way gives 18
when N = 5 and K = 5 and D = 6:
Fast way gives 1
Slow way gives 1
when N = 5 and K = 6 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 5 and K = 7 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 5 and K = 8 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 5 and K = 9 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 6 and K = 0 and D = 6:
Fast way gives 5850000
Slow way gives 900000
when N = 6 and K = 1 and D = 6:
Fast way gives 504000
Slow way gives 427608
when N = 6 and K = 2 and D = 6:
Fast way gives 42300
Slow way gives 42048
when N = 6 and K = 3 and D = 6:
Fast way gives 3420
Slow way gives 3420
when N = 6 and K = 4 and D = 6:
Fast way gives 261
Slow way gives 261
when N = 6 and K = 5 and D = 6:
Fast way gives 18
Slow way gives 18
when N = 6 and K = 6 and D = 6:
Fast way gives 1
Slow way gives 1
when N = 6 and K = 7 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 6 and K = 8 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 6 and K = 9 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 7 and K = 0 and D = 6:
Fast way gives 66600000
Slow way gives 9000000
when N = 7 and K = 1 and D = 6:
Fast way gives 5850000
Slow way gives 4748472
when N = 7 and K = 2 and D = 6:
Fast way gives 504000
Slow way gives 499131
when N = 7 and K = 3 and D = 6:
Fast way gives 42300
Slow way gives 42291
when N = 7 and K = 4 and D = 6:
Fast way gives 3420
Slow way gives 3420
when N = 7 and K = 5 and D = 6:
Fast way gives 261
Slow way gives 261
when N = 7 and K = 6 and D = 6:
Fast way gives 18
Slow way gives 18
when N = 7 and K = 7 and D = 6:
Fast way gives 1
Slow way gives 1
when N = 7 and K = 8 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 7 and K = 9 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 8 and K = 0 and D = 6:
Fast way gives 747000000
Slow way gives 90000000
when N = 8 and K = 1 and D = 6:
Fast way gives 66600000
Slow way gives 51736248
when N = 8 and K = 2 and D = 6:
Fast way gives 5850000
Slow way gives 5770611
when N = 8 and K = 3 and D = 6:
Fast way gives 504000
Slow way gives 503748
when N = 8 and K = 4 and D = 6:
Fast way gives 42300
Slow way gives 42300
when N = 8 and K = 5 and D = 6:
Fast way gives 3420
Slow way gives 3420
when N = 8 and K = 6 and D = 6:
Fast way gives 261
Slow way gives 261
when N = 8 and K = 7 and D = 6:
Fast way gives 18
Slow way gives 18
when N = 8 and K = 8 and D = 6:
Fast way gives 1
Slow way gives 1
when N = 8 and K = 9 and D = 6:
Fast way gives 0
Slow way gives 0
when N = 9 and K = 0 and D = 6:
Fast way gives 279999944
Slow way gives 900000000
when N = 9 and K = 1 and D = 6:
Fast way gives 747000000
Slow way gives 555626232
when N = 9 and K = 2 and D = 6:
Fast way gives 66600000
Slow way gives 65427678
when N = 9 and K = 3 and D = 6:
Fast way gives 5850000
Slow way gives 5845131
when N = 9 and K = 4 and D = 6:
Fast way gives 504000
Slow way gives 503991
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment