Creating factorial function is not as scary as it sounds. If you haven't heard of factorial before, here is a simple example: $$ 3! = 3 \cdot 2 \cdot 1 = 6 $$
Likewise, 4! looks like this: $$ 4! = 4 \cdot 3 \cdot 2 \cdot 1 = 24 $$
As you can imagine, the factorial of larger numbers just involves multiplying a longer list of numbers together. Factorial shows up in different parts of mathematics. The most well known place is in the permutation formula.
It is pretty easy to calculate factorial for the numbers up to about 5 or 6. But for larger than that the answers start to get crazy. For example, 10! evaluates to millions. After a certain point it becomes impractical to calculate factorial with pen and paper. In this activity we will write a code to do it so we can calculate 100! and even higher!
Click here to open up the code
In the top left corner, press to run the code and the output will look like this:
To save your work you need to create an account by clicking Sign Up in the top right corner of the screen. If you have already created an account, click "Log In" and put in your login information
A non-profit arts group called The Processing Foundation runs this site. They are nice people and they will not send you a bunch of emails for registering.
Before moving on, in the top left click click File --> Duplicate so that the code you are working with is on your account! -->
If the program can detect that something is wrong with your code it will show a red next to that objective.
If the program can detect that you have completed the objective it will show a green check mark next to that objective.
But the program is not very smart and if it can't tell whether you have completed the objective it will indicate a question mark like this next to that objective.
By the end of this activity your goal is to have green check marks next to all the objectives like this:
The directions below will help you meet all these goals.
The code here is pretty short. Here is the entire program:
// define the factorial function here function draw() { // question 1: calculate 5! answer1 = 0; // change this text('5! = ' + answer1, 10,50); // question 2: calculate 10! answer2 = 0; // change this text('10! = ' + answer2, 10,75); // question 3: calculate 100! answer3 = 0; // change this text('100! = ' + answer3, 10,100); // question 4: There are 50 patients and 121 nurses. How many possibilities are there to assign nurses to the patients? Assume 1 patient per nurse answer4 = 0; // change this text('A4 = ' + answer4, 10,125); checkvalues(); exit(); } // end draw() DO NOT ADD ANY CODE AFTER THIS LINE!!!
Similar to other coding activities, we will add code to the program until it can do something new (calculate the factorial) but then this activity is a little different from the others because we then need to answer four different questions. There is also a checkvalues();
command that checks if your answers are correct or not.
There are a couple of things to note at this point:
1. We are going to add a factorial function so that factorial(n)
gives us the result of $n!$
As discussed later, you will need to name this function factorial
because the milestone for "Define factorial function" is expecting the function to be named that.
2. You must use the factorial function that you will add to the code to answer the questions
The milestones are smart enough to figure out if you just copied the number from another program. If you just calculate 5! on paper for example and set answer1
in the code to be equal to that result you will still get a red because you didn't use the factorial function
3. The last question
The last question is "There are 50 patients and 121 nurses. How many possibilities are there to assign nurses to the patients? Assume 1 patient per nurse"
The idea behind this question is that there are 50 patients in the hospital and every day the charge nurse (i.e. the boss) assigns 50 of the available 121 nurses to care for a patient for that day. The nurses who are not assigned a patient get the day off. Each nurse will stay with that patient for the day and then go home.
The question "How many possibilities are there to assign nurses to the patients?" is asking how many combinations of patient and nurse are possible. We will come back to this question later and give you some advice. The only hint we can give right now is that the answer is NOT something you can easily compute by hand.
The bad news first: if you try to use !
in the code to calculate factorial it is not going to work.
Modify the code so that it looks like this:
// question 1: calculate 5! answer1 = 5!; text('5! = ' + answer1, 10,50); // question 2: calculate 10! answer2 = 10!; text('10! = ' + answer2, 10,75); // question 3: calculate 100! answer3 = 100!; text('100! = ' + answer3, 10,100);
Now run the code. Notice that something bad happens! Exactly what bad thing happens may depend on your browser and operating system. Maybe nothing happens.
Write down what happens when you add things like 5! in your code
Clearly this is not going to work. It turns out that the people who developed the JavaScript programming language wanted to use !
for something different than factorial. Instead we will need to add a function to the program to calculate factorial.
Sometimes calculating things by hand can help you see how to make it easier for the computer to calculate something. BEFORE YOU FILL IN THE TABLE BELOW think about how you could fill in the missing values in the fastest possible way.
n | n! |
---|---|
1 | 1 |
2 | 2 |
3 | 6 |
4 | ??? |
5 | ??? |
6 | ??? |
7 | 5040 |
8 | ??? |
9 | ??? |
10 | ??? |
What is the trick to calculating the missing factorials as quickly as possible?
Related question: If you knew the value of 50! what would be the easiest way to calculate 53!
factorial
The first thing we are going to do as we work towards having a function to compute factorials is to define a function with the name factorial
and an argument n
like this:
function factorial (n) { return n; //change this }
Paste this code into your program where it says // define factorial function here
This code will not calculate factorial correctly, but it will give you the green check mark for "Define factorial function"
factorial(n)
to answer question 1Use factorial(5)
to answer question 1 (calculate 5!). Replace this:
answer1 = 0; // change this
with this:
answer1 = factorial(5);
At this point factorial(5)
will give the wrong answer for 5! but what answer will it give? Why?
Instead of multiplying things out by hand like 5*4*3*2*1
we are going to use something called a for loop to do this job.
Here is an example of a for loop that will count from zero to three:
for (i = 0; i <= 3; i += 1) { createP(i); // write value of i below screen }
Paste this code inside of the curly brackets { and } of the draw function and see what happens!
Advice: Place the code somewhere before checkvalues();
, not after, so the numbers get written BEFORE the text of the objectives
What happens when you change each part of the for loop?
What happens when you change i = 0;
to have some other value than 0?
What happens when you change i <= 3;
to have some other value than 3?
What happens when you change i += 1;
to have some other value than 1?
Note: After you are done with this step you can delete or comment out the for loop so you can see all the objectives clearly. We won't need this code again.
Something that will help in the next step is the multiplication operator which in code looks like *=
. It is similar to the addition operator (+=
) but instead of adding the variable on the left by the number on the right we are multiplying the variable on the left by the value on the right.
Here is an example:
x = 5; x *= 2; createP('x = ' + x);
Paste this into your code. What is the value of x? What happens if you change the code to *= 3?
Note: After you are done with this step you can delete or comment out the code above so you can see all the objectives clearly. We won't need this again.
Here is something that you can use to define the factorial function that involves a for loop.
function factorial (n) { thefactorial = ???; for (i = ???; ??? <= ???; i += 1) { thefactorial *= i; } return thefactorial; }
Your job is to replace the ???
with numbers or variable names so that factorial(n)
returns the correct values.
When you are developing new code it is always important to test it. Sometimes this is called a "unit test" because the new code is like a unit and we are testing it. It is important to check a couple of different values of n to make sure the code is working.
Why is it important to check a few different values of n?
The four questions are:
1. Calculate 5!
Test this by setting:
answer1 = factorial(5);
2. Calculate 10!
Test this by setting:
answer2 = factorial(10);
3. Calculate 100!
Test this by setting:
answer3 = factorial(100);
If your factorial function is working correctly you should get all green checks for questions 1 - 3.
Question 4 is a word problem:
"There are 50 patients in the hospital and 121 nurses. How many possibilities are there to assign nurses to the patients? Assume one patient per nurse"
Let's say that a particular hospital has 121 nurses on staff who are available to care for patients. Right now there are 50 patients in the hospital. The charge nurse (i.e. the boss) needs to assign 50 of the available 121 nurses to care for the patients. Each nurse stays with one patient for the entire day and then goes home. Nurses who are not assigned to a patient get the day off.
The question of "How many possibilities are there to assign nurses to the patients?" is really asking how many different combinations of patient and nurses are possible.
Hint #1: this is not something you can easily compute with pencil and paper. The answer is a very large number.
Hint #2: You will need to use one or more factorial(n)
commands to compute the answer.
Hint #3: This video on permutations can help you figure it out.
If you get the answer correct you will get a green for question 4.
At this point you should have all green checks! Yay!
factorial(0)
equals oneTest out your code and see what you get for factorial(0)
. In math 0! is supposed to evaluate to 1. Does this work for your code? If it doesn't, modify your definition of factorial(n)
so factorial(0)
so it gives 1 but be sure to check that your answer for 5! 10! 100! etcetera still work out to be the same values as before.
As you have probably noticed, factorial(100)
evaluates to a very large number. It turns out that if n is too large then the program is overwhelmed and it just says the answer is infinity
For example, try factorial(1000)
and notice that it returns Infinity
. To be clear, this is not correct. Any non-infinte value of n should give a non-infinite value of factorial(n)
but this is not what happens.
What is the largest value of n that gives a non-infinite result from factorial(n)
?
Optional: If you can, try your program on another computer, smartphone or tablet (or xbox!), or try a different browser. Do you get the same result for the largest value of n that gives a non-infinite result?
For more information: The type of error that occurs when we increase n too much is referred to as an "overflow" error. In this particular case it is a floating point overflow. Another type of overflow error is integer overflow.
In math there is also a double factorial that is represented like this: $n!!$
So for example: $$ 6!! = 6 \cdot 4 \cdot 2 = 48 $$
and $$ 7!! = 7 \cdot 5 \cdot 3 \cdot 1 = 105 $$
Create a doublefactorial(n)
function in your code. Test it out (a.k.a. "unit test") so that it matches up with this table:
n | n!! |
---|---|
1 | 1 |
2 | 2 |
3 | 3 |
4 | 8 |
5 | 15 |
6 | 48 |
7 | 105 |
8 | 384 |
9 | 945 |
10 | 3840 |