markyrocks

3x+1 demo version 0.02 zeta

Apr 10th, 2022
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.35 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <iostream>
  3.  
  4. //######### as a word of caution to anyone who would attempt to use this, this code does contain memory leaks..... ###########
  5.  
  6.  
  7. using namespace std;
  8.  
  9. class bitlist {
  10. public:
  11.  
  12.     unsigned long long size;  
  13.     unsigned long long bytecount;
  14.     byte* masterlist;
  15.     unsigned long long index;
  16.     bitlist() :index{ 0 }, masterlist{ new byte[1]{0} }, size{ 0 }, bytecount{ 1 } {}
  17.     ~bitlist() {
  18.         if (masterlist) {
  19.             delete[] masterlist;
  20.         }
  21.     }
  22.  
  23.     void Realloc(unsigned long long ull) {
  24.         size_t newb = ull % 8 ? (ull >> 3) + 2 : (ull >> 3)+1;
  25.         byte* m = new byte[newb]{};
  26.         memcpy(m, masterlist, bytecount);
  27.         delete[] masterlist;
  28.         masterlist = m;
  29.         bytecount = newb ;
  30.         size = ull;
  31.     }
  32.  
  33.     bitlist& operator[](unsigned long long ull) {
  34.         if (ull > bytecount<<3) { Realloc(ull + 1); }
  35.         else if (ull > size) { size = ull + 1; }
  36.         index = ull;
  37.         return *this;
  38.     }
  39.     bool operator==(bool b) {
  40.         return check(masterlist[index >> 3]);
  41.     }
  42.     bool operator!=(bool b) {
  43.         return !check(masterlist[index >> 3]);
  44.     }
  45.     void operator=(bool b) {
  46.         set(masterlist[index>>3], b);
  47.     }
  48.     bool check(const byte& b) {    // bitwise intensifies
  49.         if (b & 1 << (index % 8)) { return true; }
  50.         return false;
  51.     }
  52.  
  53.     void set(byte& b, bool a) {
  54.         if (a) {
  55.             b |= 1 << (index % 8);
  56.         }
  57.         else if (check(b)){
  58.             b -= 1 << (index % 8);
  59.         }
  60.     }
  61.  
  62.     void print() {
  63.         for (auto i = 0; i < size; i++) {
  64.             index = i;
  65.             cout << check(masterlist[index>>3]) << " ";
  66.         }
  67.     }
  68.     void printNumbers() {
  69.         for (auto i = 0; i < size; i++) {
  70.             index = i;
  71.             if(check(masterlist[index >> 3]) )
  72.             cout << index<< " ";
  73.         }
  74.     }
  75. };
  76.  
  77. class node {
  78. public:
  79.    
  80.     node* prev;
  81.     node* current;
  82.     node* next;
  83.     unsigned long long root;
  84.     unsigned long long num;
  85.  
  86.     node(unsigned long long num = 0, unsigned long long root = 0, node* prev = nullptr, node* next = nullptr) : current{ nullptr },root { root }, prev{ prev }, next{ next }, num{ num }{}
  87.     node(const node& n) :num{ n.num }, root{ n.root }, prev{ n.prev }, next{ n.next }, current{ n.current }{}
  88.    
  89.     void copy(const node& other) {
  90.         num = other.num; root = other.root; prev = other.prev; next = other.next;
  91.     }
  92.     void operator=(const node& other) {
  93.         copy(other);
  94.     }
  95.  
  96. };
  97.  
  98. class nodeIT {
  99. public:
  100.     node* n;
  101.     nodeIT() :n{ new node } {
  102.     }
  103.  
  104.     nodeIT(nodeIT& other) :n{ nullptr } {
  105.         n = other.n;
  106.         other.n = nullptr;
  107.     }
  108.     ~nodeIT() {
  109.         if (n) {
  110.             delete n;
  111.         }
  112.     }
  113.     void operator=(nodeIT& other) {
  114.         copy(other);
  115.     }
  116.     void operator=(const node& node) {
  117.         copy(node);
  118.     }
  119.     void copy(nodeIT& other) {
  120.         *this = other; //iffy
  121.     }
  122.     void copy(const node& node) {
  123.         *n = node;
  124.     }
  125.  
  126. };
  127.  
  128. unsigned long long xPlusOne(unsigned long long ull) {
  129.     if (ull & 1) { return (ull * 3) + 1; } //0dd
  130.     return ull >> 1; //bitwise operations... beastmode unlocked
  131. }
  132.  
  133.  
  134. class tree {
  135. public:
  136.  
  137.     nodeIT* t;
  138.    
  139.     size_t size;
  140.     size_t capacity;
  141.  
  142.     tree() :t{ new nodeIT[10] }, size{ 0 }, capacity{ 0 } {}
  143.     ~tree() {
  144.         if (t) {
  145.             //delete t; throwing
  146.         }
  147.     }
  148.     void Realloc(size_t s) {
  149.         nodeIT* n = new nodeIT[s];
  150.         memcpy(n, t, capacity*sizeof(nodeIT));
  151.         //delete t; //throwing
  152.         t = n;
  153.         capacity = s;
  154.     }
  155.     void push_back(node n) {
  156.         if (size + 1 > capacity) {
  157.             Realloc(capacity + 10);
  158.             //cout << " re \n";
  159.         }
  160.         *t[size].n = n;
  161.         size++;
  162.     }
  163.     node* back() {
  164.         return t[size - 1].n;
  165.     }
  166.     size_t Size() {return size;}
  167.  
  168.     node* operator[](size_t index) {
  169.         return t[index].n;
  170.     }
  171.    
  172.  
  173.     node* FindNodeByVal(unsigned long long ull) {
  174.         for (auto i = 0; i < size; i++) {
  175.             if (t[i].n->num == ull) {
  176.                 return t[i].n;
  177.             }
  178.         }
  179.         return nullptr;
  180.     }
  181.  
  182.     node* FindNodeRoot(unsigned long long ull) {
  183.         for (auto i = 0; i < size; i++) {
  184.             if (!t[i].n->prev && t[i].n->root == ull) {
  185.                 return t[i].n;
  186.             }
  187.         }
  188.         return nullptr;
  189.     }
  190.  
  191.     void printBranch(unsigned long long ull) {
  192.         if (!ull) { return; }
  193.         node* n = FindNodeRoot(ull);
  194.         if (!n) { return; }
  195.         node* next = n->next;
  196.         cout << " root= " << n->root << " num = " << n->num << " prev = " << n->prev << " current = " << n << " next = " << n->next << "\n";
  197.         while(next->num!=1){
  198.             cout << " root= " << next->root << " num = " << next->num << " prev = " << next->prev << " current = " << next << " next = " << next->next  << "\n";
  199.             next = next->next;//...
  200.         }
  201.         n = next;
  202.         cout << " root= " << n->root << " num = " << n->num << " prev = " << n->prev << " current = " << n << " next = " << n->next << "\n";
  203.     }
  204.     void printTree() {
  205.         for (auto i = 0; i < branchCount()+1; i++) {
  206.             printBranch(i);
  207.             cout << "\n";
  208.         }
  209.     }
  210.    
  211.     unsigned long long branchCount() {
  212.         return back()->root;
  213.     }
  214.  
  215. };
  216.  
  217. int main() {
  218.  
  219.     unsigned long long in{};
  220.     bitlist bl;
  221.     tree t;
  222.  
  223.     for (unsigned long long i = 1; i < 20; i++) {
  224.         in = i;
  225.        
  226.         t.push_back({ i,i });
  227.         bl[in] = true;
  228.          
  229.         while (true) {
  230.             in = xPlusOne(in); //plucks a number
  231.  
  232.               if (bl[in] == true) { //if the number is already in the tree it is disregarded
  233.                
  234.                  t.back()->next = t.FindNodeByVal(in); // attaches last node in current branch to branch that has already found the end
  235.                break;
  236.                }
  237.            t.push_back({ in,i,t.back()});  //new branch node is made
  238.            t[t.Size() - 2]->next = t.back();// attach new node to the last
  239.  
  240.        
  241.             bl[in] = true; // updates the list of which numbers have been touched already.  
  242.         }
  243.     }
  244.     t.printTree();
  245.  
  246.  
  247. }
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
Advertisement
Add Comment
Please, Sign In to add comment