May 2019
Beginner
528 pages
29h 51m
English
Let’s write a program to perform a famous mathematical calculation. Consider the factorial of a positive integer n, which is written n! and pronounced “n factorial.” This is the product
n · (n – 1) · (n – 2) · … · 1
with 1! equal to 1 and 0! defined to be 1. For example, 5! is the product 5 · 4 · 3 · 2 · 1, which is equal to 120.
You can calculate 5! iteratively with a for statement, as in:
In [1]: factorial = 1In [2]: for number in range(5, 0, -1):...: factorial *= number...:In [3]: factorialOut[3]: 120
Read now
Unlock full access