katze
❔ Making collatz conjecture code more efficient
hello, i recently tried to make a C# program that runs the collatz conjecture on random numbers and saves the conjecture to a .txt file; however: its really inefficient and i was wondering if anyone had any suggestions on how to improve it?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text;
using System.Linq;
namespace COLLATZCONJECTURE
{
class Program
{
static void Main(string[] args)
{
Random randgen = new Random();
UnicodeEncoding encoder = new UnicodeEncoding();
Console.ReadLine();
start:
var values = new List<string>();
long selectedNum = Convert.ToInt64(randgen.Next());
long x = selectedNum;
string localPath = (@"D:\COLLATZ\" + Convert.ToString(x) + ".txt");
if (File.Exists(localPath)) {
goto start;
}
Console.WriteLine("Input: {0}", selectedNum);
while (x > 0 && x != 1)
{
if (x == 1)
{
Console.WriteLine("END --- {0}", Convert.ToString(selectedNum));
break;
}
if (x % 2 == 0)
{
x = x / 2;
Console.WriteLine(Convert.ToString(x));
}
else if (x % 2 != 0)
{
x = (x * 3) + 1;
Console.WriteLine(Convert.ToString(x));
}
values.Add(Convert.ToString(x));
}
values.Add("END -- INPUT: " + Convert.ToString(selectedNum));
File.WriteAllLines(localPath,values);
// Console.WriteLine(values);
// Console.ReadKey();
goto start;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text;
using System.Linq;
namespace COLLATZCONJECTURE
{
class Program
{
static void Main(string[] args)
{
Random randgen = new Random();
UnicodeEncoding encoder = new UnicodeEncoding();
Console.ReadLine();
start:
var values = new List<string>();
long selectedNum = Convert.ToInt64(randgen.Next());
long x = selectedNum;
string localPath = (@"D:\COLLATZ\" + Convert.ToString(x) + ".txt");
if (File.Exists(localPath)) {
goto start;
}
Console.WriteLine("Input: {0}", selectedNum);
while (x > 0 && x != 1)
{
if (x == 1)
{
Console.WriteLine("END --- {0}", Convert.ToString(selectedNum));
break;
}
if (x % 2 == 0)
{
x = x / 2;
Console.WriteLine(Convert.ToString(x));
}
else if (x % 2 != 0)
{
x = (x * 3) + 1;
Console.WriteLine(Convert.ToString(x));
}
values.Add(Convert.ToString(x));
}
values.Add("END -- INPUT: " + Convert.ToString(selectedNum));
File.WriteAllLines(localPath,values);
// Console.WriteLine(values);
// Console.ReadKey();
goto start;
}
}
}
12 replies
❔ Trying to write to file of a Collatz Conjecture program
hello, complete beginner here. im trying to make a program that takes a random number and puts it through the collatz conjecture, then sends every number used in the collatz conjecture to a .txt file on a USB drive. (quite a mouthful, i know). ive got the part about the collatz conjecture done, but i cant write to a file; i tried using streamwrite and File.WriteAllLines with an array
static void Main(string[] args)
{
Random randgen = new Random();
UnicodeEncoding encoder = new UnicodeEncoding();
Console.ReadLine();
start:
var values = Array.Empty<string>();
int selectedNum = randgen.Next();
int x = selectedNum;
string localPath = (@"D:\COLLATZ\" + Convert.ToString(x) + ".txt");
Console.WriteLine("Input: {0}", selectedNum);
while (x > 0)
{
if (x == 1)
{
break;
}
if (x % 2 == 0)
{
x = x / 2;
Console.WriteLine("{0} | {1} | {2}", x, "div", Convert.ToString(x % 2));
}
else
{
x = (x * 3) + 1;
Console.WriteLine("{0} | {1} | {2}", x, "mul", Convert.ToString(x % 2));
}
values.Append(Convert.ToString(x));
}
File.WriteAllLines(localPath,values);
Console.WriteLine(String.Join(",", values));
Console.ReadKey();
goto start;
}
}
}
static void Main(string[] args)
{
Random randgen = new Random();
UnicodeEncoding encoder = new UnicodeEncoding();
Console.ReadLine();
start:
var values = Array.Empty<string>();
int selectedNum = randgen.Next();
int x = selectedNum;
string localPath = (@"D:\COLLATZ\" + Convert.ToString(x) + ".txt");
Console.WriteLine("Input: {0}", selectedNum);
while (x > 0)
{
if (x == 1)
{
break;
}
if (x % 2 == 0)
{
x = x / 2;
Console.WriteLine("{0} | {1} | {2}", x, "div", Convert.ToString(x % 2));
}
else
{
x = (x * 3) + 1;
Console.WriteLine("{0} | {1} | {2}", x, "mul", Convert.ToString(x % 2));
}
values.Append(Convert.ToString(x));
}
File.WriteAllLines(localPath,values);
Console.WriteLine(String.Join(",", values));
Console.ReadKey();
goto start;
}
}
}
8 replies