3.factoring Methods
3.factoring Methods
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)
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.