Can someone help me simplify these lines of code to just one please
x = 1.0
a = float(input('Ingrese el valor de a: \n'))
for k in range(1, 10):
x = (x + a/x)/2
print(x)
I don't exactly know what you are looking for, but this code uses Newton's Method to calculate the square root with 10 iterations. You can use one-liner if you would like to calculate sqrt:
print(float(input("Enter a number: "))**0.5)
I don't think it would be possible to do the exact same code as you have in one line since you would probably need to import reduce
from functools
, but that makes it at least two lines. ¯\ (ツ) /¯
Here's the code though I don't think this kind of code will be of any use outside something like code golf.
(lambda x, a: print((lambda f: f(x, a, 1, f))(lambda x, a, i, rec: x if i >= 10 else rec((x+a/x)/2, a, i+1, rec))))(1.0, float(input('Ingrese el valor de a: \n')))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.