-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecusiveFunction.py
More file actions
54 lines (39 loc) · 1.09 KB
/
RecusiveFunction.py
File metadata and controls
54 lines (39 loc) · 1.09 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
# Binary Search
def binary_search(data, target, low, high):
if low > high:
return False
else:
mid = (low + high) // 2
if target == data[mid]:
return mid
elif target < data[mid]:
return binary_search(data, target, low, mid - 1)
else:
return binary_search(data, target, mid + 1, high)
info = [2, 3, 4, 5, 7, 8, 9, 12, 14, 16, 17, 28]
print(binary_search(info, 9, 1, 12))
# Multiplication in recursive
def multiplication(multiplier, multiplicand):
if multiplicand == 1:
return multiplier
else:
return multiplier + multiplication(multiplier, multiplicand - 1)
print(multiplication(5, 4))
# Recursive product
def power(num, k):
if k == 1:
return num
else:
return num * power(num, k - 1)
print(power(5, 5))
# Recursive Fibonacci sequence
def fibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)
# Test Fibonacci sequence
for j in range(4):
print(fibonacci(j), ", ", end=' ', flush=True),