DEV Community

Discussion on: Optimizing code with algebra

 
hdennen profile image
Harry Dennen

Not quite... Solving for the given equation t(n) = 2^(n-1) + (2^(n-1) - 1) is straight forward. Nothing complicated about replacing n with an actual number. This is not the source of my confusion.

I am asking how you get to the given equation in the first place. As in, you start with 8+7 and a few steps later it's 2^(n-1) + (2^(n-1) - 1)

I'm going to use your example with t(4) from the previous comment, but reverse, because that is the process I am trying to figure out.

t(4) = 8+7 // factored recursive hanoi down to this. cool, makes sense.
t(4) = 8 + (8 - 1) // ok I guess, but why?
t(4) = 2^(3) + (2^(3) - 1) // Math makes sense, but why did we decide to represent 8 as an 2 to the 3rd? Is the goal to find exponential representations?
t(4) = 2^(4-1) + (2^(4-1) - 1) // Why on earth did we decide 3 is better represented as 4-1??

From here, factoring 2^(4-1) + (2^(4-1) - 1) down to 2^n-1 makes perfect sense.

The source of my frustration here is not so much the math, but why the choices were made to eventually turn 8 into 2^(4-1)

I would really appreciate shedding some light on that part specifically.

Thread Thread
 
socratesdz profile image
Sócrates Díaz

t(4) = 2^(3) + (2^(3) - 1) // Math makes sense, but why did we decide to represent 8 as an 2 to the 3rd? Is the goal to find exponential representations?
t(4) = 2^(4-1) + (2^(4-1) - 1) // Why on earth did we decide 3 is better represented as 4-1??

Hold that thought right there. The reason why we use 4-1 instead of 3 is to represent the value of n in the formula, so we can write n-1. Because in that example, n = 4.

If you notice in my previous comment, my goal was not simply to solve the equation with several values of n, but to show how the result and the parameters of the function relate each other.

The goal of the deduction process is to find the function that solves these relations:
t(1) = 1 + 0 // same as 2^(1-1) + (2^(1-1) - 1)
t(2) = 2 + 1 // same as 2^(2-1) + (2^(2-1) - 1)
t(3) = 4 + 3 // same as 2^(3-1) + (2^(3-1) - 1)
t(4) = 8 + 7 // same as 2^(4-1) + (2^(4-1) - 1)
t(5) = 16 + 15 // same as 2^(5-1) + (2^(5-1) - 1)

That way you can find a function that solves for every value of n. Please let me know if I now made it clear.