1201-Day2 What's Recursion?

2644 days ago by MATH4R2013

#1) Fibonacci Sequence def fib(n): if(n==1 or n==2): return 1 else: return fib(n-1)+fib(n-2) [fib(n) for n in range(1,10)] 
       
[1, 1, 2, 3, 5, 8, 13, 21, 34]
[1, 1, 2, 3, 5, 8, 13, 21, 34]
N=range(1,21) F=[fib(n) for n in N] FN=zip(N,F) plot(point(FN)) 
       
#2) Factorial Sequence def fact(n): if(n==0): return 1 else: return n*fact(n-1) print "n\tfact(n)" for n in range(10): print n,"\t",fact(n) 
       
n	fact(n)
0 	1
1 	1
2 	2
3 	6
4 	24
5 	120
6 	720
7 	5040
8 	40320
9 	362880
n	fact(n)
0 	1
1 	1
2 	2
3 	6
4 	24
5 	120
6 	720
7 	5040
8 	40320
9 	362880
N=range(1,11) F=[fact(n) for n in N] FN=zip(N,F) plot(point(FN)) 
       
#3) Drug Overdose? Can we keep drug concentration under 180mg? def drug(day): if(day==1): return 35 else: return .75*drug(day-1)+35 for day in range(1,10): print day,"\t",drug(day) 
       
1 	35
2 	61.2500000000000
3 	80.9375000000000
4 	95.7031250000000
5 	106.777343750000
6 	115.083007812500
7 	121.312255859375
8 	125.984191894531
9 	129.488143920898
1 	35
2 	61.2500000000000
3 	80.9375000000000
4 	95.7031250000000
5 	106.777343750000
6 	115.083007812500
7 	121.312255859375
8 	125.984191894531
9 	129.488143920898
N=range(1,51) F=[drug(n) for n in N] FN=zip(N,F) plot(point(FN))