Welcome to Dream.In.Code
Become an Expert!

Join 149,609 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,867 people online right now. Registration is fast and FREE... Join Now!




calculate the square root

 
Reply to this topicStart new topic

calculate the square root

FarmGirl
29 Sep, 2007 - 05:32 PM
Post #1

New D.I.C Head
*

Joined: 3 Sep, 2007
Posts: 5


My Contributions
I am supposed to be doing the exercises out of some python user's manual, and one of them is to calculate the square root of a number using Newton's method. Whenever I run the program, my computer starts getting really hot and makes a loud buzzing sound. Is this an infinite loop? If so, could someone help me with my code?

(x is the initial guess, N is the number I want to square root of, and c is how close I want the number to be to the actual square root...I think.

def square(N,c):

x = 1

error = abs(x**2-N)

while error >= c:
x = (x+(N/x))/2

if error < c:
break

return x
User is offlineProfile CardPM
+Quote Post

gekon
RE: Calculate The Square Root
6 Oct, 2007 - 05:53 AM
Post #2

New D.I.C Head
*

Joined: 6 Oct, 2007
Posts: 1


My Contributions
try this wink2.gif

CODE

def square(N,c):
    
    x = 1
    error = abs(x**2-N)
    
    while error >= c:
        x = (x+(N/x))/2
        error = abs(x**2-N)

    return x


User is offlineProfile CardPM
+Quote Post

Nova Dragoon
RE: Calculate The Square Root
9 Oct, 2007 - 04:14 PM
Post #3

The Innocent Shall Suffer, Big Time
Group Icon

Joined: 16 Aug, 2001
Posts: 6,145



Thanked: 8 times
Dream Kudos: 515
Expert In: Python, Linux

My Contributions
Notice what gekon did there:
in the while loop you are checking the value error against the variable c, but no where in your code are you changing it. So it doesn't make sense to continually check something that never changes.

An infinite loop means your program runs forever, in which case it is doing. However it should not affect the hardware like it is doing to yours. You may want to have someone look at your computer. Possibly clean it out, if the fans and vents are gunked up where a little bit of load is having it run so rough.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:13AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month