-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinatorics_functions.RMD
More file actions
38 lines (28 loc) · 1.2 KB
/
Copy pathCombinatorics_functions.RMD
File metadata and controls
38 lines (28 loc) · 1.2 KB
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
---
title: "Home-made combinatorics functions"
author: "Andrea Scalco"
#date: "24/10/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Requirements
R-base
## Ingredients
- A bunch of if/else statements
- 3 functions
- 1/2 function arguments
## Method
It is clear that factors play a major role that will be used by all formula, regardless of the case we are examing. Thus, it seems reasonable to start by writing a function that can report the factor of any number will need. Basically, a factor is the product of an value and all the values lower than it. R base library already offers the solution through the commands "factorial". However, since everything here is home-made, we are going to build our function. It won't shine as the original, but it will be ours.
```{r factorial_function}
f.fact <- function(x) {
if (x[1] == 0) {
return(1)
} else {
return(x * f.fact(x - 1))
}
}
```
The function uses of recursion, a programming technique where a function calls itself. In this case, our function call itself, but it passes the original value minus 1, and so on. In the end, it will return the factor of any number will ask.
Now, let's build the ,