Solving C++ 3n+1 Problem - Max Cycle Length w/ Code

  • Comp Sci
  • Thread starter chaoseverlasting
  • Start date
  • Tags
    C++
In summary: Try using unsigned long.BTW, i don't know if there's a 32 bit fundamental datatype ; maybe you're supposed to use user defined data types like arraysIf you want guaranteed size, use uint32_t (#include <stdint.h>).
  • #1
chaoseverlasting
1,050
3
Im trying to find the maximum cycle length, but the program hangs when i>100000 or so. Here's the code:

#include<iostream>

using namespace std;

int main()
{
unsigned int i, j;
int count, cmax;

int process(int x);
cout<<"Enter range:\nFrom: ";
cin>>i;
cout<<"\nTo: ";
cin>>j;
cout<<i<<" "<<j;

for(;i<=j;i++)
{
count=process(i);
if(count>cmax)
cmax=count;

}
cout<<" "<<cmax;

return 0;
}



int process(int x)
{
int count;
for(count=1;x!=1;++count)
{

if(x%2==0)
x/=2;
else
x=3*x+1;

}
return count;
}

I think the values become too large for integers to handle. I was told to assume that the values would not exceed 32 bits, aren't integers 2 bits though?
 
Physics news on Phys.org
  • #2
chaoseverlasting said:
I think the values become too large for integers to handle. I was told to assume that the values would not exceed 32 bits, aren't integers 2 bits though?

Unsigned int can go from 0 to 65536. Try using unsigned long.
BTW, i don't know if there's a 32 bit fundamental datatype ; maybe you're supposed to use user defined data types like arrays
 
  • #3
chaoseverlasting said:
Im trying to find the maximum cycle length, but the program hangs when i>100000 or so. Here's the code:

...

I think the values become too large for integers to handle. I was told to assume that the values would not exceed 32 bits, aren't integers 2 bits though?

Using unsigned ints is fine, using ints is not. You aren't using unsigned ints in a very critical calculation. Hint: your problem occurs with i=113383, count=120.

f(x) said:
Unsigned int can go from 0 to 65536. Try using unsigned long.
BTW, i don't know if there's a 32 bit fundamental datatype ; maybe you're supposed to use user defined data types like arrays

This is not true with most modern compilers/modern machines. Unsigned ints usually go from 0 to 4294967295 nowadays.

If you want guaranteed size, use uint32_t (#include <stdint.h>).
 
  • #4
x=3*x+1;

by pen&paper...calculate when your forloop should terminate given the condition ou have stated

cout's or printfs are your friend.
 
  • #5
D H said:
Using unsigned ints is fine, using ints is not. You aren't using unsigned ints in a very critical calculation. Hint: your problem occurs with i=113383, count=120.

If you want guaranteed size, use uint32_t (#include <stdint.h>).

How did you find that count? And how do you use uint32_t ?
 
  • #6
chaoseverlasting said:
How did you find that count?
The biggest problem is that you are not using unsigned ints in some very critical places. The maximum value for a signed int (assuming 32 bit integers) is 231-1=2147483647, which means the calculation x = 3*x + 1 will fail when x exceeds than (2147483647-1)/3=715827882. So how did I find when this occurs? I added a test for this event.

BTW, your program has another serious flaw. You should be able to do some simple cases by hand (e.g., 1, 2, 3). Do you get the right answers?

And how do you use uint32_t ?

You don't need this, but simply #include <stdint.h>.
 
  • #7
u should be able to declare it as
uint32_t...it'd be like using u_int64_t or int64_t.
 
  • #8
D H said:
The biggest problem is that you are not using unsigned ints in some very critical places. The maximum value for a signed int (assuming 32 bit integers) is 231-1=2147483647, which means the calculation x = 3*x + 1 will fail when x exceeds than (2147483647-1)/3=715827882. So how did I find when this occurs? I added a test for this event.

BTW, your program has another serious flaw. You should be able to do some simple cases by hand (e.g., 1, 2, 3). Do you get the right answers?



You don't need this, but simply #include <stdint.h>.

By default, arent int's 2 bits and floats 4 bits, double 8 bits and long double 16 bits?
 
  • #9
I take it you mean bytes, not bits. 2 bits doesn't represent much at all: 0,1,2,3, done. 2 bytes represents 0 to 65535.

The answer is still no. An unsigned short int and an unsigned int must be able to hold all integer values from 0 to 65535 (16 bits) while an unsigned long int must be able to hold all integer values from 0 to 4294967295 (32 bits). There is nothing to stop a compiler vendor from doing more than the minimum.

In particular, an "int" is supposed to be the "natural" size for the computer in question. In the days of 186 PCs, the natural word size was 16 bits, so ints were 16 bits on those machines. The natural word size on a 32-bit architecture machine is 32 bits; ints are much more likely to be 32 bits than 16 bits on such machines. Unless you work with a dinosaur, your computer has a 32-bit architecture, minimum, and ints will be 32 bits wide.
 
  • #10
simple test to help you go along way...
sizeof(datatype)...
 
  • #11
Im sorry to bring this up again, but I've been busy with college.

Im getting the right results when I run the program up until it hangs (I checked the sample input/output of the problem). I changed the int's to unsigned long int, but I still get the same problem... the program hangs somewhere around n=110000. I can't spot the major flaw. Could you give me a hint DH?
 
  • #12
Are you using unsigned long ints everywhere you might have a problem with signed integers? (Hint: the original program is not.)
 
  • #13
Yes... that's exactly what I did, and it worked... I got 525 as the longest cycle length. Thank you so very much!
 

Related to Solving C++ 3n+1 Problem - Max Cycle Length w/ Code

1. What is the C++ 3n+1 Problem?

The C++ 3n+1 Problem, also known as the Collatz Conjecture, is a mathematical problem that involves a sequence of numbers starting with a positive integer n. If n is even, divide it by 2. If n is odd, multiply it by 3 and add 1. The sequence will eventually reach 1, no matter what value of n is chosen.

2. What is the purpose of solving the C++ 3n+1 Problem?

The purpose of solving the C++ 3n+1 Problem is to gain a better understanding of number sequences and their patterns. It is also a popular problem in the field of computer science and can be used to test and improve programming skills.

3. How can I solve the C++ 3n+1 Problem?

There are several ways to solve the C++ 3n+1 Problem, but one common method is to use a recursive function that checks if the current number is even or odd and applies the appropriate calculations. The function will continue until the number reaches 1, and then the total number of steps taken will be returned as the maximum cycle length.

4. What is the maximum cycle length in the C++ 3n+1 Problem?

The maximum cycle length in the C++ 3n+1 Problem is not known for all numbers. However, it has been proven that the maximum cycle length for any number less than 2^60 is equal to 88. There are also certain numbers, known as "hailstone numbers," that have a very long cycle length and are still being studied by mathematicians.

5. Can I see an example of code for solving the C++ 3n+1 Problem?

Yes, here is an example of code written in C++ for solving the C++ 3n+1 Problem using a recursive function:

int maxCycleLength(int n) {
 if (n == 1)
  return 0;
 if (n % 2 == 0)
  return 1 + maxCycleLength(n/2);
 else
  return 1 + maxCycleLength(3*n+1);
}

This function takes in a positive integer n as the starting value and returns the maximum cycle length in the sequence.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
773
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
881
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top