Skip to content

Instantly share code, notes, and snippets.

@guss77
Created August 7, 2021 13:10
Show Gist options
  • Save guss77/740790b99460804bad3e25a49e16eca4 to your computer and use it in GitHub Desktop.
Save guss77/740790b99460804bad3e25a49e16eca4 to your computer and use it in GitHub Desktop.
"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