Guest User

Untitled

a guest
Dec 7th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. def divides(n,d):
  2. '''True if remainder of n/d is 0, False otherwise'''
  3. return n%d == 0
  4.  
  5. def least_divisor_from(n,k):
  6. '''smallest number starting from k, k <= n, that divides n'''
  7. if divides(n,k):return k
  8. else: return least_divisor_from(n,k+1)
  9.  
  10. def least_divisor(n):
  11. '''smallest divisor of n that is greater than 1'''
  12. return least_divisor_from(n,2)
  13.  
  14. def is_prime(n):
  15. '''True if n is prime, False otherwise'''
  16. return least_divisor(n)== n
  17.  
  18. def next_prime(n):
  19. '''next prime > n'''
  20. if is_prime(n+1): return n+1
  21. else: return next_prime(n+1)
  22.  
  23.  
  24. def primes_between(a,b):
  25. '''list of primes in [a..b]'''
  26. if a>b: return []
  27. if is_prime(a): return [a]+primes_between(next_prime(a),b)
  28. else: return primes_between(next_prime(a),b)
  29.  
  30. def prime_factors(n):
  31. '''list of prime factors of n'''
  32.  
  33. def factorial(n):
  34. '''returns n*(n-1)*(n-2)*...*2*1'''
  35. if n==0:return 1
  36. else:return n* factorial(n-1)
  37.  
  38. def P(n,r):
  39. '''returns nPr'''
  40. if r == 0: return 1
  41. else: return n*P(n-1,r-1)
  42.  
  43. def C(n,r):
  44. '''returns nCr'''
  45. return P(n,r)//factorial(r)
  46.  
  47. def gcf(a,b):
  48. '''greatest common factor of a and b'''
  49. if a==b: return a
  50. if a<b: return gcf(a,b-a)
  51. if a>b: return gcf(a-b,b)
  52.  
  53. def lcm(a,b):
  54. '''least common multiple of a and b'''
  55. return a*b//gcf(a,b
  56.  
  57. def arithmetic(a,d,n):
  58. '''arithmetic sequence of n terms'''
  59. if n == 0: return []
  60. else: return [a]+arithmetic(a+d,d,n-1)
  61.  
  62. def geometric(g,r,n):
  63. '''geometric sequence of n terms'''
  64. if n==0: return []
  65. else: return [g]+geometric(g*r,r,n-1)
  66.  
  67. def first(L):
  68. '''first item in list L'''
  69. return L[0]
  70.  
  71. def rest(L):
  72. '''all items in list L after the first'''
  73. return L[1:]
  74.  
  75. def last(L):
  76. '''last item in list L'''
  77. return rest(L)
  78.  
  79. def init(L):
  80. '''all items in lst before the last'''
  81.  
  82. def length(L):
  83. '''number of items in list L'''
  84. if L == []: return 0
  85. else: return 1+length(rest(L))
  86.  
  87. def sum(L):
  88. '''sum of items in list L'''
  89.  
  90. def product(L):
  91. '''product of items in list L'''
  92.  
  93. def mean(L):
  94. '''arithmetic average of list L'''
  95.  
  96. def smaller(a,b):
  97. '''smaller of values a and b'''
  98.  
  99. def min(L):
  100. '''minimum value in lst'''
  101.  
  102. def larger(a,b):
  103. '''larger of values a and b'''
  104.  
  105. def max(L):
  106. '''maximum value in list L'''
  107.  
  108. def remove(item, L):
  109. '''list L without item'''
  110.  
  111. def sorted(L):
  112. '''items of list L sorted in increasing order'''
Add Comment
Please, Sign In to add comment