+ 1

[Python] Nth root of y

so im trying to write a code to learn with that will return what times itself x times = y or the nth root of y. i know its a logic error because it runs but i doesnt do what i want i think the error is in the last print statement or in how im manipulating the variables to try the next case i have a feeling this method is floored but im not sure how my next attempt will be to use % test case instead of percentage of the answer i want https://code.sololearn.com/cPHAZNLLpsZ0/#py

28th May 2020, 5:34 AM
MaxBanter
MaxBanter - avatar
4 ответов
+ 4
First of all, powers in python are done with the ** operator instead of ^. This also means you can find nth roots like this: x ** (1/n) But doing it "by hand" is an interesting challenge. For square roots there is the well known babylonian method: Lets say we want to find √100. First we take a guess `x`, say 5, doesn't really matter. Then you do: (100/x + x) / 2 and that is your new x. You take the midway point between your guess and the reciprocal so to say. For higher roots there is Newton's method: Say we want to find x = ³√100. then x³ - 100 = 0 with a bit of algebra, which is a polynomial where we want to find the root. The derivative of x³ - 100 is 2x². Newtons method therefor says: Take a guess x, say 5, doesn't matter. Do x - (x³ - 100) / (2x²) And that is your new x. Do that a couple of times and you get a pretty good estimate for ³√100. I hope that wasn't too maths-heavy.
28th May 2020, 7:20 AM
Schindlabua
Schindlabua - avatar
+ 3
Schindlabua that makes me feel like I need to take a refresher in mathematics. Lol Maybe I'm just getting old, been close to 30 yrs since my last math class. Lol
28th May 2020, 7:25 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
ChaoticDawg Honestly I just know it's a thing and I look up the details as needed :P My uni maths was like 5 years ago though so it's a bit fresher, ha.
28th May 2020, 7:29 AM
Schindlabua
Schindlabua - avatar