Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
"Improved" seven boom by `hasSeven()` being a one op and only the first digit tracking divisibility
#include <memory>
#include <ranges>
#include <iostream>
class Digit {
unsigned char val;
std::unique_ptr<Digit> moreSignificant;
static unsigned int sevens;
public:
Digit(unsigned char startVal) : val(startVal) {}
virtual void inc() {
if (val == 7)
sevens--;
if (++val > 9) {
val = 0;
if (moreSignificant)
moreSignificant->inc();
else
moreSignificant = std::make_unique<Digit>(1);
}
if (val == 7)
sevens++;
}
virtual bool hasSeven() {
return sevens > 0;
}
};
unsigned int Digit::sevens = 0;
class FirstDigit : public Digit {
unsigned char sevenCount;
public:
FirstDigit(unsigned char startVal) : Digit(startVal), sevenCount(startVal % 7) { }
virtual void inc() {
Digit::inc();
if (++sevenCount >= 7)
sevenCount = 0;
}
virtual bool isDivisibleBySeven() {
return sevenCount == 0;
}
};
int main() {
FirstDigit num(0);
for (unsigned long long i : std::ranges::views::iota(1ULL, 100000ULL)) { // while (true)
num.inc();
if (num.hasSeven() || num.isDivisibleBySeven())
std::cout << "BOOM" << std::endl;
else
std::cout << i << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment