Download presentation

Presentation is loading. Please wait.

Published byRaven Causley Modified over 2 years ago

1
CS 1000 H. James de St. Germain Problem Solving Writing a Program to Approximate Pi (or why 3.141592653589793238462643383279502884197169399 is better than 3.141592653589793238462643383279)

2
CS 1000 Problem We need pi… The computer needs pi… –At least for geometric, mathematical, physics, mechanical, civil, etc. computations The computer doesn’t know pi –Okay, it does know pi, but someone had to tell it. –What if you were that someone?

3
CS 1000 Option 1: Don’t use a Symbolic Name, just Memorize the digits… 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899 86280348253421170679 8214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196 4428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273 7245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094 3305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912 9833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132 0005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235 4201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859 5024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303 5982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989 3809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913151 5574857242454150695950829533116861727855889075098381754637464939319255060400927701671139009848824012 8583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912 9331367702898915210475216205696602405803815019351125338243003558764024749647326391419927260426992279 6782354781636009341721641219924586315030286182974555706749838505494588586926995690927210797509302955 3211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000 8164706001614524919217321721477235014144197356854816136115735255213347574184946843852332390739414333 4547762416862518983569485562099219222184272550254256887671790494601653466804988627232791786085784383 8279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863 0674427862203919494504712371378696095636437191728746776465757396241389086583264599581339047802759009 9465764078951269468398352595709825822620522489407726719478268482601476990902640136394437455305068203 4962524517493996514314298091906592509372216964615157098583874105978859597729754989301617539284681382 6868386894277415599185592524595395943104997252468084598727364469584865383673622262609912460805124388 4390451244136549762780797715691435997700129616089441694868555848406353422072225828488648158456028506 0168427394522674676788952521385225499546667278239864565961163548862305774564980355936345681743241125 1507606947945109659609402522887971089314566913686722874894056010150330861792868092087476091782493858 9009714909675985261365549781893129784821682998948722658804857564014270477555132379641451523746234364 5428584447952658678210511413547357395231134271661021359695362314429524849371871101457654035902799344 0374200731057853906219838744780847848968332144571386875194350643021845319104848100537061468067491927 8191197939952061419663428754440643745123718192179998391015919561814675142691239748940907186494231961 5679452080951465502252316038819301420937621378559566389377870830390697920773467221825625996615014215 0306803844773454920260541466592520149744285073251866600213243408819071048633173464965145390579626856 1005508106658796998163574736384052571459102897064140110971206280439039759515677157700420337869936007 2305587631763594218731251471205329281918261861258673215791984148488291644706095752706957220917567116 7229109816909152801735067127485832228718352093539657251210835791513698820914442100675103346711031412 6711136990865851639831501970165151168517143765761835155650884909989859982387345528331635507647918535 8932261854896321329330898570642046752590709154814165498594616371802709819943099244889575712828905923 2332609729971208443357326548938239119325974636673058360414281388303203824903758985243744170291327656 1809377344403070746921120191302033038019762110110044929321516084244485963766983895228684783123552658 2131449576857262433441893039686426243410773226978028073189154411010446823252716201052652272111660396 6655730925471105578537634668206531098965269186205647693125705863566201855810072936065987648611791045 3348850346113657686753249441668039626579787718556084552965412665408530614344431858676975145661406800 7002378776591344017127494704205622305389945613140711270004078547332699390814546646458807972708266830

4
CS 1000 The current unofficial (as of October 4 2006) world record is 100,000 decimal places, and was set by a Japanese mental health counselor named Akira Haraguchi, who is currently 59 years of age… Perhaps not the best use of Time?

5
CS 1000 Why Approximate Pi Pi is: –Irrational (X/Y ~= pi) –Infinite and Varied Computers cannot represent –Irrational numbers –Infinite numbers AH HA! You said in the previous slides that the computer knows pi! You were WRONG! The computer must only know an approximation of pi! –Perhaps we can do better than the Matlab Programmers?

6
CS 1000 Your “Assignment” (or word problems are fun) The magical number pi (3.141927…) can be calculated by a sequence of subtractions and additions (e.g., 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + 4/13 - 4/15 + etc). Write a program to calculate pi. First ask the user how many steps she wants to take. Make sure the inputted number is positive. Print out the final value.

7
CS 1000 Solving the Pi Problem Your Objective: –Turn Problem Statement into Code Process –Separate out and Identify (in high level English) the Steps necessary to accomplish the goal! –Convert each step into Pseudocode –Convert Pseudocode into Code

8
CS 1000 Things to Remember Computer programs execute ONE statement at a time and thus you have to think in a series of small steps while programming. While DESIGNING you should think in high level concepts, such as: –Get input –Calculate values (or use a loop to calc values) –Plot data Design First!

9
CS 1000 Programming Problem Calculate Pi Program This is DAUNTING! This is TOO Much! Must Not Panic. Must Not Panic! Lets break this into smaller easier steps!

10
CS 1000 Identify High Level Goals First re-read the problem statement: The magical number pi (3.141927…) can be calculated by the following sequence of subtractions and additions.: 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + 4/13 - 4/15 + etc Write a program to calculate pi. First ask the user how many steps she wants to take. Make sure the inputted number is positive. Print out the final value.

11
CS 1000 Break Down the Problem First lets break this down: –“Write a program to calculate pi. First ask the user how many steps she wants to take. Make sure the number is positive. Print out the value.” This becomes 1.The user must input how many steps in the calculation. –This “number of steps” must be positive. 2.The program calculates an approximation to pi. –We have a formula for this 3.Print out value of pi.

12
CS 1000 Program Decomposition Graph Calculate Pi Program Ask user for non negative number of steps Use formula to calculate pi Output the answer Much Better!

13
CS 1000 Or Flow Chart End Start Get Sequence Length Calculate Pi Print Output

14
CS 1000 Next: Analyze the Formula We are calculating the value of pi. Don’t panic, we are given the formula. – 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + 4/13 - 4/15 + etc –Step 1 2 3 4 5 6...

15
CS 1000 Oh my! It’s Math! Many people experience what I call the, “Oh my, its math!” phenomenon. Try not to let the idea that you are doing “Math” scare you into not thinking about the problem. The computer is going to do the work for you. Your job is to tell the computer how

16
CS 1000 So, first: Solve the Problem by Hand… … for a few simple examples: –If the user inputs the number zero, you ask again. –If the user inputs 1 you would print out that pie is 4 –If the user inputs 2 you would print out that pie is 2.6666 (4 – 4/3) –If the user inputs 3 you would print out that pie is 3.4666 (4 – 4/3 + 4/5) –etc, etc, etc Formula: 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + 4/13 - 4/15 + etc Step: 1 2 3 4 5 6 …

17
CS 1000 Looking for Patterns!

18
CS 1000 Write a For loop! for i = 1 : 1000 % for index = start:cnt:end –fprintf(‘%d ‘, i); end fprintf(‘\n’);

19
CS 1000 Write a While loop! value = 1 while ( value < 1000 ) % while ( true ) –fprintf(‘%d ‘, value); –value = value + 1; end fprintf(‘\n’);

20
CS 1000 Write a For Loop! for i=2:2:1000 –fprintf(‘%d ‘, i); end fprintf(‘\n’);

21
CS 1000 Write a While Loop! value = 2; while (value < 10000) –fprintf(‘%d ‘, value); –value = value + 2; end fprintf(‘\n’);

22
CS 1000 Write a Program. Use a While loop! current_digit = 1; while ( true ) –fprintf(‘%d ‘, current_digit); –if (current_digit == 1) current_digit = 0; –else current_digit = 1; –end end

23
CS 1000 Write a program. Use a For loop! for i = 1:10000 –if ( rem ( i, 2) == 0) fprintf(‘1 ‘); –else fprintf(‘0 ‘); –end end

24
CS 1000 Write a Loop Can you figure this one out?

25
CS 1000 Back to Pi: Looking for Patterns! The formula is: 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + 4/13 – … What patterns do you see? –Write them down on your notes:

26
CS 1000 Does re-writing the formula help? Lets rewrite this on its side, step by step: 4 -4 / 3 +4 / 5 -4 / 7 +4 / 9 -4 / 11 …etc …

27
CS 1000 Making the pattern consistent. Lets make the pattern consistent: +4 / 1 -4 / 3 +4 / 5 -4 / 7 +4 / 9 -4 / 11 …etc …

28
CS 1000 Seek the Pattern What do we divide by? STEPDivision 14 / 1 24 / 3 34 / 5 44 / 7 -------------------------------- i4 / ? Symbolic of all possible values ? = some math with i Which is often written: ? = f(i) % some function of i

29
CS 1000 stepdivisor 11 23 35 47 59 611 713 Write down a math equation to compute the “divisor” value from the “step” variable! divisor = 2 * step – 1 or ( ( 2 * i ) – 1) Seek the Pattern Compute the Divisor?

30
CS 1000 Seek the Pattern When do we Add? When do we Subtract? STEPAdd/Sub 1 + 2 - 3 + 4 - --------------------------------------------------------------------------- i ? Write your Pseudocode in your notes:

31
CS 1000 When do we Add? When do we Subtract? What is the pattern? If step number “i” is odd we add If step number “i” is even we subtract In Matlab: if ( odd(i) ) add the new value else subtract the new value end

32
CS 1000 Thus we have the following patterns: STEPAdd/Sub Value 1+4 / 1 2-4 / 3 3+4 / 5 4-4 / 7 --------------------------------------------------------------- iadd when i is odd4 / (2*i-1)

33
CS 1000 Time to Start Programming We know we are supposed to: 1.Query the user for how many iterations (how many steps, how big (i) gets) in our loop – If the number is not positive repeat step 1 2.Calculate the “current” approximation to pi using our formula 3.Output answer Calculate Pi Program Ask user for non negative number of steps Use formula to calculate pi Output the answer

