Collatz Tree Branch Calculator In C#

Collatz Tree Branch Calculator In C#

Postby Guest » Mon Mar 11, 2024 4:50 am

I have created a C# program which can determine branches on The Collatz Tree.
I believe my program is unique as it can determine both the parent branch and child branches for any given branch number.
Every branch on The Collatz Tree starts with an odd number at it's base, the branch is named after this odd number.

To use the program:
Enter an even integer and the program will determine which, odd numbered parent branch, the even integer is on.
Enter an odd integer and the program will determine the first few branches the that grow from that branch,
it will also show which parent branch the odd integer connects to.
The number for the parent branch can be manually inputted back into the program to continue the process again.

A visual of The Collatz Tree, for comparison, is available here:
https://en.wikipedia.org/wiki/File:Coll ... o_1000.svg

I am not a programmer so I expect my code may not use correct programming practices, though it seems to function as intended.


This is the program.

// Code for "Collatz Tree Branch Calculator.cs"
// Each branch is named after the odd number at its base.
// Program does not work for values higher than 999,999,998.

using System;

public class CollatzTreeBranchCalculator
{
static void Main()
{
int D, NoOfValues, aa, ab, ac, ad;
double b, bb, nb, bc, nc, ma, mb, mbb, mc, mcc, md, mdd, n;

Console.WriteLine("Enter an integer value to check");
D = Convert.ToInt32(Console.ReadLine());
// NoOfValues: Calculate values up to this number of steps along the current branch.
NoOfValues = 10;

if ((D % 2) == 0)
{
aa = D;
while ((aa % 2) == 0)
{
aa = aa / 2;
}

b = (Math.Log(D / aa)) / (Math.Log(2));
n = 3 * aa + 1;
Console.WriteLine("{0} is step {2} on the {1} branch, {1} is step 0",D,aa,b);
ma = n;
while ((ma % 2) == 0)
{
ma = ma / 2;
}
Console.WriteLine("The {0} branch, branches off from the {2} branch at {1}",aa,n,ma);
}
else
{

ab = D;
decimal i1 = Decimal.Divide((ab - 1), 6);
if ((i1 % 1) == 0)
{
Console.WriteLine("These are the first few child branches that attach to the {0} branch:",ab);
for (bb = 2; bb < (2 * NoOfValues); bb = bb + 2)
{
nb = (D * Math.Pow(2, bb));
mb = ((nb-1) / 3);
Console.WriteLine("{0} * (2 ^ {1}) = {2} {2} Connects to branch {3}",D,bb,nb,mb);
}
Console.WriteLine("");

mb = 3 * ab + 1;
mbb = mb;
while ((mbb % 2) == 0)
{
mbb = mbb / 2;
}
Console.WriteLine("The {0} branch, branches off from the {2} branch at {1}",D,mb,mbb);
}
else
{

}


ac = D;
decimal i2 = Decimal.Divide((ac + 1), 6);
if ((i2 % 1) == 0)
{
Console.WriteLine("These are the first few child branches that attach to the {0} branch:",ac);
for (bc = 1; bc < (2 * NoOfValues); bc = bc + 2)
{
nc = (D * Math.Pow(2, bc));
mc = ((nc-1) / 3);
Console.WriteLine("{0} * (2 ^ {1}) = {2} {2} Connects to branch {3}",D,bc,nc,mc);
}
Console.WriteLine("");

mc = 3 * ac + 1;
mcc = mc;
while ((mcc % 2) == 0)
{
mcc = mcc / 2;
}
Console.WriteLine("The {0} branch, branches off from the {2} branch at {1}",D,mc,mcc);
}
else
{

}


ad = D;
decimal i3 = Decimal.Divide((ad - 3), 6);
if ((i3 % 1) == 0)
{

Console.WriteLine("Branch {0} has no further branches",D);
md = 3 * ad + 1;
mdd = md;
while ((mdd % 2) == 0)
{
mdd = mdd / 2;
}
Console.WriteLine("The {0} branch, branches off from the {2} branch at {1}",D,md,mdd);
}
else
{

}

}

Console.ReadKey();
}
}
Guest
 

Return to Programming and Algorithms



Who is online

Users browsing this forum: No registered users and 1 guest