18
loading...
This website collects cookies to deliver better user experience
Recursion is a way of approaching problems which breaks apart complex concepts into smaller and smaller solvable steps.
def collatz(a):
count=a[len(a)-1]
if(count==1):
return a
if(count%2==0):
a.append(count/2)
else:
a.append(count*3+1)
return collatz(a)
print(collatz([7]))
[7, 22, 11.0, 34.0, 17.0, 52.0, 26.0, 13.0, 40.0, 20.0, 10.0, 5.0, 16.0, 8.0, 4.0, 2.0, 1.0]
A common computer programming tactic is to divide a problem into sub-problems of the same type as the original, solve those sub-problems, and combine the results. This is often referred to as the divide-and-conquer method; when combined with a lookup table that stores the results of previously solved sub-problems (to avoid solving them repeatedly and incurring extra computation time), it can be referred to as dynamic programming or memoization. - Wikipedia
def fibo(n):
if(n==1 or n==2):
return 1
return fibo(n-1)+fibo(n-2)
print(fibo(6))
print(fibo(10))
8
55
******
*****
****
***
**
*