Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a string of the prime factorization of a number (with exponents)
    primarykey
    data
    text
    <p>So I have a program that creates a binary tree of the prime factors of a user input number and displays them in a treeView control:</p> <p><a href="http://i.imgur.com/zi7vCDE.png" rel="nofollow">Example One</a></p> <p><a href="http://i.imgur.com/uIYy9vD.png" rel="nofollow">Example Two</a></p> <p>Now I would like to create a string like the ones shown in the messageboxes, except with exponents ("256 = 2 ^ 8", "1234567890 = 2 X 3 ^ 2 X 5 X 3607 X 3803")</p> <p>My current code looks like: </p> <pre><code>private void LabelDisplayCondensed(FactorTreeNode currentNode) { string result = Convert.ToString(root.Key) + " = " + Convert.ToString(currentNode.Left.Key); FactorTreeNode prevNode = currentNode; int exponent = 1; while (currentNode.Right != null) { prevNode = currentNode; currentNode = currentNode.Right; if (currentNode.Left.Key == prevNode.Left.Key) { exponent += 1; } else { exponent = 1; } if ((exponent != 1) &amp;&amp; (currentNode.Left.Key != prevNode.Left.Key)) { result += " ^ " + exponent + " X " + currentNode.Left.Key; } } MessageBox.Show(result); } </code></pre> <p>This is my latest, desperate attempt. The function is called with the root of the tree. I realize this code is completely flawed. The current wall I am hitting is the currentNode reaches the right-most child in the tree, evaluates the key for its .Left.Key in </p> <pre><code>if (currentNode.Left.Key == prevNode.Left.Key) </code></pre> <p>and crashes because .Left is null.</p> <p>I was actually a lot closer earlier. I had it at a point where 500 would be evaluated to "500 = 2 ^ 2 X 5 ^ 2 ^ 2" I think (rather than the ideal 500 = 2 ^ 2 X 5 ^ 3)</p> <p>Here is the code for my FactorTreeNode:</p> <pre><code>class FactorTreeNode { // constants // variables private long data; // this is our only data, so also key private FactorTreeNode leftPointer; private FactorTreeNode rightPointer; // these pointers point to the // left and right children respectively private FactorTreeNode parentPainter; // pointer to the parent of the node // constructors public FactorTreeNode(long newValue) { Key = newValue; Left = null; Right = null; Parent = null; } // Properties public long Key { get { return data; } set { data = value; } } public FactorTreeNode Left { get { return leftPointer; } set { leftPointer = value; } } public FactorTreeNode Right { get { return rightPointer; } set { rightPointer = value; } } public FactorTreeNode Parent { get { return parentPainter; } set { parentPainter = value; } } } </code></pre> <p>I've been chewing on this all day. I appreciate any help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload