0% found this document useful (0 votes)
5 views42 pages

3.factoring Methods

Uploaded by

predator103722
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views42 pages

3.factoring Methods

Uploaded by

predator103722
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

FACTORING

METHODS
CHAPTER 3
3.1: Finding the Square Root of a
Number
Problem: given a number m devise an algorithm to compute its square
root.
The square root of m should be smaller than m.
What can be a good estimate?
◦ g1 =m/2 ?
Next estimate?
◦ g2 = (g1 + (m/g1))/2
Next?
◦ g1= g2
3.1: Finding the Square Root of a
Number
When to stop?
◦ When the absolute difference between the two estimates is less than the
acceptable error
Input: m and err, Output: sqrt of m
Variables : g1-previous estimate of square root
g2- current estimate of square root. (Initially g2= m/2)
while (|g1-g2| < err)
◦ g1 = g2
◦ g2 = (g1+m/g1)/2
3.1: Finding the Square Root of a
Number
At the nth step:
◦ (s - the desired square root and e - the corresponding error term)

◦ With and for small e, we get


◦ The quadratic term confirms that the method will converge rapidly
to the desired result.
3.2: The Smallest Divisor of an Integer
Problem: Given an integer n devise an algorithm that will
find its smallest exact divisor other than one.
Solution: check for divisibility for all numbers < n, till we
find the smallest divisor.
Is this efficient?
If n is an even number then we know that the smallest
divisor will be 2.
3.2: The Smallest Divisor of an Integer
Compute the square root (r) of n
Initialize the first divisor guess (d) to be 3.
While not an exact divisor and less than r
◦ Assign the next odd number in the sequence to d
If the current odd value d is an exact divisor
◦ return d
Else
◦ return 1
3.2: The Smallest Divisor of an Integer
Input- n, Output- d ( smallest divisor of m)
Variables: d (member of odd sequence)
r (integer less than or square root of n)
If even, then return d = 2, else d = 3
While ( n mod d !=0 & d < r)
◦ d = d+2
If n mod d = 0
◦ return d
Else
◦ return 1 ( n is prime)
3.2: The Smallest Divisor of an Integer
The algorithm is guaranteed to terminate because d is
incremented by 2 and eventually the condition d ≥ r will be
satisfied.
The algorithm takes at most iterations.
3.3 The Greatest Common Divisor of
Two Integers
Problem: Given two positive non-zero integers m and n design
an algorithm for finding their greatest common divisor (gcd).
Solution: Find individual divisors for both m and n, compare
and return the largest.
Problem with the above approach?
Let m be smaller than n, then no number larger than m can be
the gcd.
◦ It is possible that the smaller number is the gcd.
3.3 The Greatest Common Divisor of
Two Integers
Changing the original problem into an equivalent problem.
Let m = 30 and n = 18.
This can be converted to gcd between 18 and 12.
Since 12 does not divide 18 fully, this problem is then
converted to gcd between 12 and 6.
3.3 The Greatest Common Divisor of
Two Integers
Input: integers m and n, Output: integer q = gcd(m,n)
Integer: remainder r = m%n
while (r is not zero){
◦ get the remainder ( r ) by dividing the larger integer by the smaller
integer.
◦ let the smaller integer assume the role of the larger integer
◦ let the remainder assume the role of the divisor
}
3.4 Generating Prime Numbers
Problem: design an algorithm to establish all the primes in the first n positive
integers.
How to test for primality?
◦ To see if a given number x is prime, check for divisibility with all integers smaller than
x.
Required storage space for large integers will be huge.
Design an algorithm in which we can reduce the numbers to test for primality.
Given an integer n, apart from 2 we need to only consider odd integers to test
for divisibility.
Start with x=1 and then increment x as x = x+2
3.4 Generating Prime Numbers
Consider n = 20 then
◦ Numbers to be tested: 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19
◦ Removing multiples of 2: 2,3,5,7,9,11,13,15,17,19
◦ Removing multiples of 3: 2,3,5,7,11,13,17,19
◦ dx = abs(dx-6) initially dx = 4
Until value of x < 25, the above methodology (Sieve of Eratosthenes)
will generate all primes < 25.
Given a larger number we will need to eliminate integers which are
products of more prime numbers.
Storage proportional to the span of integers up to n.
3.4 Generating Prime Numbers
To test for primality of a number n it is sufficient to consider
prime divisors up to √n
Why not consider composite numbers?
For example, given n = 1000, we should consider only all
primes up to 31.
Thus from storing n-1 numbers for testing divisibility of a
given integer n, by this method we will store only primes up
to √n
3.4 Generating Prime Numbers
To test all integers up to n for primality we will need to
retain all primes up to √n
n range Prime divisors required
2
2,3
2,3,5
2,3,5,7
3.4 Generating Prime Numbers
1. An array of the prime numbers to be used for testing the
primality a number x<n: p.
2. A variable to store the value of the square of the largest prime
used for testing: plimsq
3. An index to track the prime numbers array p: j
4. An upper index for primes less than sqrt(x): limit
5. An index to track the prime numbers saved: i
6. A Boolean value to decide if x is prime: prime
3.4 Generating Prime Numbers
while x<n do j = 3, prime = true
◦ begin
while prime and (j<limit) do
◦ dx = abs(dx-6); ◦ begin
◦ x = x + dx; ◦ rem = x mod p[j]
◦ if limit ≤ i then ◦ prime = rem!=0;
◦ if x ≥ plimsq then ◦ j = j+1
◦ begin ◦ end
◦ limit = limit +1; ◦ if prime then “write out x and save if
◦ if limit ≤ i then necessary”
◦ plimsq = sqr (p[limit])
end
◦ end;
3.4 Generating Prime Numbers
For large n a lot of time will be spent in the inner loop doing
costly divisions ( in the mod function)
How can the above division testing be eliminated?
3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47

