My brother and I stumbled across something interesting whilst playing around with wolfram between watching Digimon and gaining weight. For some reason we got onto square roots of 2 to power x. I noticed that when x is even, then result is always whole, and when x is odd, the result is always the same as x-1, multiplied by sqrt(2). I also noticed that the whole number in the result doubles with every even number x. To illustrate what I'm saying...
sqrt(2^0) = 1 -> As 2^0 = 1.
sqrt(2^1) = 1 * sqrt(2) -> Previous result multiplied by sqrt(2)
sqrt(2^2) = 2 -> Original result (1) doubled.
sqrt(2^3) = 2 * sqrt(2) -> Previous result multiplied by sqrt(2)
sqrt(2^4) = 4 -> Last whole number result (2) doubled.
sqrt(2^5) = 4 * sqrt(2) -> Previous result multiplied by sqrt(2)
sqrt(2^6) = 8 -> Last whole number result (4) doubled.
sqrt(2^7) = 8 * sqrt(2) -> Previous result multiplied by sqrt(2)
And so on in that fashion. I'm not surprised by this, but I found it intriguing, and I'm almost certainly not the first one to find it (but I've never seen it before). My brother, however, was really fascinated and challenged me to write a little formula to calculate the result of sqrt(2^n). I've decided to take him up on that and try and write it in Ruby.
The first thing I realized I had to do was determine if n was even or odd. Easy stuff there (i = n % 2), no problem. The easy part is the even formula. That's still pretty easy (x = Math.sqrt(2 ** n)).
The part that stumped me a bit was the odd formula, but I figured out eventually, whilst typing this topic, so it's actually kinda pointless because I was going to ask for help but now I don't need it.
I ended up going with x = Math.sqrt(2 ** (n - 1)) * Math.sqrt(2), but any improvements would be accepted before I rub my brother's face in this relatively simple fomula.
Here's the method:
def whocares(n)
i = n % 2
if i == 0
x = Math.sqrt(2 ** n)
elsif i == 1
x = Math.sqrt(2 ** (n - 1))
x = x.to_s + (" root 2") # Or x *= Math.sqrt(2)
end
return x
end
Does anyone know anything about this formula? Is it famous and I'm just stupid? Are there any improvements that could be done?
Anyway, thanks for being a sound board without even knowing it. I just happened to solve this pretty simple problem too soon.
I'mma post this anyway.