Square Root

A forum for general discussion of the Python programming language.

Square Root

Postby b849961@drdrb.com » Thu Dec 19, 2013 2:54 am

How to find square root in a number using Python 3.2?
Last edited by Mekire on Thu Dec 19, 2013 3:53 am, edited 1 time in total.
Reason: Head. Desk. Lock.
My Python Version is 3.2.3
b849961@drdrb.com
 
Posts: 13
Joined: Thu Oct 24, 2013 12:41 pm

Re: Square Root

Postby Mekire » Thu Dec 19, 2013 3:51 am

The same way you do it in python 2.x.

Perhaps I'm misunderstanding your question.
Try this.

Potential solution:
Code: Select all
def square_root(number,iterations=100):
    guess = number/2.0
    for _ in range(iterations):
        guess -= (guess**2-number)/(2*guess)
    return guess


-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Square Root

Postby stranac » Thu Dec 19, 2013 9:27 am

Just use math.sqrt().
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1111
Joined: Thu Feb 07, 2013 3:42 pm

Re: Square Root

Postby Mekire » Thu Dec 19, 2013 9:35 am

Boo. You sayin' my Newton's Method approximation isn't good enough for ya? :(

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Square Root

Postby micseydel » Thu Dec 19, 2013 7:31 pm

Mekire wrote:Boo. You sayin' my Newton's Method approximation isn't good enough for ya? :(

-Mek

+1
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1256
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Square Root

Postby XndrK » Thu Dec 19, 2013 10:56 pm

Or you could use this.

Code: Select all
root ** (1 / index)


That's probably how math.sqrt() is written, anyway. :P
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Square Root

Postby micseydel » Thu Dec 19, 2013 11:01 pm

I tend to use **0.5 for my square roots, but that's not how math.sqrt() works actually: http://stackoverflow.com/questions/3270 ... math-sqrtx
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1256
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Square Root

Postby b849961@drdrb.com » Fri Dec 20, 2013 5:05 am

I was wrote a python program for find solutions of quadratic equation( aX² + bX + c ). Code is here. But something is wrong with finding square root. Please help me to solve this problem. Quadratic equation solving formula is,

x = (-b ± (squareroot(b² - 4ac))) / (2*a)

Code: Select all
X1 = 0
X2 = 0
a = int(input('Input A :- '))
if (a == 0) : print('No Solution.')
else :
    b = int(input('Input B :- '))
    c = int(input('Input C :- '))
    if (((b**2)-(4*a*c)) > 0) :
        X1 = (-b+(math.sqrt(b**2-4*a*c)))/(2*a)
        X2 = (-b-(math.sqrt(b**2+4*a*c)))/(2*a)
        if (X1 == X2) : print('\nSolution =',X1)
        else : print('\nSolotions =',X1,',',X2)
    else : print('\nNo Solution.')
Last edited by Mekire on Fri Dec 20, 2013 8:11 am, edited 1 time in total.
Reason: Images too annoying; removed.
My Python Version is 3.2.3
b849961@drdrb.com
 
Posts: 13
Joined: Thu Oct 24, 2013 12:41 pm

Re: Square Root

Postby Mekire » Fri Dec 20, 2013 5:20 am

I really feel like you aren't trying to help yourself here.

You never imported the math module.

Also please don't upload images if a paste with code tags would suffice.

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Square Root

Postby ochichinyezaboombwa » Fri Dec 20, 2013 5:24 am

You are creating problems instead of trying to solve them.
What would you do if someone asked you to help because "something went wrong"?
How would you yourself, looking at your own post, figure out what was the error?
Finally: screenshots will always be much worse than text, point.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Square Root

Postby b849961@drdrb.com » Fri Dec 20, 2013 6:25 am

Thank you all of you. Problem was solved. My heartiest sorry for my mistakes. I'm never do that. Again I want to say THANK YOU...
My Python Version is 3.2.3
b849961@drdrb.com
 
Posts: 13
Joined: Thu Oct 24, 2013 12:41 pm


Return to General Discussions

Who is online

Users browsing this forum: snippsat and 2 guests