-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment2.Rmd
74 lines (56 loc) · 1.15 KB
/
Assignment2.Rmd
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
---
title: "Assignment 2"
author: "Aadi Kalloo"
date: "November 3, 2015"
output: html_document
---
The following code computes choose(n,r):
```{r}
calc_choose <- function(n,r)
{
# declare all local variables
n1 <- n;
r1 <- r;
n_factorial <- 1;
nr_factorial <- 1;
r_factorial <- 1;
nchooser <- 1;
nr <- n - r;
#set up three repeat loops. one each for n!, (n-r)!, and r!
repeat
{
if (n1==0) {break}
n_factorial = n_factorial*n1;
n1 = n1 - 1;
}
repeat
{
if (nr==0) {break}
nr_factorial = nr_factorial*nr;
nr = nr - 1;
}
repeat
{
if (r1==0) {break}
r_factorial = r_factorial*r1;
r1 = r1 - 1;
}
#compute n choose r
nchooser = n_factorial/(nr_factorial*r_factorial)
#return answer
return(nchooser)
}
#This function allows user to enter integer and passes to variable
readinteger <- function()
{
n <- readline(prompt="Enter an integer: ")
return(as.integer(n))
}
```
The following code tests the above function:
```{r}
n <- readinteger(); #Enter value for n
r <- readinteger(); #Enter value for r
nchooser = calc_choose(n,r);
nchooser
```