LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2008, 04:17 PM   #1
rec3g
LQ Newbie
 
Registered: Mar 2008
Posts: 12

Rep: Reputation: 0
a program for the 3x+1 problem


okay so I'm trying to write a program that will calculate the number of steps it takes to get to 1 in the 3x+1 problem. The rules for the 3x+1 problem are as follows:

If a number is even divide by 2
If a number is odd, triple it and add 1
Stop if you ever get to 1



I have a program written but it only gives me an answer of 4 everytime. I don't understand why though! Can anyone point out what my problem is???

Quote:
#include <cmath>
#include <iostream>

using namespace std;

//Function prototype.
int UlamSequenceLength(int& number);


int main()
{
int number;
long count;
long x;

cout << "Enter number: " << endl;
cin >> number;

while (number != 1)
{
UlamSequenceLength(number);
count++;
}

x = UlamSequenceLength(number);

cout << x << endl;

}

int UlamSequenceLength(int& number)
{
if(number%2 == 0)
number = number/2;
else
number = (number * 3) + 1;
return number;
}
 
Old 03-29-2008, 04:51 PM   #2
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Lightbulb

By the time the statement

Code:
x = UlamSequenceLength(number);
is executed, number will always be 1. Thus, you'll always get the hailstones sequence 1, 4, 2, 1 when you call UlamSequenceLength a second time. Why are you doing that?

Last edited by Dan04; 03-29-2008 at 05:14 PM.
 
Old 03-29-2008, 04:56 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Please post code in code tags, not quote tags. code tags will preserve indentation, making your posted code a lot easier to read (assuming you have sane indentation in th original).

It looks like homework, so I won't try to answer the question directly. I would suggest stepping through the program with a debugger to understand what it is doing.
 
Old 03-29-2008, 08:35 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Also, don’t you want to print count (not x)?
 
Old 03-30-2008, 10:19 AM   #5
rec3g
LQ Newbie
 
Registered: Mar 2008
Posts: 12

Original Poster
Rep: Reputation: 0
I'm so confused... if I assigned count to x...why would it matter which one I print? but I did remove the x and tried to just print count and it gave me some off the wall negative number. And Dan04..why am I doing what?
 
Old 03-30-2008, 10:37 AM   #6
rec3g
LQ Newbie
 
Registered: Mar 2008
Posts: 12

Original Poster
Rep: Reputation: 0
Code:
#include <cmath>
#include <iostream>

using namespace std;

//Function prototype.
int UlamSequenceLength(int& number);
	

int main()
{
	int number;
	long count;

	
	cout << "Enter number: " << endl;
	cin >> number;
	
	while (number != 1)
	{
		UlamSequenceLength(number);
		count++;	
	}
	
	
	cout << count << endl;
	
}

int UlamSequenceLength(int& number)
{
	if(number%2 == 0)
		number = number/2;
	else
		number = (number * 3) + 1;
	return number;
}
So this is my latest program..still not working ..I stink at programming
 
Old 03-30-2008, 11:30 AM   #7
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
You aren't initializing count.
 
Old 03-30-2008, 12:42 PM   #8
rec3g
LQ Newbie
 
Registered: Mar 2008
Posts: 12

Original Poster
Rep: Reputation: 0
wow...I'm stupid haha
 
Old 03-30-2008, 01:01 PM   #9
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by rec3g View Post
if I assigned count to x...why would it matter which one I print?
(In your first attempt) you never assigned count to x. You assigned to x the number in the sequence after 1 (which was invariably 4).
Quote:
Originally Posted by Dan04 View Post
You aren't initializing count.
Yes. Remember that local variables with automatic storage (those without the keyword static) are not initialized to anything (so the value they usually have is whatever data which was already on the stack).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
program problem megadeth Linux - Software 1 05-24-2005 09:58 PM
su program problem xaos5 Linux - Software 6 03-17-2005 01:02 PM
Problem with C program exvor Programming 27 01-04-2005 11:51 AM
Program installing problem nutz Linux - Software 5 11-23-2003 07:23 PM
C program problem.. Winter Programming 5 05-08-2002 05:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration