Nitin`s slides on factors, hcf or gcd, lcm, div & mod, precedence

advertisement
The Mathematics Driving License for
Computer Science- CS10410
Factors, HCF/GCD, LCM, DIV & MOD, Precedence &
Associativity and Infix, Prefix & Postfix Notations
Nitin Naik
Department of Computer Science
Factors
• A factor is a smaller number that divides exactly into a
larger number.
• A factor is simply a number that is multiplied to get a
product.
• For example, 2, 3 and 4 are all factors of 12, because it
can be divided evenly by these numbers:
• 12÷2 = 6
• 12÷3 = 4
• 12÷4 = 3
• Similarly we can say 12 is a multiple of 2, 3 and 4.
Factors..
• 12÷5 = 2.4
• This shows that 5 does not divide exactly into 12.
Thus 5 is not a factor of 12.
• 12÷6=2
• Here 6 divides exactly so 6 is another factor of 12,
other than 2, 3 and 4.
• The complete factors for 12 are: 1, 2, 3, 4, 6, 12.
• All numbers greater than 1 have at least two
factors (number itself and 1).
Factor Types
• Factors are either Composite numbers or Prime numbers
(except that 0 and 1 are neither prime nor composite).
• Prime Number: Numbers with only two factors (itself and
1) are called primes.
• Examples: Prime Numbers up to 100• 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73
,79,83,89,97
• Composite Number: A composite number is any number
that has more than two factors. A natural number that is
not prime.
• Examples: Composite Numbers up to 20• 4,6,8,9,10,12,14,15,16,18,20
Factor Types..
Prime Factorization
• Prime Factorization is the process of finding of
which prime numbers multiply together to make
the original number.
• Example 1: What are the prime factors of 12 ?
• It is best to start working from the smallest prime
number, which is 2, so let's check:
• 12 ÷ 2 = 6
• Yes, 12 is divided evenly by 2. Thus 2 is the first
and smallest prime factor.
Prime Factorization..
• Now remaining 6 is not a prime number, so we need to
go further. Let's try 2 again:
• 6÷2=3
• Yes, that worked also. Thus 2 is again the second prime
factor.
• Now remaining 3 is a prime number, so we stop the
operation and have the answer:
• 12 = 2 × 2 × 3 =22 × 3 -- (using the exponent method
2 × 2 = 2 2)
• As you can see, every factor (2,2,3) is a prime number,
so the answer must be right.
Prime Factorization..
• Example 2: What is the prime factorization of 147 ?
• Can we divide 147 evenly by 2? No, so we should try
the next prime number, 3:
• 147 ÷ 3 = 49
• Then we try factoring 49, and find that 7 is the smallest
prime number that works:
• 49 ÷ 7 = 7
• Now remaining 7 is a prime number, because all the
factors are prime numbers so we stop the operation
and have the answer:
• 147 = 3 × 7 × 7 = 147 = 3 × 72 (7 × 7 = 72)
Prime Factorization..
• Example 3: What is the prime factorization of
19 ?
• The 19 is a Prime Number.
• So any prime number is divisible by only itself
i.e. 19 = 19
• Remember Prime number is not divisible by
any other number except 1 and itself.
Prime Factorization using Factor Tree
• Example 4: What is the prime factorization of 102 ?
• We can also use the factor tree to find out the prime
factors like as shown in the figure:
• Use the same rules of dividing to find any two factors (like
start from 2, then 3 and so on..)
• Continue this process to find out the factors until all the
factors are prime (here 102 = 2 × 3 × 17).
Cryptography & Prime Factorization
• Cryptography is the study of secret codes.
• Prime Factorization is very important to
people who try to make (or break) secret
codes based on numbers.
• That is because factoring very large numbers is
very hard, and can take computers a long time
to do.
Highest Common Factor (HCF) /
Greatest Common Divisor (GCD)
• The highest common factor (HCF) of two numbers (or
expressions) is the largest number (or expression)
which is a factor of both.
• Factors: List all the factors of 40 and 60.
• Factors of 40: 1,2,4,5,8,10,20,40
• Factors of 60: 1,2,3,4,5,6,10,12,15,20,30,60
• Common Factors: Write down the common factors (i.e.
numbers appearing in both lists) - 1, 2, 4, 5, 10, 20.
• Highest Common Factors: The Highest Common Factor
(HCF) of 40 and 60 is: 20
HCF/GCD using Factor Tree
• The highest common factor is very useful in
simplifying fractions.
• Listing all the factors of given numbers is slightly
difficult way to find out HCF, which we used in
the previous example of HCF.
• Another way, we use the factor tree to find the
Highest Common Factor (HCF).
• In factor tree, calculate the product of all prime
factors which are common to both.
• This product is called Highest Common Factor
(HCF).
HCF Examples using Factor Tree
• Example-1: Find HCF of 60 and 36
• Here common prime factors are: 2,2,3
HCF Examples using Factor Tree..
• Example-2: Find HCF of 80 and 150
• Here common prime factors are: 2,5
Euclid's Algorithm for HCF/GCD
• Euclid's algorithm or Euclidean algorithm to
find HCF/GCD.
• This algorithm is very useful to calculate the
greatest common divisor (GCD) of two
arbitrarily large integers.
• The algorithm uses the fact that when two
numbers share a common divisor, subtracting
the smaller number from the larger yields a
result that also shares the common divisor.
Euclid's Algorithm Examples
• Example-3: Find GCD/HCF of 210 and 126
• Solution: The GCD of these two numbers is 42.
Note that 210 / 42 = 5 and 126 / 4 = 3.
• If we were to subtract the smaller value from the
larger we get 210 - 126 = 84. This too divides
equally by 42, giving the result, 2.
• If the process of subtracting the smaller number
from the larger is repeated, eventually one of the
values will become zero.
• At this point, the other value will contain the GCD
for the original pair.
Euclid's Algorithm Examples..
• This iterative process for our example values
yields the following results:
• 210 - 126 = 84
• 126 - 84 = 42
• 84 - 42 = 42
• 42 - 42 = 0
• The final calculation gives a zero result, which
would leave the two values 42 and zero.
• Therefore, the GCD is the value 42.
Euclid's Algorithm Examples..
• Example-4: Find GCD/HCF of 60 and 36
• The iterative subtraction process yields the
following results:
• 60 - 36 = 24
• 36 - 24 = 12
• 24 - 12 = 12
• 12 -12 = 0
• The final calculation gives a zero result, which
would leave the two values 12 and zero.
• Therefore, the GCD is the value 12.
Least Common Multiple (LCM)
• The least common multiple (LCM) of two numbers is
the smallest number (not zero) that is a multiple of
both.
• Multiples: The multiples of a number are what you get
when you multiply it by other numbers (such as if you
multiply it by 1,2,3,4,5, etc).
• It is just like the multiplication table.
• Example: List the multiples of 4 and 5:
• The multiples of 4 are:
4,8,12,16,20,24,28,32,36,40,44,48,...
• The multiples of 5 are:
5,10,15,20,25,30,35,40,45,50,55,60,...
Least Common Multiple (LCM)..
• Common Multiples : When you list the multiples of two
(or more) numbers, and find the same value in both
lists, then that is a common multiple of those numbers.
• For example, when you write down the multiples of 4
and 5, the common multiples are those that are found
in both lists:
• The multiples of 4 are:
4,8,12,16,20,24,28,32,36,40,44,48,...
• The multiples of 5 are:
5,10,15,20,25,30,35,40,45,50,55,60,...
• The common multiples of 4 and 5 are 20,40,...
Least Common Multiple (LCM)..
• Least Common Multiples : It is simply the
smallest of the common multiples.
• In our example, the smallest of the common
multiples is 20.
• So the Least Common Multiple of 4 and 5 is
20.
• It is a really easy thing to do. Just start listing
the multiples of the numbers until you get a
match.
LCM Examples
•
•
•
•
•
Example-1: Find LCM of 3 and 5.
Multiples: List all the multiples of 3 and 5.
Multiples of 3: 3, 6, 9, 12, 15,18,21,24,27,30, ...,
Multiples of 5: 5, 10, 15, 20,25,30,35,40, ...,
Common Multiples : Write down the common
multiples (i.e. numbers appearing in both lists) –
15,30,...
• Least Common Multiples : Least Common
Multiple (LCM) of 3 and 5 is: 15
LCM using Factor Tree
• Like the HCF/GCD, we use the factor tree to
find the Least Common Multiple (LCM).
• In factor tree, calculate the product of all the
prime factors of the one (larger) number by
those prime factors of the other (smaller)
number that are not already included.
• This product is called Least Common Multiple
(LCM).
LCM Examples using Factor Tree
• Example-2: Find LCM of 60 and 36
• Here one number’s prime factors are: 2,2,3,5 and
other number’s factors (except earlier) is:3
LCM Examples using Factor Tree ..
• Example-3: Find LCM of 80 and 150
• Here one number’s prime factors are: 2,2,2,2,5
and other number’s factors (except earlier)
are:3,5
DIV and MOD Functions
• The division process has two outputs: Quotient and
Remainder.
• MOD stands for Modulo (remainder of division) and
DIV for Division (quotient of division).
• DIV and MOD functions are used to represent these
outcomes for the Division of two natural numbers (i.e.
Integers, in programming ).
• The DIV function returns the Quotient.
• The MOD function returns the Remainder.
• In many programming languages DIV is represented by
symbol / and MOD is by symbol %
DIV and MOD Functions..
Examples
•
•
•
•
•
•
•
4 DIV 2 = 2 and 4 MOD 2 = 0
5 DIV 2 = 2 and 5 MOD 2 = 1
50 DIV 10 = 5 and 50 MOD 10 = 0
53 DIV 10 = 5 and 53 MOD 10 = 3
14 / 4 = 3 and 14 % 4 = 2
3 / 4 = 0 and 3 % 4 = 3
20 / 40 = 0 and 20 % 40 = 20
Operator Associativity
• The associativity (or fixity) of an operator is a
property that determines how operators of
the same precedence are grouped in the
absence of parentheses.
• If two operators in an expression have the
same precedence level, they are evaluated
from left to right or right to left depending on
their associativity.
Operator Associativity..
• Example-1:
• Addition’s associativity is Left to Right, so the
expression 2+3+4 is evaluated as (2+3)+4.
• In contrast, the assign operator’s associativity
is Right to Left; so the expression x=y=z is
evaluated as x=(y=z).
Operator Associativity..
• Example-2:
• It's important, since it changes the meaning of an
expression.
• Consider the division operator with integer arithmetic,
which is left associative:
• 4 / 2 / 3 <=> (4 / 2) / 3 <=> 2 / 3 = 0
• If it were right associative, it would evaluate to an
undefined expression, since you would divide by zero
• 4 / 2 / 3 <=> 4 / (2 / 3) <=> 4 / 0 = undefined
Operator Precedence / Order of
Operations
• In mathematics or computer programming,
the operator precedence is a rule that
determines which operators should be
performed first in a given mathematical
expression.
• When two operators share an operand the
operator with the higher precedence goes
first.
Operator Precedence..
•
•
•
•
Example-1:
x = 7 + 3 * 2 = 20 OR x = 7 + 3 * 2 = 13
( x is assigned 13, not 20 )
The previous statement is equivalent to the
following:
• x = 7 + ( 3 * 2 ) = 7 + 6 =13
• Using parenthesis in an expression alters the
default precedence. For example:
• x = (7 + 3) * 2 = 10 * 2 = 20 ( (7 + 3) is evaluated
first and x=20 )
Operator Precedence..
• Example-2:
• For instance, the expression "4 + 5 * 6 / 3" will
be treated as "4 + ((5 * 6) / 3))" and 14 is
obtained as a result.
Infix, Prefix and Postfix Notations
• Infix, Prefix and Postfix notations are three
different but equivalent ways of writing
expressions.
• In all three notations, the operands occur in the
same order, and just the operators have to be
moved to keep the meaning correct.
• This is particularly important for asymmetric
operators like subtraction and division: A - B does
not mean the same as B - A; the former is
equivalent to A B - or - A B, the latter to B A - or B A).
Infix Notation (X + Y)
• This is the standard way of writing expressions.
• In which operators are written in-between their
operands like X + Y.
• Example: An expression such as A * ( B + C ) / D is
usually taken to mean something like:
1. First add B and C together,
2. Then multiply the result by A,
3. Then divide the result by D to give the final
answer.
Problems with Infix Notation (X + Y)
• Infix notation needs extra information to make
the order of evaluation of the operators clear.
• Some rules are needed into the programming
language about operator precedence and
associativity.
• Also parentheses ( ) to allow users to override
these rules.
• In computer and programming languages the
Prefix and Postfix notations are commonly used
instead of Infix.
Prefix Notation (+ X Y)
• This is the preferred computer way of writing
expressions.
• In which operators are written before their
operands like: + X Y.
• Prefix notation was introduced by the polish
logician Lukasiewicz.
• Prefix notation also came to be known as
Polish Notation in honour of Lukasiewicz.
Prefix Notation (+ X Y)..
• Example: The previous expression is:
A*(B+C)/D
• It can be written in Prefix notation such as:
/ * A + B C D usually taken as a same.
• Operators act on the two nearest values on the
right.
• The order of evaluation of operators is always
left-to-right.
• Parentheses () cannot be used to change this
order.
Prefix Notation (+ X Y)..
• Although Prefix operators are evaluated left-to-right,
they use values to their right, and if these values
themselves involve computations then this changes the
order that the operators have to be evaluated in.
• In the example : / * A + B C D , although the division is
the first operator on the left, it acts on the result of the
multiplication [A* (B+C)] and D [A* (B+C)/D].
• So the multiplication has to happen before the division
and similarly the addition (B+C) has to happen before
the multiplication [A* (B+C)].
Postfix Notation (X Y +)
• This is the preferred computer way of writing
expressions.
• In which operators are written before their
operands like: + X Y.
• Prefix notation was introduced by the polish
logician Lukasiewicz.
• Postfix notation also came to be known as
Reverse Polish Notation in honour of
Lukasiewicz.
Postfix Notation (X Y +)..
• Example: The previous expression is:
A*(B+C)/D
• It can be written in Postfix notation such as:
A B C + * D / usually taken as a same.
• Operators act on the two nearest values on the
left.
• Like the Prefix, the order of evaluation of
operators is always left-to-right.
• Parentheses () cannot be used to change this
order.
Postfix Notation (X Y +)..
• Because Postfix operators use values to their left, any
values involving computations will already have been
calculated as we go left-to-right, and so the order of
evaluation of the operators is not disrupted in the
same way as in Prefix expressions.
• In the example : A B C + * D / , the addition is the first
operator on the left so (B+C) happens first.
• Then multiplication happens on the result of the (B+C)
and A [A* (B+C)].
• Finally, division will happen on the result of the [A*
(B+C)]and D [A* (B+C)/D].
Conversion Techniques (Infix, Prefix
and Postfix )
• We can programmatically convert expressions
from one form into another.
• There are various methods such as:
1) Using Stacks
2) Using Expression Trees
Examples
References
• http://www.mathsisfun.com/least-commonmultiple.html
• http://www.cs.man.ac.uk/~pjj/cs212/fix.html
• http://www.dreamincode.net/forums/topic/3742
8-converting-and-evaluating-infix-postfix-andprefix-expressions-in-c/
• http://www.cs.gsu.edu/~skarmakar/cs3410/InPost-PrefixNotation.ppt
• http://www.wikihow.com/Find-the-LeastCommon-Multiple-of-Two-Numbers
Thank You
Download