-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqrt.py
More file actions
52 lines (45 loc) · 1.39 KB
/
sqrt.py
File metadata and controls
52 lines (45 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
Exercise Author: Gainluca Rossi
Student: Flavio Fois
Date: 7th of November 2021
Exercise:
Program that asks for two x and y numbers in input and prints the square root of x added to the square root of y.
It is forbidden to use the operator **.
"""
def sqrt_x_without_power(x):
"""
Returns the square root of x without using power.
"""
if 0 == x:
return 0 # Avoid division by zero
n = (x / 2) + 1 # Initial estimate, never low
n1 = (n + (x / n)) / 2
while n1 < n:
n = n1
n1 = (n + (x / n)) / 2
result_x = n
return result_x
def sqrt_y_without_power(y):
"""
Returns the square root of y without using power.
"""
if 0 == y:
return 0 # Avoid division by zero
n = (y / 2) + 1 # Initial estimate, never low
n1 = (n + (y / n)) / 2
while n1 < n:
n = n1
n1 = (n + (y / n)) / 2
result_y = n
return result_y
def sqrt_sum_without_power(x, y):
"""
Returns the square root of x added to the square root of y without using power.
"""
return sqrt_x_without_power(x) + sqrt_y_without_power(y)
if __name__ == '__main__':
"""
Main function.
"""
print("Running exercise without using ** operator.")
print(sqrt_sum_without_power(float(input("x value: ")), float(input("y value: "))))