Lecture 7 Notes

What is the run me ie how many iteraons for a given

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: t;best" quotient à༎ smallest error myPi = pBest/qBest; Lecture 8 4 Ques,on Time This is an example of a “nested loop” for every q M p’s are checked. What is the run ,me? (i.e. how many itera,ons for a given M?) A) M B) 2M C) M^2 D) M^M Improvements •  Inner loop floor(qπ) < qπ < ceil(qπ) => floor(qπ)/q < π < ceil(qπ)/q => need only check p- = floor(qπ), p+ = ceil(qπ) This also gives bound J༄ p- /q < π < p+/q 1 2/13/14 % Better BetterEg3_1! ! pBest = 1; % Best numerator! qBest = 1; % Best denominator ! err_pq = abs(pBest/qBest - pi); % Error qBest/pBest! ! % Check out all possible denominators...! for q = 1:M! % For this q, find the best numerator p! for p = floor(q*pi):ceil(q*pi)! % Check to see if we have a better quotient! if abs(pi - p/q) < err_pq! ! ! ! err_pq= abs(pi - p/q);! pBest= p;! qBest= q;! end! end!...
View Full Document

This document was uploaded on 03/11/2014 for the course CSCI 004 at Brown.

Ask a homework question - tutors are online