PPaste!

brute.cc

Home - All the pastes - Authored by Thooms

Raw version

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <algorithm>
#include <cstring>
#include <experimental/functional>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

using ii = __uint128_t;

ii fib(ii n) {
  static auto known = std::unordered_map<__uint64_t, ii>{{0,0}, {1,1}, {2,1}};
  auto it = known.find(n);
  if (it != known.end())
    return it->second;

  if (n & 1) {
    ii k = (n+1)/2, fk = fib(k), fk1 = fib(k-1);
    return fk*fk + fk1*fk1;
  } else {
    ii k = n/2, fk = fib(k);
    return (2*fib(k-1) + fk)*fk;
  }
}

ii fibsum3(ii n) {
  return fib(n+6) - (n+5) - (n+2)*(n+3)/2;
}

class ascii_coded_decimals {
  char _n[128] = {0};
  char* _begin = std::end(_n) - 2;
  char* _end = std::end(_n) - 2;

public:
  ascii_coded_decimals() {
    *_begin = '1';
  }
  ascii_coded_decimals(const std::string& init) {
    _begin = std::copy_backward(init.cbegin(), init.cend(), _begin+1);
  }
  ascii_coded_decimals(ii n) {
    do {
      *_begin = "0123456789"[n % 10];
      n /= 10;
      --_begin;
    } while (n);
    ++_begin;
  }
  ascii_coded_decimals& operator ++() {
    char *p = _end;
    bool carry = true;
    while (carry && p > _n) {
      char c = *p;
      if (c) {
        ++c;
        carry = c > '9';
        if (carry)
          c = '0';
        *p = c;
      } else {
        *p = '1';
        carry = false;
      }

      --p;
    }

    if (p < _begin)
      _begin = p + 1;

    return *this;
  }
  const char* cbegin() const { return _begin; }
  const char* cend() const { return _end; }
  const char* cstr() const { return _begin; }
  size_t size() const { return _end - _begin + 1; }
};

void main14() {
  const std::vector<std::string> xs{
    "0", "1", "4", "25", "97", "9226935",
  };

  auto n = ascii_coded_decimals("2660000000");
  ii count = 43230504ULL;
  for(ii i = 0; i<10000000ULL; ++i) {
    std::string nstr{n.cstr(), n.size()};
    count += std::none_of(xs.cbegin(), xs.cend(), [&](const auto& x) -> auto {
        return nstr.find(x) != std::string::npos;
      });

    if (i % 10000000ULL == 0ULL || count == 103371362ULL) {
      std::cout << n.cstr() << "\t"
                << ascii_coded_decimals(count).cstr() << std::endl;
      if (count == 103371362ULL)
        break;
    }

    ++n;
  }
}

/*
void main17() {
  auto xs = std::vector< std::experimental::boyer_moore_searcher<const char*> >{};
  for (const char* x : {"0", "1", "4", "25", "97", "9226935"}) {
    auto searcher = std::experimental::make_boyer_moore_searcher<const char*>(x, x + std::strlen(x));
    xs.emplace_back(std::move(searcher));
  }

  auto n = ascii_coded_decimals("2660000000");
  ii count = 43230504ULL;
  for(ii i = 0; i<10000000ULL; ++i) {
    auto nstr = std::string{n.cstr(), n.size()};
    count += std::none_of(xs.cbegin(), xs.cend(), [&](auto x) -> auto {
      });

    if (i % 1000000ULL == 0ULL || count == 103371362ULL) {
      std::cout << n.cstr() << "\t"
                << ascii_coded_decimals(count).cstr() << std::endl;
      if (count == 103371362ULL)
        break;
    }

    ++n;
  }
}
*/

int main() { main14(); }