Download this introduction

Transcript
which gives 3, the correct residue.
The opposite of the “mod” operator is the integer quotient. To find the integer quotient of a number,
use
25 // 4
to get 6. This is the quotient rounded down to the nearest integer.
You can define
def is_even(n): return n%2 == 0
which will return True for even integers, and False for odd ones. This can then be used to wrap up into
other if statements. We’ll talk much more about if statements in the chapter on programming.
3.2
Minor Commands of SAGE
Here we list a few commands
that require them.
min(x,y)
max(x,y)
floor(x)
ceil(x)
factorial(n)
that come up from time to time. They can be quite useful in the situations
Returns either x or y, whichever is smaller.
Returns either x or y, whichever is greater.
Just round x down, or bxc
Just round x up, or dxe
This is n!, also called “n factorial.”
binomial(x,m)
This is usually written
x
m
binomial(x,m) * m!
This is usually written
x!
m!
3.2.1
or
x!
m!(x−m)!
or x Cm
or x Pm
Rounding, Floors, and Ceilings
As you can see, the floor command rounds a number down, and the ceil command rounds a number up.
In older books, the “floor” function is sometimes called “The Greatest Integer Function.” This is because
it can be defined by “the greatest integer that is less than or equal to x.” Some older mathematics books
will write [x] instead of bxc. I suppose if you wanted, you could define the ceiling function by “the least
integer that is greater than or equal to x.”
3.2.2
Combinations and Permutations
In a lot of textbooks, particularly for Finite Mathematics, or Quantitative Literacy, as well as a basic
Probability & Statistics, a lot of time is spend talking about combinations and permutations. For example,
if I have 52 distinct cards in a deck of cards, I might ask how many 5-card hands I can build with them.
If order doesn’t matter, such as in Poker, then I would have
52
52!
52 × 51 × 50 × 49 × 48
=
=
= a lot
52 C5 =
5
5!47!
5×4×3×2×1
possibilities, and SAGE calls this binomial(52, 5). This is an example of the “combinations” formula.
However, if order does matter, which is not the case for poker, then I would have
52
52!
= 52 × 51 × 50 × 49 × 48 = even more
P
=
5! =
52 5
5
47!
possibilities. In SAGE, you can type
factorial(52)/factorial(52-5)
60