Advertisement
Matthen

Python Maths Quiz

Oct 17th, 2013
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. def f(x, n=0) :
  2.     head, tail = x
  3.     if n<100 :
  4.         return head + 1.0/(f(tail, n+1))
  5.     return head
  6. x = [1]
  7. x.append(x)
  8. print f(x)
  9.  
  10. def f2(n) :
  11.     a,b = 1,1
  12.     for i in range(n):
  13.         b,a = a,a+b
  14.     return a/float(b)
  15. print f2(100)
  16.  
  17. def f3(x) :
  18.     y = 1.0
  19.     for i in range(100) :
  20.         y = 0.5*(1+y + x/y)
  21.     return y
  22.    
  23. print f3(1.0)
  24.  
  25. class RabbitPair(object) :
  26.     def __init__(self) :
  27.         self.age = 0
  28.     def mate(self) :
  29.         offspring = []
  30.         if self.age > 0 :
  31.             offspring.append(RabbitPair())
  32.         self.age += 1
  33.         return offspring
  34.    
  35. populations = []
  36. rabbit_pairs = [RabbitPair()]
  37. for i in range(20) :
  38.     new_rabbits = []
  39.     for rabbit_pair  in rabbit_pairs :
  40.         new_rabbits += rabbit_pair.mate()
  41.     rabbit_pairs += new_rabbits
  42.     populations.append(len(rabbit_pairs))
  43. print populations[-1]/float(populations[-2])
  44.  
  45. x = [False]
  46. a,b = 1,1
  47. for i in range(30) :
  48.     new = [False for y in x if y]
  49.     x = [True]*len(x) + new
  50.     a,b = b,len(x)
  51. print b/float(a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement