Skip to content

Commit b474f6d

Browse files
authored
Merge pull request #3 from LeandroTk/big-o-aditional
Big o aditional
2 parents 734869b + aa2f616 commit b474f6d

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

big_o/example21.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def sqrt(n):
2+
return sqrt_helper(n, 1, n)
3+
4+
def sqrt_helper(n, min, max):
5+
if min > max:
6+
return -1
7+
8+
guess = (min + max) / 2
9+
10+
if guess * guess > n:
11+
return sqrt_helper(n, min, guess - 1)
12+
elif guess * guess < n:
13+
return sqrt_helper(n, guess + 1, max)
14+
else:
15+
return guess
16+
17+
print(sqrt(100))
18+
print(sqrt(9))
19+
print(sqrt(25))
20+
print(sqrt(3))
21+
22+
# Complexity: O(log N) --> It is a binary search algorithm

big_o/example22.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from math import sqrt
2+
3+
def square_root(n):
4+
for guess in range(int(sqrt(n))+1):
5+
if guess * guess == n:
6+
return guess
7+
8+
return -1
9+
10+
print(square_root(100))
11+
print(square_root(9))
12+
print(square_root(25))
13+
print(square_root(3))
14+
15+
# Complexity: O(log N) --> It is a binary search algorithm
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def sqrt(n):
2+
return sqrt_helper(n, 1, n)
3+
4+
def sqrt_helper(n, min, max):
5+
if min > max:
6+
return -1
7+
8+
guess = (min + max) / 2
9+
10+
if guess * guess > n:
11+
return sqrt_helper(n, min, guess - 1)
12+
elif guess * guess < n:
13+
return sqrt_helper(n, guess + 1, max)
14+
else:
15+
return guess
16+
17+
print(sqrt(100))
18+
print(sqrt(9))
19+
print(sqrt(25))
20+
print(sqrt(3))
21+
22+
# Complexity: O(log N) --> It is a binary search algorithm
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from math import sqrt
2+
3+
def square_root(n):
4+
for guess in range(int(sqrt(n))+1):
5+
if guess * guess == n:
6+
return guess
7+
8+
return -1
9+
10+
print(square_root(100))
11+
print(square_root(9))
12+
print(square_root(25))
13+
print(square_root(3))
14+
15+
# Complexity: O(log N) --> It is a binary search algorithm

0 commit comments

Comments
 (0)