0

I am stuck on a problem from a textbook. It asks:

Write your own square root approximation function using the equation Xk+1 = 1/2 * (Xk + n/(Xk), where X0 = 1.

This equation says that the sqrt'n' can be found by repeatedly computing the next Xi term. The larger number of terms used, the better the answer. Allow your function to have two input parameters, the number that you want the square root of and the number of terms to compute.'

I am using Python3.5.2 for this.

attached a picture of the problem

Thanks!

lokusking
  • 7,396
  • 13
  • 38
  • 57
Babeeshka
  • 105
  • 1
  • 4
  • 21
  • 1
    Per [SO's general guidelines on homework help](http://meta.stackexchange.com/a/10812) it's suggested that you make a **good faith attempt to solve the problem yourself first**, and post that in your question. – Andy Hoffner Sep 09 '16 at 02:33

1 Answers1

0

A new school year, an old Babylonian method.

So, I won't solve this for you, but I can get you started.

We can write a little function that computes each x_{k+1}:

def sqrt_step(n, xk):
    return 1/2.0 * (xk + float(n)/xk)

Let's set n = 100.

sqrt_step(100, 1) # returns 50.5

Now let's feed that number into the function a few more times:

sqrt_step(100, 50.5) # 26.2

sqrt_step(100, 26.2) # 15.0

sqrt_step(100, 15.0) # 10.8

...this converges to 10 as k goes to infinity.

Now, if only there was a way to perform an operation over and over again k times...I'm thinking of a three letter word that starts with 'f' and rhymes with 'ore'...


EDIT

You've made an honest effort to solve the problem -- which I'm going to assume is a homework practice exercise and not an assignment.

You can solve this simply by using the sqrt_step function inside of a new function. This can be done as follows:

def square_root(n, k): 
    xk = 1
    for i in range(k): 
        xk = sqrt_step(n, xk) # or just: xk = 1/2.0 * (xk + float(n)/xk)
    return xk

Tests:

square_root(64, 100)  # 8.0
square_root(144, 100) # 12.0

As you become more advanced, you will learn about functional programming techniques that allow you to avoid overwriting variables and explicitly writing for loops. For now however, this is the most straightforward approach.

Community
  • 1
  • 1
lnNoam
  • 1,055
  • 11
  • 20
  • Thank you InNoam for responding to my post and helping me get started. I am still having trouble visualizing how to repeat this operation over and over again k times. I will show you what I've tried so far. def sqrt_step(n,xk): return 1/2.0* (xk + float (n)/xk) # as you mentioned I tried repeating this k times by: def sqrt_step(n,xk): for xk in range(xk,1,-1): return 1/2.0* (xk + float (n)/xk) My reasoning was to repeat xk until it reached 1 by decreasing by 1 each time. – Babeeshka Sep 09 '16 at 03:41
  • In math, we call this type of method `explicit`, because `x_{k+1}` only depends on us knowing `x_{k}`. In English, this means what's most important is to know the *value* obtained at the previous step. So, try to set the value of `xk = 1` and run the `sqrt_step()` function, and then *replace* `xk` with it (while this violates a programming principle followed by some known as 'immutability', it is the easiest way to get started). You need the `for` loop to simply tell the computer *how many times* to overwrite the value of `xk`. The actual index at any given step is not particularly relevant. – lnNoam Sep 09 '16 at 04:07
  • I think I did it! You are so helpful and the best. I am not sure how to format my comment in here, but I think I over complicated it and what worked was just to set: for i in range(1,10): between the other lines. – Babeeshka Sep 09 '16 at 04:28
  • I can't not say that's the correct approach :). To learn how to format your code, click [here](http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks). For another cool example of math in python, define `s = 0` and then run `for n in range(1, 250): s += 1/(n**2)`. This returns `s = 1.64`...Now, look up the value of `(pi^2)/6`. While you need a lot of fancy math to *prove* this is true, you can suggest it is true with just a few lines of code. The power of computing. Good luck with your studies! – lnNoam Sep 09 '16 at 05:00
  • Once again InNoam, thank you for your help. Is there a way for the sqrt_step function to calculate the square root of any number depending on the number assigned to the "xk" parameter? I think I have it set up currently only to find the square root of 100. When I set "({n=1000})" and "({xk=100})" I am returned with 55.0. – Babeeshka Sep 09 '16 at 14:14
  • Again, the `sqrt_step` function only computes `x_{k+1}`. Let's try to walk though the whole problem in words. Say you wish to compute `√(n)`, where `n = 100`. Next, we define the number of steps you'd like to use to be any number, let's say `k = 8`. Lastly, the algorithm states that we must also define `x_{k} = 1` to start. Below this, we'd write our `for` loop (from `1...k`) and set `xk` equal to `sqrt_step(n, xk)`. Once you've figured out how to translate this into code (should be easy now), try `n = 4` or `n = 144`. Also, see what happens when you increase or decrease `k`. – lnNoam Sep 09 '16 at 17:54
  • Hey InNoam, sorry I did not reply to your comment. I finally figured it out after your very helpful advice. I wanted to thank you for helping me through this exercise. I ended up coding it as: 'code'def step(n,x): xk = 1/2.0 * (x +(n)/x) for i in range(1,x): xk = 1/2.0 * (xk +(n)/xk) return xk '/code' – Babeeshka Sep 15 '16 at 17:51
  • Hey @InNoam, Thank you for editing that. Yes, this was mostly for practice. And, I did not know that you could include a function within a function like that. The function that I wrote does seem to produce the same results, meaning the square root of 'n'. Is my approach wrong or just less straightforward? Function: Also, I still can't get the format to work in these comments :( – Babeeshka Sep 17 '16 at 15:09