Page - 48 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 48 -
Text of the Page - 48 -
48 2 BasicConstructions
Exercise2.11:Compute
Up through history, greatminds have developed different computational schemes
for thenumber .Wewillhereconsider twosuchschemes,onebyLeibniz(1646
1716), andonebyEuler (1707 1783).
TheschemebyLeibnizmaybewritten
D8 1X
kD0 1
.4kC1/.4kC3/;
whileone formof theEuler schememayappearas
D vu
u
t6 1X
kD1 1
k2 :
If only the firstN terms of each sum are used as an approximation to , each
modifiedschemewill havecomputed with someerror.
Writeaprogramthat takesN as input fromtheuser, andplots theerrordevelop-
mentwith both schemes as thenumber of iterations approachesN . Your program
shouldalsoprintout thefinalerrorachievedwithbothschemes, i.e.when thenum-
ber of terms is N. Run the programwithN D 100 and explain brieflywhat the
graphsshow.
Filename:compute_pi.py.
Exercise2.12:Computecombinationsof sets
ConsideranIDnumberconsistingof two lettersand threedigits, e.g.,RE198.How
many different numbers canwe have, and how can a program generate all these
combinations?
Ifacollectionofn thingscanhavem1variationsof thefirst thing,m2of thesec-
ondandsoon, the total numberofvariationsof the collectionequalsm1m2 mn.
Inparticular, theIDnumberexemplifiedabovecanhave26 26 10 10 10D676;000
variations. To generate all the combinations, wemust have five nested for loops.
Thefirst two runoverall lettersA,B, andsoon toZ,while thenext three runover
all digits0;1;:: :;9.
To convinceyourself about this result, start outwith an IDnumberon the form
A3where thefirst part canvaryamongA,B, andC, and thedigit canbeamong1,
2, or 3. Wemust startwithAand combine itwith 1, 2, and3, then continuewith
B,combinedwith1,2, and3, andfinallycombineCwith1,2, and3.Adouble for
loopdoes thework.
a) Inadeckofcards, eachcard is acombinationofa rankandasuit. Thereare13
ranks: ace (A), 2, 3, 4, 5, 6, 7, 8, 9, 10, jack (J), queen (Q), king (K), and four
suits: clubs (C), diamonds (D), hearts (H), and spades (S).A typical cardmay
beD3.Write statements thatgenerateadeckofcards, i.e., all thecombinations
CA,C2,C3, and soon toSK.
b) Avehicle registrationnumber ison the formDE562,where the lettersvaryfrom
AtoZandthedigits from0to9.Writestatements thatcomputeall thepossible
registrationnumbersandstores themina list.
back to the
book Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations β Python
A Gentle Introduction to Numerical Simulations with Python
- Title
- Programming for Computations β Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2016
- Language
- English
- License
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 248
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik