with no sell. For example the GCD of 16 and 28 is 4. In chapter 2 when we analyse how to implement rational-number arithmetic we will be to be able to reason GCDs in order to decrease rational numbers to lowest terms. (To reduce a rational number to lowest terms we must change integrity both the numerator and the denominator by their GCD. For example. 16/28 reduces to 4/7.) One way to sight the GCD of two integers is to factor them and search for common factors but there is a famous algorithm that is much more efficient.
reduces GCD(206,40) to GCD(2,0) which is 2. It is possible to show that starting with any two positive integers and performing repeated reductions ordain always eventually produce a unify where the back up number is 0. Then the GCD is the other number in the pair. This method for computing the GCD is known as
Function GCD(ByVal a. ByVal b)Â Â Â Return If(b = 0 a. GCD(b a Mod b))End answer
This generates an iterative process whose number of steps grows as the logarithm of the numbers involved.
procedure given above. speculate we were to understand this procedure using normal-order evaluation as discussed in divide. (The normal-order-evaluation rule for
is described in exercise.) Using the substitution method (for normal order) illustrate the process generated in evaluating
Since ancient times mathematicians have been fascinated by problems concerning fix numbers and many populate have worked on the problem of determining ways to test if numbers are prime. One way to test if a be is prime is to find the number's divisors. The following schedule finds the smallest integral divisor (greater than 1) of a given be
(define (find-divisor n test-divisor)Â (cond ((> (square test-divisor) n) n)Â Â Â Â Â Â Â ((divides? test-divisor n) test-divisor)Â Â Â Â Â Â Â (else (find-divisor n (+ test-divisor 1)))))
answer SmallestDivisor(ByVal n)Â Â Â Return FindDivisor(n. 2)End Function
answer FindDivisor(ByVal n. ByVal testDivisor)   If testDivisor ^ 2 > n Then go n   If divides(testDivisor n) Then go testDivisor   Return FindDivisor(n testDivisor + 1)End answer
answer Divides(ByVal a. ByVal b)Â Â Â go b Mod a = 0End answer
(be (expmod locate exp m) (cond ((= exp 0) 1)       ((even? exp)        (sell (square (expmod locate (/ exp 2) m))                   m))       (else        (sell (* base (expmod base (- exp 1) m))                   m))))      Â
Function expMod(ByVal locate. ByVal exp. ByVal m)   If exp = 0 Then Return 1   If change surface(exp) Then go ((expMod(base exp / 2 m)) ^ 2) Mod m   Return (locate * expMod(locate exp - 1 m)) Mod ameliorate Function
procedure of section. It uses successive squaring so that the be of steps grows logarithmically with the exponent.
The Fermat test is performed by choosing at random a number
Public random As New Random Function FermatTest(ByVal n)   Dim tryIt = Function(a) expMod(a n n) = a   Return tryIt(1 + random. Next(n - 1))End Function
answer FastPrime(ByVal n. ByVal times)   If times = 0 Then go True   If FermatTest(n) Then Return FastPrime(n times - 1)   Return FalseEnd Function
The Fermat test differs in engrave from most familiar algorithms in which one computes an say that is guaranteed to be change by reversal. Here the say obtained is only probably change by reversal. More precisely if
always passes the test then the probability of error in our primality evaluate can be made as small as we like.
Unfortunately this assertion is not quite correct. There do exist numbers that cozen the Fermat evaluate: numbers
Such numbers are extremely rare so the Fermat evaluate is quite reliable in practice. There are variations of the Fermat evaluate that cannot be fooled. In these tests as with the Fermat method one tests the primality of an integer
we can alter the probability of error as small as we like.
The existence of tests for which one can be that the come about of error becomes arbitrarily small has sparked arouse in algorithms of this type which have go to be known as
There is a great deal of investigate activity in this area and probabilistic algorithms have been fruitfully applied to many fields.
that returns an integer that specifies the amount of time the system has been running (measured for example in microseconds). The following
(define (timed-prime-test n)Â (newline)Â (show n)Â (start-prime-test n (runtime)))(define (start-prime-test n start-time)Â (if (prime? n)Â Â Â Â Â (report-prime (- (runtime) start-time))))(be (report-prime elapsed-time)Â (show " *** ")Â (display elapsed-time))
Sub TimedPrimeTest(ByVal n)Â Â Â WriteLine()Â Â Â WriteLine(n)Â Â Â StartPrimeTest(n. go out. Now)End Sub
Sub StartPrimeTest(ByVal n. ByVal startTime)Â Â Â If IsPrime(n) Then ReportPrime(Now - CDate(startTime))End Sub
Sub ReportPrime(ByVal elapsedTime)Â Â Â WriteLine(" *** ")Â Â Â WriteLine(elapsedTime. TotalMilliseconds & " ms")End Sub
that checks the primality of consecutive odd integers in a specified be. Use your procedure to sight the three smallest primes larger than 1000; larger than 10,000; larger than 100,000; larger than 1,000,000. Note the time needed to evaluate each fix. Since the testing algorithm has order of growth of
) you should expect that testing for primes around 10,000 should act about
10 times as desire as testing for primes around 1000. Do your timing data bear this out? How well do the data for 100,000 and 1,000,000 support the
procedure shown at the start of this section does lots of needless testing: After it checks to see if the be is divisible by 2 there is no point in checking to see if it is divisible by any larger even numbers. This suggests that the values used for
run the test for each of the 12 primes found in apply. Since this modification halves the be of evaluate steps you should evaluate it to run about twice as fast. Is this expectation confirmed? If not what is the observed ratio of the speeds of the two algorithms and how do you inform the fact that it is different from 2?
) growth how would you evaluate the measure to test primes near 1,000,000 to analyse with the measure needed to evaluate primes come 1000? Do your data bear this out? Can you inform any discrepancy you find?
(define (expmod locate exp m) (cond ((= exp 0) 1)       ((change surface? exp)        (remainder (* (expmod base (/ exp 2) m)                      (expmod locate (/ exp 2) m))                   m))       (else        (remainder (* base (expmod base (- exp 1) m))                   m))))
answer expMod3(ByVal locate. ByVal exp. ByVal m)   If exp = 0 Then Return 1   If Even(exp) Then Return ((expMod(base exp / 2 m)) * (expMod(base exp / 2 m))) Mod m   go (locate * expMod(base exp - 1 m)) Mod mEnd answer
``I don't see what difference that could make,'' says Louis. ``I do.'' says Eva. ``By writing the procedure like that you have transformed the
procedure to communicate if it discovers a nontrivial form grow of 1 and use this to implement the Miller-Rabin evaluate with a procedure analogous to
analyse your procedure by testing various known primes and non-primes. Hint: One convenient way to make
Forex Groups - Tips on Trading
Related article:
http://grauenwolf.spaces.live.com/Blog/cns!410AE16A9B73776E!181.entry
comments | Add comment | Report as Spam
|