Click here to Skip to main content
15,919,774 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
To get a Collatz sequence from a number, if it's even, divide it by two, and if it's odd, multiply it by three and add one. Continue the operation on the result of the previous operation until the number becomes 1.

As you see the code bottom, I tried to list the number in a line and after the line is printed I want to get another value by Console.ReadLine(); but it does not do what I intended.

Can you please give me some points or hints so I can fix it?

Thank you guys.

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercise
{
	class Program
	{
		static void Main(string[] args)
		{
			int sequenceNumber = Convert.ToInt32(Console.ReadLine());
			List<int> list = new List<int>();

			while (sequenceNumber>=1)
			{
				if (sequenceNumber == 1)
				{
					Console.WriteLine(1);
					sequenceNumber = Convert.ToInt32(Console.ReadLine());
				}

				else if(sequenceNumber>1)
				{
					while (sequenceNumber>=1)
					{
						if (sequenceNumber == 1)
						{
							list.Add(sequenceNumber);
						}

						else if (sequenceNumber % 2 == 0)
						{
							list.Add(sequenceNumber);
							sequenceNumber = sequenceNumber / 2;

						}
						else if (sequenceNumber % 2 != 0)
						{
							list.Add(sequenceNumber);
							sequenceNumber = sequenceNumber * 3 + 1;

						}
					}

					list.ForEach(Console.WriteLine);
					foreach (int i in list)
					{
						Console.Write(i + " ");

					}
				}//else

				sequenceNumber = Convert.ToInt32(Console.ReadLine());


			}    //while--sequence number

			

		}//main



	}
}
Posted
Updated 30-Aug-17 22:34pm
v3
Comments
Patrice T 30-Aug-17 20:58pm    
"but it does not do what I intended."
This us not informative, we don't know what you intended.
Tell us what it does and why it is wrong.

This is your homework assignment... You should solve it on your own. However, there is nothing stopping you from doing a Google Search: c# Collatz conjecture[^] which has a number of solutions that you can study... Like:

* The collatz conjecture[^]
* GitHub - genewitch/collatz: collatz conjecture simple C# code[^]
* GitHub - steelethis/Collatz_Conjecture: Implementation of the Collatz Conjecture in C#.[^]
* Collatz Conjecture - dcode.fr[^]
* Project Euler 14: Longest Collatz Sequence in C# | MathBlog[^]
* and more...
 
Share this answer
 
Your code is over engineered, in a "If Then Else" structure, you reach the Else part because the condition is not met, you don't need to check the not condition.
C#
if (sequenceNumber == 1)
{
}
else if(sequenceNumber>1)
{
}

can be simplified to
C#
// Here, you know thar (sequenceNumber >= 1) as per previous line
if (sequenceNumber == 1)
{
	// this part is executed because (sequenceNumber == 1)
}
else if(sequenceNumber>1)
{
	// Here, you know that (sequenceNumber >= 1) and (sequenceNumber != 1)
	// which combine to (sequenceNumber > 1)
}

Use the debugger to see what you code does as it execute.


There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
Graeme_Grant 30-Aug-17 22:33pm    
What-if the value was negative or zero? fail? :P
Patrice T 30-Aug-17 22:45pm    
The previous line is "while (sequenceNumber>=1)" which imply not negative and not zero. :p

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900