-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProjectPart1.R
47 lines (37 loc) · 917 Bytes
/
ProjectPart1.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
##########################PROJECT PART 1
#Version 1
Factorial_loop<-function(number)
{
for(i in 1:number) {if(number>0){result=result*i }}
}
#Version 2
Factorial_reduce<-function(number)
{
library(purrr)
if(number>0)
{
reduce(1:number, function(x, y){
x * y
})
}
}
#Version 3
Factorial_func<-function(number) {if(number<=0) 1 else number*Factorial_func(number-1) }
#Version 4
createMemFactorial <- function() {
res <- 1
memFactorial <- function(n) {
if (n == 1) return(1)
if (length(res) < n) res <<- `length<-`(res, n)
if (!is.na(res[n])) return(res[n])
res[n] <<- n * factorial(n-1)
res[n]
}
memFactorial
}
memFactorial <- createMemFactorial()
##microbenchmark
library(microbenchmark)
Factorial_loop_perf <- microbenchmark(Factorial_loop(5))
Factorial_reduce_perf <- microbenchmark(Factorial_reduce(5))
Factorial_func_perf <- microbenchmark(Factorial_func(5))