-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.py
More file actions
38 lines (30 loc) · 1.21 KB
/
func.py
File metadata and controls
38 lines (30 loc) · 1.21 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
# -------------------------------------- Task 1 -----------------------------------
def add(x, y):
return x + y
def sub(x,y):
return x - y
def div(x,y):
return x / y
def mult(x,y):
return x * y
def exp(x,y):
return x ** y
# TODO: Add definitions of sub(), div(), mult(), exp(), as well as neg() and sqrt().
# neg() should return the negation of the given number, and sqrt() should
# return the square root of the given number.
def neg(x):
return -x # fill here
def sqrt(x):
return x**(1/2) # fill here
# -------------------------------------- Task 2 -----------------------------------
# TODO: Implement the quadratic formula using *only* the functions defined here.
# Do not use arithmetic operators directly.
# You're allowed to define other functions.
a = 1
b = -3
c = 1
x1 = div(add(neg(b), sqrt(sub(exp(b,2),mult(mult(4,a),c)))), mult(2,a)) # TODO: write a code to compute the first root of the quadratic equation
x2 = div(sub(neg(b), sqrt(sub(exp(b,2),mult(mult(4,a),c)))), mult(2,a)) # TODO: then do the same for the second root
# Note: Make sure to remove the ellipsis (...) when you're writing your code
print("First root:" + str(x1))
print("Second root:" + str(x2))