Skip to content

Instantly share code, notes, and snippets.

@adamjedlicka
Created July 8, 2018 08:50
Show Gist options
  • Select an option

  • Save adamjedlicka/6c9192bb0ded74851bf6c06b1999ca77 to your computer and use it in GitHub Desktop.

Select an option

Save adamjedlicka/6c9192bb0ded74851bf6c06b1999ca77 to your computer and use it in GitHub Desktop.
#include <cmath>
#include <cstdint>
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
class HereditaryBaseNotation {
public:
int base;
uint64_t number;
vector<int> multipliers;
vector<HereditaryBaseNotation> exponents;
int c;
HereditaryBaseNotation(int base, uint64_t number) {
this->base = base;
this->number = number;
this->c = 0;
int res = this->number;
while (res > 0) {
int exp = highestExponentFor(res);
int multi = highestMultiplicatorFor(res, exp);
this->exponents.push_back(HereditaryBaseNotation(this->base, exp));
this->multipliers.push_back(multi);
res -= multi * pow(this->base, exp);
}
}
uint64_t calculate() {
uint64_t acc = 0;
for (uint i = 0; i < this->multipliers.size(); i++) {
acc += this->multipliers[i] *
pow(this->base, this->exponents[i].calculate());
}
return acc + this->c;
}
void goodsteinSequence() {
this->base++;
for (uint i = 0; i < this->multipliers.size(); i++) {
if (this->multipliers[i] == this->base) {
this->multipliers[i]++;
}
this->exponents[i].goodsteinSequence();
}
}
void sub() { this->c--; }
int highestExponentFor(int number) {
int exponent = 0;
while (true) {
if (pow(this->base, ++exponent) > number) {
return exponent - 1;
}
}
}
int highestMultiplicatorFor(int number, int exponent) {
int multiplicator = 1;
while (true) {
if (++multiplicator * pow(this->base, exponent) > number) {
return multiplicator - 1;
}
}
}
friend ostream &operator<<(ostream &os, const HereditaryBaseNotation &self) {
os << "(";
for (uint i = 0; i < self.multipliers.size(); i++) {
if (i != 0) {
os << " + ";
}
if (self.exponents[i].number == 0 && self.multipliers[i] == 1) {
os << 1;
continue;
}
if (self.multipliers[i] != 1) {
os << self.multipliers[i];
if (self.exponents[i].number != 0) {
os << "*";
}
}
if (self.exponents[i].number == 1) {
os << self.base;
} else if (self.exponents[i].number != 0) {
os << self.base << "^" << self.exponents[i];
}
}
if (self.c != 0) {
cout << self.c;
}
os << ")";
return os;
}
};
int main() {
int cycles = 7;
int iBase = 2;
int iNum = 13;
int base = iBase;
uint64_t tmp = iNum;
for (int i = 0; i < cycles; i++) {
HereditaryBaseNotation hbn(base, tmp);
hbn.goodsteinSequence();
hbn.sub();
base++;
tmp = hbn.calculate();
cout << hbn << endl;
cout << tmp << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment