-1
\$\begingroup\$

I managed to make a simple function that calculates the Hailstone sequence of numbers and print it out, and I just done a calculator that grabs the number with the largest sequence smaller than n — and it may disprove the Collatz conjecture.

Code with input numbers:

#include <iostream>
#include <string>

using namespace std;

void parseSequence(long n, long o) {
    long m = n;
    if (m % 2 == 0) {
        m /= 2;
    }
    else if (m != 1) {
        m *= 3;
        m++;
    }
    cout << m << endl;
    if (m == 1) {
        cout << "End of sequence! (sequence's lenght is " << o << ")." << endl;
    }
    else {
        o++;
        parseSequence(m, o);
    }
}

int main() {
    string p;
    long n;
    cout << "\nWhich is the number to calculate the Hailstone sequence?\n";
    cin >> n;
    parseSequence(n, 1);
    cout << "Parse more sequence?\n\n(y|n)\n\n:";
    cin >> p;
    if (p == "y" or p == "Y") {
        return main();
    }
    else return 0;
}

Calculator that displays number's largest sequence lenght with it's sequence's lenght:

#include <iostream>
#include <string>

using namespace std;

int parseSequence(long n, long o) {
    long m = n;
    if (m % 2 == 0) {
        m /= 2;
    }
    else if (m != 1) {
        m *= 3;
        m++;
    }
    cout << m << endl;
    if (m == 1) {
        cout << "End of sequence! (sequence's lenght is " << o << ")." << endl;
        return o;
    }
    else {
        o++;
        return parseSequence(m, o);
    }
}

int main() {
    string p;
    long n;
    cout << "\nWhich is the max number to calculate the longest sequence?\n";
    cin >> n;
    long l;
    long q;
    long y;
    long i;
    q = 0;
    for ( i = 1; i <= n; i++) {
         l = parseSequence(i, 0);
         if (l > q) {
                q = l;
                y = i;
         }
    }
    cout << "The number with the largest sequence : It's sequence's lenght = " << y << " : " << q;
    return 0;
}

I call the last one Project LightyMoon.

\$\endgroup\$
  • 2
    \$\begingroup\$ Long before you disprove the Collatz conjecture, you're likely to prove that numeric overflow exists. Take the largest representable number n and then calculate n*3+1. Oops. Collatz is safe. \$\endgroup\$ – Edward Dec 23 '15 at 14:21
  • \$\begingroup\$ @Edward The solution is simple. Ask NASA to compute it for us. I've heard of they have giant supercomputers... \$\endgroup\$ – Gustavo6046 Dec 23 '15 at 14:26
3
\$\begingroup\$

Never call main recursively.

It is undefined behavior. Use a loop instead.

Don't use recursion excessively.

In the case of parseSequence recursion actually makes it harder to understand and perform much worse. Usually each step for recursion takes a little bit of memory - something you want to avoid in this case.

Use a loop instead, in this case a for loop.

Use meaningful variable and function names

parseSequence does not parse a sequence. In the current form computes one step of a sequence.

One character names are generally frowned upon. Use descriptive names such as length, etc..

Avoid using namespace std;

See https://stackoverflow.com/a/1452738/620382

\$\endgroup\$
  • \$\begingroup\$ Thanks for the advice. It was a website where I learnt C++ and it told to use "using namespace std". But other than that, it was quite obvious and easy to understand. Was there something cool or innovative in my 2nd calculator code that called your attention? \$\endgroup\$ – Gustavo6046 Dec 23 '15 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.