Python Forum
Can someone please explain this code for me and what it does?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can someone please explain this code for me and what it does?
#1
def my_sqrt(n):
    approx = n/2
    closer = (approx + n/approx)/2
    while closer != approx:
        approx = closer
        closer = (approx + n/approx)/2
    return approx
Reply
#2
It seems to be an implementation of the Babylonian method of finding the square root of a number:
https://en.wikipedia.org/wiki/Methods_of...ian_method

Do you have a specific question about it?
Reply
#3
Would like to know what each line does and how the code works, new to coding so just trying to understand.
Reply
#4
def my_sqrt(n):
    approx = n/2
    closer = (approx + n/approx)/2
    while closer != approx:
        approx = closer
        closer = (approx + n/approx)/2
    return approx
I'm a novice coder but I'll give it a go and wiser contributors than me can add to it.

def my_sqrt(n): # create a function calling in an 'n' arguement
approx = n/2 # set a variable equal to 'n' ÷ 2
closer = (approx + n/approx)/2 # set a variable equal to ('approx' variable + 'n' ÷'approx' variable) ÷ 2
while closer != approx: # start of while loop that is run when following 'approx' variable is false, or not equal to
approx = closer # 'approx' variable equals 'closer' variable
closer = (approx + n/approx)/2 # takes the 'closer' variable equal to ('approx' variable + 'n' ÷'approx' variable) ÷ 2
return approx # function returns 'approx' variable

Add this line to see some results in terminal:
print('\'approx\' variable is:', approx)
Add this line indented (under 'closer =' in while loop) and inline with 'def' to see different results.
You'll have to remove this line that is inline with 'def'. It causes an error which I mention just for you to see it.

The programs gives the square root of any number you call the function with (replace a number for 'n').
So if you open Python Idle, paste this code, save, press F5, and type 'my_sqrt(9)' is the python window that opens, you should see some lines with the final line showing a floating point value of 3.0
Replace '9' with any # you want.

Phil Blush
Reply
#5
I recommend that you copy and paste the code into the Python Visualiser and you can then step through the programme a line at a time and see, visually, what is going on.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#6
This piece of code will give me the square root of a number?! I trust you guys but I just can't get it. Big Grin The math is an interesting thing. Lots of tricks in it.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Explain the python code in this definition Led_Zeppelin 1 737 Jan-13-2023, 10:20 PM
Last Post: deanhystad
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,003 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
  Explain the python code in this definition Led_Zeppelin 1 1,088 Oct-27-2022, 04:04 AM
Last Post: deanhystad
  Sudoku Solver in Python - Can someone explain this code ? qwemx 6 2,124 Jun-27-2022, 12:46 PM
Last Post: deanhystad
  Can someone explain this small snippet of code like I am a 5 year old? PythonNPC 3 1,239 Apr-08-2022, 05:54 PM
Last Post: deanhystad
  Could you explain each part of the code? Tsushida 2 1,501 Mar-20-2022, 08:19 AM
Last Post: Larz60+
  What is the run time complexity of this code and please explain? samlee916 2 2,292 Nov-06-2020, 02:37 PM
Last Post: deanhystad
  poplib - parsing message body, could somebody please help explain this code t4keheart 2 2,293 Oct-12-2020, 01:59 PM
Last Post: t4keheart
  Explain range in this code RavCOder 4 2,316 Oct-02-2019, 05:04 PM
Last Post: jefsummers
  Explain this code - for loop RavCOder 9 3,436 Sep-24-2019, 01:01 PM
Last Post: RavCOder

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020