34
CS 1000 Breaking the steps down We know we are supposed to: 1.Query the user for how many iterations (how many steps) in our loop number_of_steps = input(‘How many steps: ‘); 2.If the number is not positive repeat step 1 How do we repeat an unknown number of times? WHILE LOOP 3.Calculate the value of pi using our formula What are we doing? Repeating something a pre-defined number of times. What programming element do we use? FOR LOOP 4.Ouput the value

35
CS 1000 Getting the number of steps from the user We can combine our ideas into the following code: number_of_steps = input(‘How many steps: ‘); while (number_of_steps < 1) number_of_steps = input(‘How many steps: ‘); end –This is a DESIGN PATTERN that you will see many times in your programming career.

36
CS 1000 While Loop Design Pattern Ask for data While data is incorrect –Re-ask for data End while

37
CS 1000 Now for the calculation If we are calculating something, we had better have a variable for it: our_pi = 0; Note 1: Even though we are using the variable name our_pi, what we really mean is: “our approximation to the value of pi over the previous X steps” (but that would be too long a variable name) Note 2: we initialize the value of our variable to 0 to represent that no work has yet been done.

38
CS 1000 Now for the calculation our_pi = 0; % (our pi value starts at 0). Here is our formula ( at STEP=i, compute 4 / ( i*2 -1 ), and add to our_pi ) –our_pi = our_pi + ( 4 / ( (i * 2) – 1 ) ; remember: sign alternates but initially lets just use ‘+’ Thus we need a Loop for i = 1 to number_of_steps do calculation end

39
CS 1000 Calculation continued: Lets combine our steps: –Remember Initialize variable Write loop Insert formula into loop our_pi = 0; % (our pi value starts at 0). for i = 1 : number_of_steps our_pi = our_pi + ( 4 / ( (i * 2) – 1 ) ); end % read this to be, “Our approximation of Pi becomes equal to the old approximation of Pi plus (or minus) the new computation.”

40
CS 1000 Sign Alternates! We need to take into account the fact that the sign alternates. There are many options: 1.Mathish expression –add if odd ( i ) 2.Another Math expression –sign = ( -1 ^ (i+1) ); 3.A boolean flag (called a toggle): sign_is_positive = true; % then change to false. sign_is_positive = ~sign_is_positive; % inside loop 4.Others are possible

41
CS 1000 Alternating Sign using odd even if ( odd(i) ) our_pi = our_pi + % PLUS formula else % i must be even our_pi = our_pi - % MINUS formula end

42
CS 1000 Alternating Sign using a Boolean if (time_for_a_negative) our_pi = our_pi - % MINUS formula time_for_a_negative = ~time_for_a_negative; else % only alternative is time for a positive our_pi = our_pi + % PLUS formula time_for_a_negative = ~time_for_a_negative; end

43
CS 1000 Cleaning up Duplicate Code if (time_for_a_negative) our_pi = out_pi - % MINUS formula else % only alternative is time for a positive our_pi = our_pi + % PLUS formula end time_for_a_negative = ~time_for_a_negative;

44
CS 1000 Alternating Sign using Math our_pi = … our_pi + (formula * -1^(i+1)); Advantage: No IF statement Disadvantage: Perhaps Compute Intensive

45
CS 1000 Insert alternating sign code Old code: our_pi = 0; % (our pi value starts at 0). for i = 1 : number_of_steps our_pi = our_pi ? ( 4 / ( (i * 2) – 1 ) ); end New code using “boolean flag” to alternate add/sub our_pi = 0; % (our pi value starts at 0). sign_is_positive = true; for i = 1 : number_of_steps if (sign_is_positive == true) our_pi = our_pi + ( 4 / ( (i * 2) – 1 ) ); else our_pi = our_pi - ( 4 / ( (i * 2) – 1 ) ); end sign_is_positive = ~sign_is_positive; % must switch each time end

46
CS 1000 Finally: Print out the Answer This step is easy! (I hope!) fprintf(‘the value of pi over %d iterations is approximated as %f\n’, number_of_steps, our_pi); %d – print integer number (whole number) %f – print float number (with decimal point) \n – print a newline

47
CS 1000 Summary What we did: –Analyzed program from the TOP DOWN. –Found major components of program. –For each major component, wrote pseudocode –For each section of pseudocode we wrote actual code

48
CS 1000 What does the final program look like? % % Program by : H. James de St. Germain % This program calculates the value of pi, using the formula % pi = 4 – 4/3 + 4/5 – 4/7 + 4/9 +... number_of_steps = input('How many steps: '); while (number_of_steps < 1) number_of_steps = input('How many steps: '); end our_pi = 0; sign_is_positive = true; for i = 1 : number_of_steps if ( sign_is_positive == true ) our_pi = our_pi + ( 4 / ( ( i * 2 ) - 1 ) ); else our_pi = our_pi - ( 4 / ( ( i * 2 ) - 1 ) ); end sign_is_positive = ~sign_is_positive; end fprintf('the value of pi over %d iterations is %f\n', number_of_steps, our_pi);

49
CS 1000 Lets Convert to C

50
CS 1000 Questions?

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google