Advertisement
Guest User

zzz

a guest
Jul 30th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. # Question 2
  2.  
  3. ## a)
  4. ```{r}
  5. qs2a = function(n){
  6.  
  7. s1 = 0 # Outer sum
  8. for (k in 0:n){ # Outer summation
  9.  
  10. s2 = 0 # Inner sum
  11. for (j in 0:k) { # Inner summation
  12. s2 = s2 + ((-1)^(k - j))*choose(k,j)*j^n # Formula applied
  13. }
  14.  
  15. s1 = s1 + s2/factorial(k) # Multiplying the inner sum by 1/k!
  16. }
  17. return(s1) # Returning the total sum
  18. }
  19. ```
  20.  
  21. ###### $b) test case: P_5$
  22. ```{r}
  23. qs2a(5) # Finding P5
  24. ```
  25.  
  26. ###### $a) test case: P_15$
  27. ```{r}
  28. qs2a(15) # Finding P15
  29. ```
  30.  
  31. ## b)
  32. ```{r}
  33. qs2b = function(n){
  34. s = 0 # Outer sum
  35. for (k in 0:n){ # Outer summation
  36.  
  37. # Changed the inner sum to a vector replacing j with 0:k for single evaluation of inner summation
  38. s = s + sum(((-1)^(k - 0:k))*choose(k,0:k)*(0:k)^n)/factorial(k)
  39. }
  40. return(s) # Returning total sum
  41. }
  42. ```
  43.  
  44. ###### $b) test case: P_5$
  45. ```{r}
  46. qs2b(5) # Finding P5
  47. ```
  48.  
  49. ###### $b) test case: P_15$
  50. ```{r}
  51. qs2b(15) # Finding P15
  52. ```
  53.  
  54. ## c)
  55. ```{r}
  56. qs2c = function(n){
  57. # Replacing both summations with vectors: k with 0:n and j with
  58. k = 0:n
  59. return(sum ( sum(((-1)^(k - 1:k))*choose(k, 1:k )*( 1:k )^n)/factorial(k) ) )
  60. }
  61. ```
  62.  
  63. ###### $c) test case: P_5$
  64. ```{r}
  65. qs2c(5) # Finding P5
  66. ```
  67.  
  68. ###### $c) test case: P_15$
  69. ```{r}
  70. qs2c(15) # Finding P15
  71. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement