-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLecture_8.py
More file actions
66 lines (59 loc) · 1.33 KB
/
Copy pathLecture_8.py
File metadata and controls
66 lines (59 loc) · 1.33 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def add(x,y):
return x+y
def mult(x,y):
print (x*y)
add(1,2)
print(add(2,3))
mult(4,5)
print(mult(2,3))
# Cool thing
print(print(5)) #It will give output as 5 and NONE
#Debug
def is_triangular(n):
"""if n is int and greater than 0
Returns True if n is Triangular, i.e. summation of natural numbers
(1+2+3+.....+k)
Else False"""
total=0
for i in range(n+1):
total+=i
if total ==n:
return True
return False
print(is_triangular(1))
#Using functions to find square roots
def bisection_root(x):
low=0
high=x
epsilon=0.01
ans=(high+low)/2
while abs(ans**2 -x)>= epsilon:
if ans**2 < x:
low=ans
else:
high = ans
ans=(high+low)/2
return ans
print(bisection_root(16))
print(bisection_root(123))
#
def count_nums_with_sqrt_close_to(n,epsilon):
count=0
for i in range(n**3):
#Take the square root of i
sqrt=bisection_root(i)
if abs(n-sqrt)<epsilon:
count+=1
return count
print(count_nums_with_sqrt_close_to(10,0.1))
# Example
def apply(criteria,n):
count=0
for i in range (n+1):
if criteria(i):
count+=1
return count
def is_even(x):
return x%2==0
how_many= apply(is_even,10)
print(how_many)