Sieve method: cross out all the multiples in advance and


then make a single pass through the array and pick out the
numbers not crossed.
3.4 Generating Prime Numbers
Whenever x is incremented, increment the multiples of 3
and 5 while they are less than x.
If none of the multiples of 3 and 5 are equal to x then x must
be prime.
In general case this check will be made with the multiples of
all primes less than or equal to √x: create an array called
multiple.
3.4 Generating Prime Numbers
while x<n do j = 3, prime = true
◦ begin while prime and (j<limit) do
◦ dx = abs(dx-6); ◦ begin
◦ x = x + dx; ◦ while multiple[j] < x:
◦ if limit ≤ i then ◦ multiple[j]=multiple[j] + p[j] *2;
◦ if x ≥ plimsq then ◦ prime = x != multiple[j];
◦ begin ◦ j = j+1
◦ multiple[limit] = plimsq; ◦ end
◦ if prime then write(x)
◦ if limit ≤ i then
◦ If prime and x <= root(n):
◦ plimsq = sqr (p[limit])
◦ i = i+1;
◦ end; ◦ p[i] = x;

end
3.4 Generating Prime Numbers
The outermost while loop will terminate because the difference
between x and n decreases by 2 with each pass through the
outermost loop.
The prime-test loop will terminate because the difference between j
and limit decreases by 1 with each pass through the loop.
The innermost while loop involving multiples will terminate
because multiple[j] is incremented by 2*p[j] with each iteration.
Since all loops terminate, the whole process will terminate.
3.5: Computing the Prime Factors of an
Integer
Problem: Design an algorithm to compute the prime factors
of an integer.
Consider n = 60.
All prime factors of n must be lesser than √n.
Are all the primes lesser than √n needed?
Consider n=1024, primes up to √1024 = primes up to 32.
◦ What is the largest prime factor of 1024?
3.5: Computing the Prime Factors of an
Integer
Compute prime factors only when needed.
When do the divisions terminate?
◦ When the quotient is a prime number or the quotient is 1.
How can we test for non-primality of the number?
◦ Use integer division and test for zero remainder
◦ The quotient resulting from the division is greater than the prime
being tested for factorization.
3.5: Computing the Prime Factors of
an Integer
1. Establish n the number whose prime factors are 1. Given n
sought.
2. nxtprime =2, q = n div nxtprime, r = n mod nxtprime
2. Compute the remainder r and quotient q for the first
prime nxtprime = 2 3. i =0

