155k views
0 votes
A president and a treasurer are to be chosen from a student club consisting of 50 people. We can find the number of different choices of officers when there are no restrictions as 50P2. How would you program this into R

1 Answer

3 votes

Answer:

> factorial()

Input a number to find factorial : 50

[1] " The factorial result is 50 is 3.04140932017134e+64"

> factorial()

Input a number to find factorial : 48

[1] " The factorial result is 48 is 1.24139155925361e+61"

and we divide both numbers and we got 2450

Explanation:

We know that in permutations the order matters, and the general formula for
nPx is given by:


nPx = (n!)/((n-x)!)

So we need to create a function for the factorial function in R like this:

factorial <- function(){

# The function only accepts an integer

number = as.integer( readline(" Input a number to find factorial : "))

fact = 1

# If the number is negative or 0

if(number < 0) {

print(" The number is negative the factorial does not exist. ")

} else if(number == 0) {

print(" The factorial result is 1 ")

} else {

for( i in 1:number) {

fact = fact * i

}

print(paste(" The factorial result is ", number ,"is", fact ))

}

}

And we can find the expression like this:

> factorial()

Input a number to find factorial : 50

[1] " The factorial result is 50 is 3.04140932017134e+64"

> factorial()

Input a number to find factorial : 48

[1] " The factorial result is 48 is 1.24139155925361e+61"

and we divide both numbers and we got 2450

User Oda Mitsuru
by
5.7k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.