elementary number theory - wordpress.com · 7/3/2017 · example use the definitions of even and...
TRANSCRIPT
Elementary Number Theory
Even and Odd Integers
Definition
An integer n is even if, and only if, n equals twice some integer. An integer n is odd if, and only if, n equals twice some integer plus 1.
Symbolically, if n is an integer, then
n is even ⇔ ∃ an integer k such that n = 2k.
n is odd ⇔ ∃ an integer k such that n = 2k + 1.
Example
Use the definitions of even and odd to justify your answers to the following questions.
a. Is 0 even?
b. Is−301 odd?
c. If a and b are integers, is 6a2b even?
d. If a and b are integers, is 10a+8b+1 odd?
e. Is every integer either even or odd?
Prime and Composite Numbers
Definition
An integer n is prime if, and only if, n>1 and for all positive integers r and s, if n = rs, then either r or s equals n.
An integer n is composite if, and only if, n >1 and n=rs for some integers r and s with 1< r < n and 1< s < n.
Prime and Composite Numbers
In symbols:
n is prime ⇔ ∀ positive integers r and s, if n =rs then either r =1 and s =n or r =n and s =1.
n is composite ⇔ ∃ positive integers r and s such that
n = rs and 1 < r < n and 1 < s < n.
Example
a. Is 1 prime?
b. Is every integer greater than 1 either prime or composite?
c. Write the first six prime numbers.
d. Write the first six composite numbers.
Perfect Square
Definition:
An integer n is called a perfect square if, and only if, n = k2
for some integer k.
Example
Definition
A real number r is rational if, and only if, it can be expressed as a quotient of two integers with a nonzero denominator. A real number that is not rational is irrational.
More formally, if r is a real number, then
r is rational ⇔ ∃ integers a and b such that r = a/b and b≠0.
Example
a. Is 10/3 a rational number?
b. Is −5/39 a rational number?
c. Is 0.281 a rational number?
d. Is 7 a rational number?
e. Is 0 a rational number?
f. Is 2/0 a rational number?
g. Is 2/0 an irrational number?
h. Is 0.12121212... a rational number (where the digits 12 are assumed to repeat forever)?
i. If m and n are integers and neither m nor n is zero, is (m+n)/mn a rational number?
Definition
If n and d are integers and d ≠ 0 then
n is divisible by d if, and only if, n equals d times some integer. Instead of “n is divisible by d,” we can say that
n is a multiple of d, or
d is a factor of n, or
d is a divisor of n, or
d divides n.
Definition
The notation d|n is read “d divides n.” Symbolically, if n and d are integers and d ≠ 0:
d|n ⇔ ∃ an integer k such that n =dk.
Example
a. Is 21 divisible by 3?
b. Does 5 divide 40?
c. Does 7|42?
d. Is 32 a multiple of−16?
e. Is 6 a factor of 54?
f. Is 7 a factor of−7?
g. If a and b are integers, is 3a+3b divisible by 3?
h. If k and m are integers, is 10km divisible by 5?
Quotient-Remainder Theorem
Theorem
Given any integer n and positive integer d, there exist unique integers q and r such that
n = dq + r and 0 ≤ r < d.
Example
For each of the following values of n and d, find integers q and r such that n = dq + r and 0 ≤ r < d.
a. n =54, d =4
b. n =−54, d =4
c. n =54, d =70
div and mod
• A number of computer languages have built-in functions that enable you to compute many values of q and r for the quotient-remainder theorem.
• These functions are called div and mod in Pascal, are called / and % in C and C++, are called / and % in Java, and are called / (or \) and mod in .NET.
div and mod
• The functions give the values that satisfy the quotient-remainder theorem when a nonnegative integer n is divided by a positive integer d and the result is assigned to an integer variable.
• However, they do not give the values that satisfy the quotient-remainder theorem when a negative integer n is divided by a positive integer d.
Definition
Given an integer n and a positive integer d,
n div d = the integer quotient obtained when n is divided by d, and
n mod d = the nonnegative integer remainder obtained when n is divided by d.
Symbolically, if n and d are integers and d > 0, then
n div d=q and n mod d=r ⇔ n =dq+r where q and r are integers and 0≤ r < d.
Definition
Symbolically, if n and d are integers and d > 0, then
n div d = q and n mod d= r ⇔ n = dq + r
where q and r are integers and 0 ≤ r < d.
Example
• Compute 32 div 9 and 32 mod 9 by hand and with a calculator.
• Suppose today is Thursday, and neither this year nor next year is a leap year. What day of the week will it be 1 year from today?
• Suppose m is an integer. If m mod11=6, what is 4m mod11?
FLOOR AND CEILING
Floor
• Given any real number x, the floor of x, denoted 𝑥 , is defined as follows:
𝑥 =that unique integer n such that n ≤ x < n+1.
Symbolically, if x is a real number and n is an integer, then
𝑥 =n ⇔ n ≤ x < n+1.
Ceiling
• Given any real number x, theceiling of x, denoted 𝑥 , is defined as follows:
𝑥 =that unique integer n such that n−1 < x ≤n.
Symbolically, if x is a real number and n is an integer, then
𝑥 =n ⇔ n−1< x ≤ n.
Examples
• Compute for the floor and ceiling
a. 25/4 b. 0.999 c. −2.01
Examples
• The 1,370 students at a college are given the opportunity to take buses to an out-of-town game. Each bus holds a maximum of 40 passengers.
• a. For reasons of economy, the athletic director will send only full buses. What is the maximum number of buses the athletic director will send?
• b. If the athletic director is willing to send one partially filled bus, how many buses will be needed to allow all the students to take the trip?
Absolute Value
• Definition
• For any real number x, the absolute value of x, denoted |x|, is defined as follows:
|x|= x if x ≥0
−x if x < 0 .
Greatest Common Divisor
Definition
Let a and b be integers that are not both zero. The greatest common divisor of a and b, denoted gcd(a,b), is that integer d with the following properties:
1. d is a common divisor of both a and b. In other words, d|a and d|b.
2. For all integers c, if c is a common divisor of both aand b, then c is less than or equal to d. In other words,
for all integers c, if c|a and c|b, then c ≤ d.
Calculate the gcd
a. Find gcd(72, 63).
b. Find gcd(1020,630).
c. In the definition of greatest common divisor, gcd (0, 0) is not allowed. Why not?
Application: Algorithm
• Refers to a step-by-step method for performing some action.
• Examples in everyday life are food preparation recipes, directions for assembling equipment or hobby kits, sewing pattern instructions, and instructions for filling out income tax forms.
• Much of elementary school mathematics is devoted to learning algorithms for doing arithmetic such as multidigit addition and subtraction, multidigit multiplication, and long division
Count the number of persons in the room
30
ALGORITHMS
Example:
Finding the Maximum Element in a Finite Sequence.
procedure max(a1, a2 , . . . , an : integers)
max : = a1
for i : = 2 to n
if max < ai then max := ai
return max {max is the largest element}
31
32
ALGORITHMS
Introduction
There are many general classes of problems that arise in discrete mathematics. For instance:
given a sequence of integers, find the largest one;
given a set, list all its subsets;
given a set of integers, put them in increasing order;
given a network, find the shortest path between two vertices.
33
ALGORITHMS
• When presented with such a problem, the first thing to do is to construct a model that translates the problem into a mathematical context.
• Setting up the appropriate mathematical model is only part of the solution. To complete the solution, a method is needed that will solve the general problem using the model.
• Ideally, what is required is a procedure that follows a sequence of steps that leads to the desired answer.
34
ALGORITHMS
• An algorithm is a finite sequence of precise instructions for performing a computation or for solving a problem.
35
ALGORITHMS
• Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers.
• 1. Set the temporary maximum equal to the first integer in the sequence. (The temporary maximum will be the largest integer examined at any stage of the procedure.)
• 2. Compare the next integer in the sequence to the temporary maximum, and if it is larger than the temporary maximum, set the temporary maximum equal to this integer.
• 3. Repeat the previous step if there are more integers in the sequence.
• 4. Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence.
FLOW CHART
• diagram that represents an algorithm, workflow or process, showing the steps as boxes of various kinds, and their order by connecting them with arrows.
36
6 BASIC SYMBOLS USED
• Terminal• Indicates the starting or ending
• Input/Output
• Process• Indicates the operation
37
6 BASIC SYMBOLS USED
• Decision• Yes or No
• Connector• Used to connect breaks
• Control Flow• Show direction
38
FLOW CHART RULES
• Top to bottom
• Boxes must connected with arrows
• Start with a Terminal or Process symbol
• Decision symbols have 2 exit points, YES and NO
39
Create Flow Charts for the following• Addition of 6 and 4
• Sum of 3 numbers
• Print “Passed” 5 times
• Logging in to Facebook
40
41
PSEUDOCODE
• An algorithm can also be described using a computer language
• A form of pseudocode is used instead of using a particular computer language like C, C++, Java, etc… to specify algorithms
• Pseudocode provides an intermediate step between an English language description of an algorithm and an implementation of this algorithm in a programming language.
42
PSEUDOCODE
• In pseudocode, the instructions used can include any well-defined operations or statements.
• A computer program can be produced in any computer language using the pseudocode description as a starting point.
43
COMMON PSEUDOCODE STATEMENTS
Procedure
Assignment
Comments
Conditional Constructions
Loop Constructions
Return
44
Procedure Statements
The pseudocode for an algorithm begins with a procedure statement that gives the name of an algorithm, lists the input variables, and describes what kind of variable each input is.
Example:
procedure maximum(L: list of integers)
is the first statement in the pseudocode description of the algorithm, which we have named maximum, that finds the maximum of a list L of integers.
45
Assignment Statements
• used to assign values to variables.
symbol := is used
• the left-hand side is the name of the variable and the right-hand side is an expression that involves constants, variables that have been assigned values, or functions defined by procedures and may contain any of the usual arithmetic operations.
variable := expression
• Example:
max := a
x := largest integer in the list L
46
Conditional Constructions
• The simplest form of the conditional construction that we will use is
• if condition then statement
or
• if condition then block of statements
or
• if condition then statement 1
else statement 2
• Example: If a > b then a = c
else a = d
Execution of if-then-else and if-then Statements Consider the following algorithm segments:
a. if x > 2
then y := x +1
else do x := x −1
y :=3·x end do
b . y :=0
if x > 2 then y :=2x
What is the value of y after execution of these segments for the following values of x?
i. x = 5 ii. x = 2
48
Loop Constructions
• Iterative statements are used when a sequence of algorithm statements is to be executed over and over again.
• for variable := initial value to final value statement
or
• for variable := initial value to final value block of statements
• Example:
sum := 0
for i := 1 to n
sum := sum + i
Trace Table for a for-next Loop
Construct a trace table for the loop.
for i :=1 to 4
x :=i2
next i
50
Loop Constructions
• while condition statement
or
• while condition block of statements
• Example:
sum := 0
while n > 0
sum := sum +n
n := n − 1
Tracing Execution of awhile while while Loop Trace the execution of the following algorithm segment by finding the values of all the algorithm variables each time they are changed during execution:
i :=1,s :=0
while ( i ≤ 2)
s :=s + i
i := i +1
end while
52
Comments
• statements enclosed in curly braces are not executed. Such statements serve as comments or reminders that help explain how the procedure works.
• For instance, the statement
{x is the largest element in L}
can be used to remind the reader that at that point in the procedure the variable x equals the largest element in the list L.
53
RETURN
• We use a return statement to show where a procedure produces output.
• A return statement of the form return x produces the current value of x as output. The output x can involve the value of one or more functions, including the same function under evaluation, but at a smaller value.
• For instance, the statement return f (n − 1) is used to call the algorithm with input of n − 1. This means that the algorithm is run again with input equal to n − 1.
54
ALGORITHMS
Example: Finding the Maximum Element in a Finite Sequence.
procedure max(a1, a2 , . . . , an : integers)
max : = a1
for i : = 2 to n
if max < ai then max := ai
return max {max is the largest element}
55
PROPERTIES OF ALGORITHMS
• Input. An algorithm has input values from a specified set.
• Output. From each set of input values an algorithm produces output values from a specified set. The output values are the solution to the problem.
• Definiteness. The steps of an algorithm must be defined precisely.
56
PROPERTIES OF ALGORITHMS
• Correctness. An algorithm should produce the correct output values for each set of input values.
• Finiteness. An algorithm should produce the desired output after a finite (but perhaps large) number of steps for any input in the set.
57
PROPERTIES OF ALGORITHMS
• Effectiveness. It must be possible to perform each step of an algorithm exactly and in a finite amount of time.
• Generality. The procedure should be applicable for all problems of the desired form, not just for a particular set of input values.
Division Algorithm
Input: a [a nonnegative integer], d [a positive integer] Algorithm Body:
r :=a, q :=0
while (r ≥ d)
r :=r −d
q :=q +1
end while
Output: q, r
Tracing the Division Algorithm
Trace the action of the division algorithm on the input variables
a =19 and d = 4.
a =57 and d = 6.
Euclidean Algorithm
Input: A, B [integers with A > B ≥ 0]
Algorithm Body:
a := A, b := B, r := B
while (b≠0)
r :=a mod b
a :=b
b :=r
end while
gcd:=a
Output: gcd
Tracing the Euclidean Algorithm
Use the Euclidean algorithm to calculate the greatest common divisors of each of the pairs of integers
1,188 and 385
509 and 1,177