3. while it has not been established that n is prime do while (r == 0) or ( q > nxtprime) {
◦ (i) if nxtprime is an exact divisor of n then if r == 0 {
i = i+1;
◦ Save nxtprime as a factor f
f[i] = nxtprime;
◦ Reduce n by nxtprime
n = q;
◦ else } else Eratosthenes( d, nxtprime) {d is the array containing the
◦ Get next biggest prime from Sieve of Eratosthenes primes}
◦ (ii) compute next quotient q and remainder r for q = n div nxtprime;
current value of n and current prime divisor nxtprime r = n mod nxtprime;
4. if n>1{
4. if n is greater than 1 add n to list as prime factor f
i = i+1;
5. return the prime factors f of the original number n f[i] = n;
}
3.5: Computing the Prime Factors of an
Integer
The condition that remains invariant for the prime factoring
loop is after an iteration with the divisor nxtprime all prime
factors less than nxtprime will be established.
Also n would have been reduced at least by the product of
all its prime factors lesser than nxtprime.
The algorithm must eventually terminate because with each
iteration either n decreased or nxtprime increased leading to
eventually the condition q> nxtprime failing.
3.7: Raising a Number to a Large Power
Problem: Given some integer x, compute the value of xn where n is
considerably greater than 1.

Consider x10 by the above method 9 multiplications are needed.


Exponential can be written as a sum of two numbers.
10 = 1+9 = 2+8 = 3+7 = 4+6 = 5+5.
3.7: Raising a Number to a Large Power
Choose the smallest pair of numbers such that they add up to 10.
For 10, the smallest pair is (5,5).
For 5, the smallest pair is (2,2)
Thus we have the following multiplications:
,
,
,

Only 4 multiplications are needed in this process.


3.7: Raising a Number to a Large Power
3.7: Raising a Number to a Large Power
Only 7 multiplications are required for
At each stage one of the following two conditions hold
◦ (a) for odd power we must have it generated with power that is one less
◦ (b) for even power it can be computed from a power that is half its size.
The algorithm can be studied in two parts.
(a) a part that determines the multiplication strategy
(b) a second power that actually does the power evaluation.
3.7: Raising a Number to a Large Power
To map out the multiplication procedure we use the following technique.
Even/Odd information can be recorded in an array, say d.
Divide the power by 2
◦ If quotient is odd save 1 in the position in d
◦ If quotient is even save 0 in the position in d.
Example:
3.7: Raising a Number to a Large Power
Power evaluation
Initialize p to 1
If the current element in d is 0 then
◦ square the current power product p
else
◦ square the current power product p and multiply by x to generate an odd power
3.7: Raising a Number to a Large Power
The repeated division of n by 2 has the effect of computing the binary
representation of n.
If the power evaluation can be done while attaining the binary
representation of n, there may not be a need to save the multiplication
strategy.
NOTE: derivation of the binary representation of the power gives us the
binary digits in the order from the least significant bit to the most
significant.
Thus power evaluation can be directly coupled with the derivation of the
binary representation as follows.
3.7: Raising a Number to a Large Power
Variables:
◦ product – denotes the accumulated product
◦ psequence – denotes the successive members of the power sequence
Initialization:
◦ product = 1 // for the case when n (power) is 0
◦ psequence = x // the number that is raised to n.
Loop:
◦ With successive iterations psequence will be doubled
◦ With successive iterations n = n/2
◦ When the next msb is 1, then product = product × psequence
3.7: Raising a Number to a Large
Power
3.7: Raising a Number to a Large Power
3.7: Raising a Number to a Large Power
The while loop generates the binary representation of the integer n for
all values of n > 0.
After the ith iteration the i rightmost bits of the binary representation of
n have been generated and x has been raised to a power equal to the
value of these i rightmost bits.
In the same loop n has been reduced by 2i
The algorithm terminates because with each iteration n is reduced and
so eventually the condition n>0 will be false.
Strategy used: divide and conquer.
3.8: Computing the nth Fibonacci
Number
Problem: Given a number n generate the n th member of the
Fibonacci sequence

We shall study a more efficient way to generate the n th


Fibonacci number
Can we use divide and conquer, like the previous algorithm?
◦ No doubling relation like powers exists between Fibonacci
numbers.
3.8: Computing the nth Fibonacci
Number

Is the above solution complete?


To evaluate by the above strategy one would require
how to get the value ?
Normal way: but then we cant get using the doubling
strategy we just found.
On further substitutions we find
3.8: Computing the nth Fibonacci
Number
So now the algorithm is divided into two parts
◦ A part that determines the doubling strategy by generating the
binary representation of n.
◦ A part that actually computes the nth Fibonacci number according to
the doubling strategy.
Relation between pairs:
3.8: Computing the nth Fibonacci
Number
3.8: Computing the nth Fibonacci
Number
When the binary digit is 0 we need to carry out only the
doubling process. i.e
◦ and
When the binary digit is 1 we need to carry out the doubling
process and then extend the sequence by one before
doubling again.
◦ and
3.8: Computing the nth Fibonacci
Number
For a given value of n the algorithm requires of the order
steps to compute the nth Fibonacci number
The while loop terminates because n follows a strictly
decreasing sequence to 1 due to the repeated division by 2.

You might also like