0
\$\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.

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Dec 23, 2015 at 13:05
\$\endgroup\$
2
  • 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\$ Commented Dec 23, 2015 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\$ Commented Dec 23, 2015 at 14:26

1 Answer 1

4
\$\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

answered Dec 23, 2015 at 13:52
\$\endgroup\$
1
  • \$\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\$ Commented Dec 23, 2015 at 13:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.