0

so I've written some C++ code which checks a 2^68 for a few calculations, and during the calculations, if it ever reaches 1, 2 or 4, it's supposed to end the calculations for it, add 1 to the initial number, and then repeat the whole process again. However, it doesn't seem to add 1 to the variable, and repeats the process for the same number over and over again.

Please let me know what is my issue. I've tried multiple methods, but they all seem to fail.

#include <iostream>
#include <bits/stdc++.h>
#include <cmath>

using namespace std;

void p(string s)
{
    cout << s + "\n\x1b[0m";
}
void cls()
{
    p("\033[2J\033[1;1H");
}

long double is = pow(2, 68);
long double x;
long double z;
long double i2 = (is / 2.0);
long double x2 = (x / 2.0);
long double z2 = (z / 2.0);
long double rem, fraction;

int main()
{
    cout.precision(200);
    
    while (3 > 2)
    {
        if ( modf(i2, &rem) != 0)
        {
            x = ((3.0 * is) + 1.0);
        } 
        else 
        {
            x = (is / 2.0);
        }
        
        while (3 > 2)
        {
            if ((x == 1.0) || (x == 2.0) || (x == 4.0))
            {
                cout<<is<<" Doesn't Work"<<endl;
                is += 1.0;
                break;
            }
            if (x == is)
            {
               
                p(to_string(is));
                p("success! Counterexample found!");
                break;
            } 
            else 
            {
                if ( modf(x2, &rem) != 0)
                {
                    z = ((3.0 * x) + 1.0);
                } 
                else 
                {
                    z = (x / 2.0);
                }
            }
            if ((z == 1.0) or (z == 2.0) or (z == 4.0))
            {
                cout<<is<<" Doesn't Work"<<endl;
                is += 1.0;
                break;
            }
            if (z == is)
            {
                p(to_string(is));
                p("success! Counterexample found!");
                break;
            } else {
                if ( modf(z2, &rem) != 0)
                {
                    x = ((3.0 * z) + 1.0);
                } else {
                    x = (z / 2.0);
                }
            }
            
        }
    }
    return 0;
}

Any help is appreciated.

  • 6
    `3 > 2` tends to be `true` for a very long time. – Eljay Apr 09 '22 at 12:43
  • the point is that the calculations go on forever. It doesn't end, well, atleast till it reaches the max limit of a long double in C++ (10^308 or something to my knowledge). – Tsar Asterov XVII Apr 09 '22 at 12:44
  • 6
    This seems like a good time to familiarize yourself with your debugger. You can step through the program's execution line by line and see exactly what it's doing. – paddy Apr 09 '22 at 12:46
  • 1
    Possibly related: [Is floating point math broken?](https://stackoverflow.com/q/588004/1553090) – paddy Apr 09 '22 at 12:49
  • 2
    At some point `is` won't have the precision to `is += 1.0;` because `1.0` is an insignificant value (since `long double` only has so many significand bits). – Eljay Apr 09 '22 at 12:49
  • I'm not very used to using a debugger, do u have any good resources to know how to use it – Tsar Asterov XVII Apr 09 '22 at 12:49
  • @Eljay what about cout.precision? – Tsar Asterov XVII Apr 09 '22 at 12:49
  • 3
    `cout.precision` doesn't affect how many *significand* bits are available to the data type. That only affects output for expressing the value. – Eljay Apr 09 '22 at 12:50
  • is there no way to increase the number of significant bits? every single bit it important for the calculations. – Tsar Asterov XVII Apr 09 '22 at 12:51
  • 1
    In IEEE 754, a `float` can express incremental integers up to 24-bits (including the implicit most significant bit), a `double` to 53-bits, and a `long double` (assuming 80-bit *extended precision format* on the x86) to 63-bits (there is no implicit most significant bit for the x86). – Eljay Apr 09 '22 at 12:55
  • 1
    Yes, you can increase the number of significant bits by using a "bignum" library. (I'm not familiar with any of them, so I can't recommend one from personal experience. There are quite a few options available.) – Eljay Apr 09 '22 at 12:55
  • Why are you using doubles for this? The algorithm you're trying to write is specifically related to integers. If you need more bits, either use a library or write something crude to jam two 64-bit integers together and do the operations yourself. – paddy Apr 09 '22 at 12:58
  • 1
    If you are using Visual Studio Community, Enterprise or Pro there is a debugging tutorial here: [https://learn.microsoft.com/en-us/visualstudio/debugger/quickstart-debug-with-cplusplus?view=vs-2022](https://learn.microsoft.com/en-us/visualstudio/debugger/quickstart-debug-with-cplusplus?view=vs-2022) other IDEs and standalone debuggers will have similar tutorials. – drescherjm Apr 09 '22 at 13:05
  • @paddy can you suggest any good libraries to increase the number of bits available for int? I can't seem to find any. – Tsar Asterov XVII Apr 09 '22 at 13:17
  • boost.multiprecision or GMP here is a list: [https://en.wikipedia.org/wiki/List_of_arbitrary-precision_arithmetic_software](https://en.wikipedia.org/wiki/List_of_arbitrary-precision_arithmetic_software) – drescherjm Apr 09 '22 at 13:20
  • see [Why should I not #include ?](https://stackoverflow.com/q/31816095/995714), [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/995714) – phuclv Apr 09 '22 at 15:38

1 Answers1

2

If you share the prize money with me and the Fields medal, then I will help you to solve the Collatz Conjecture. :-)

Please read here. And, especially:

Jeffrey Lagarias stated in 2010 that the Collatz conjecture "is an extraordinarily difficult problem, completely out of reach of present day mathematics."

Let us talk with the NSA to take over several of its server farms and then, in some hundred years, we will have a result.

You cannot tackle this with standard floating point arithmetic. It will never work. It is not even worth to start a try.

Use the "BigInt" library.

And get rid if your endless loops and other serious bugs.

Happy coding!

A M
  • 14,694
  • 5
  • 19
  • 44
  • Haha, you got me. I know Collatz Conjecture is incredibly difficult, I only made the code because I was curious on what I can do with it, and I was pretty bored. As for BigInt, I'll check it out, thanks! – Tsar Asterov XVII Apr 10 '22 at 10:45