markyrocks

3x+1 demo version 0.01 zeta

Apr 9th, 2022 (edited)
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1.  
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <Windows.h>
  6.  
  7.  
  8. using namespace std;
  9.  
  10. class bitlist {
  11. public:
  12.  
  13.     unsigned long long size;  
  14.     unsigned long long bytecount;
  15.     byte* masterlist;
  16.     unsigned long long index;
  17.     bitlist() :index{ 0 }, masterlist{ new byte[1]{0} }, size{ 0 }, bytecount{ 1 } {}
  18.     ~bitlist() {
  19.         if (masterlist) {
  20.             delete[] masterlist;
  21.         }
  22.     }
  23.  
  24.     void Realloc(unsigned long long ull) {
  25.         size_t newb = ull % 8 ? (ull >> 3) + 2 : (ull >> 3)+1;
  26.         byte* m = new byte[newb]{};
  27.         memcpy(m, masterlist, bytecount);
  28.         delete[] masterlist;
  29.         masterlist = m;
  30.         bytecount = newb ;
  31.         size = ull;
  32.     }
  33.  
  34.     bitlist& operator[](unsigned long long ull) {
  35.         if (ull > bytecount<<3) { Realloc(ull + 1); }
  36.         else if (ull > size) { size = ull + 1; }
  37.         index = ull;
  38.         return *this;
  39.     }
  40.     bool operator==(bool b) {
  41.         return check(masterlist[index >> 3]);
  42.     }
  43.     bool operator!=(bool b) {
  44.         return !check(masterlist[index >> 3]);
  45.     }
  46.     void operator=(bool b) {
  47.         set(masterlist[index>>3], b);
  48.     }
  49.     bool check(const byte& b) {    // bitwise intensifies
  50.         if (b & 1 << (index % 8)) { return true; }
  51.         return false;
  52.     }
  53.  
  54.     void set(byte& b, bool a) {
  55.         if (a) {
  56.             b |= 1 << (index % 8);
  57.         }
  58.         else if (check(b)){
  59.             b -= 1 << (index % 8);
  60.         }
  61.     }
  62.  
  63.     void print() {
  64.         for (auto i = 0; i < size; i++) {
  65.             index = i;
  66.             cout << check(masterlist[index>>3]) << " ";
  67.         }
  68.     }
  69.     void printNumbers() {
  70.         for (auto i = 0; i < size; i++) {
  71.             index = i;
  72.             if(check(masterlist[index >> 3]) )
  73.             cout << index<< " ";
  74.         }
  75.     }
  76. };
  77.  
  78. class node {
  79. public:
  80.     node* prev;
  81.     node* next;
  82.     node* current;
  83.     unsigned long long root;
  84.     unsigned long long num;
  85.  
  86.     node() :root{ 0 }, current { this }, prev{ nullptr }, next{ nullptr }, num{ 0 }{}
  87.     node(unsigned long long num = 0, unsigned long long root = 0, node* prev = nullptr, node* next = nullptr) :root{ root }, current { this }, prev{ prev }, next{ next }, num{ num }{}
  88.     node(node* prev) :root{ 0 }, current { this }, num{ 0 }, prev{ prev }, next{ nullptr }{}
  89.  
  90. };
  91.  
  92. unsigned long long xPlusOne(unsigned long long ull) {
  93.     if (ull & 1) { return (ull * 3) + 1; } //0dd
  94.     return ull >> 1; //bitwise operations... beastmode unlocked
  95. }
  96.  
  97. node* FindNode(const vector<node>& t, unsigned long long ull) {
  98.     for (auto i = 0; i < t.size(); i++) {
  99.         if (t[i].num == ull) {
  100.             return t[i].current;
  101.         }
  102.     }
  103.     return nullptr;
  104. }
  105.  
  106.  
  107. int main() {
  108.  
  109.  
  110.  
  111.     unsigned long long in{};
  112.     bitlist bl;
  113.     vector<node> tree{};
  114.  
  115.     for (auto i = 1; i < 10; i++) {
  116.         in = i;
  117.         tree.emplace_back(i, i); //makes root node
  118.         bl[in] = true;
  119.  
  120.         while (true) {
  121.             in = xPlusOne(in); //plucks a number
  122.             if (bl[in] == true) { //if the number is already in the tree it is disregarded
  123.            
  124.                 break;
  125.             }
  126.             tree.emplace_back(in, i, &tree[tree.size() - 1]);      //new branch node is made
  127.             tree[tree.size() - 2].next = &tree[tree.size() - 1];   // attach new node to the last
  128.             bl[in] = true; // updates the list of which numbers have been touched already.  
  129.         }
  130.     }
  131. //bl.printNumbers();
  132.  
  133.     for (auto& n : tree) {
  134.     cout << "num = " << n.num << " prev = " << n.prev << " current = " << n.current <<  " next = " << n.next << " root= " << n.root << "\n";
  135.     }
  136.  
  137. }
  138.  
  139.  
Advertisement
Add Comment
Please, Sign In to add comment