Decision Maths - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Decision Maths

Description:

... chart that would represent an algorithm for making a cup of tea for your teacher? ... Fill mug to within 1 inch of the top. Wiltshire. Tea Algorithm ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 20
Provided by: bengr1
Category:
Tags: decision | maths

less

Transcript and Presenter's Notes

Title: Decision Maths


1
Decision Maths
  • Lesson 10 Algorithms

2
What is an algorithm?
  • An algorithm is sequence of instructions that
    will solve a particular problem.
  • There can be no ambiguity in the instructions.
  • There must be a clause that can terminate the
    algorithm.
  • Algorithms can be shown in a number of ways
  • A list of instructions
  • Pseudo code (computer language)
  • Flow charts
  • You will have been using Algorithms for years
    without even realising it. Addition, Subtraction,
    Long multiplication and division.

3
Zellers Algorithm
  • Do you know which day you were born on?
  • Mondays child is fair of face,
  • Tuesdays child is full of grace,
  • Wednesdays child is full of woe,
  • Thursdays child has far to go,
  • Fridays child is loving and giving,
  • Saturdays child works hard for a living,
  • And a child that is born on the Sabbath day,
  • Is fair and wise and good and gay.

4
Zellers Algorithm
  • Let day number D
  • Month number M
  • And year Y
  • If M is 1 or 2 replace M with M add 12 and
    replace Y with Y subtract 1.
  • Let C be the first two digits of Y and Y be the
    last to digits of Y.
  • Add together the integer parts of (2.6M - 5.39),
    (Y/4), (C/4).
  • Now add on D and Y and subtract 2C.
  • Find the remainder when the answer is divided by
    7.
  • 0 Sunday
  • 1 Monday
  • etc
  • D 06
  • M 02
  • Y 1977
  • D 06
  • M 14
  • Y 1976
  • C 19 Y 76
  • (2.6 x 14 5.39 31)
  • 76/4 19
  • 19/4 4
  • 31 19 4 54
  • 54 6 76 2 x 19 98
  • 98/7 14 r 0
  • Sunday
  • Is fair and wise and good and gay.

Now try and see what day you were born on.
5
Russian Peasants Algorithm
  • Step 1 Write down the two numbers to be
    multiplied.
  • Step 2 Beneath the left number write down
    double that number. Beneath the right number
    write down half that number
  • Step 3 Repeat step 2 until the right number is
    1.
  • Step 4 Delete those rows where the number in
    the right column is even.
  • Step 5 Add up the remaining numbers in the left
    column.
  • So 24 x 163 3912
  • 24 163
  • 48 81
  • 96 40
  • 192 20
  • 384 10
  • 768 5
  • 1536 2
  • 3072 1
  • 3912

6
Russian Peasants Algorithm
  • The order of the sum is not important.

163 24 326 12 652 6 1304 3 2608 1
3912
7
Russian Peasants Algorithm
  • Now try these ones
  • 76 x 53
  • 76 53
  • 152 26
  • 304 13
  • 608 6
  • 1216 3
  • 2432 1
  • 4028
  • 28 x 405
  • 28 405
  • 56 202
  • 112 101
  • 224 50
  • 448 25
  • 896 12
  • 1792 6
  • 3584 3
  • 7168 1
  • 11340

8
Flow Charts
  • A diamond indicates a question or choice to be
    made.
  • A rectangle is an instruction box, it has one
    entrance and one exit.
  • A circle (oval) indicates a start or stop.

9
Flow diagrams
  • Below is an algorithm expressed in psuedo code.
  • Step 1 - Let M b2 4ac.
  • Step 2 - If M lt 0 print no real solutions and
    go to step 5.
  • Step 3 - Let x1 (-b vM)/(2a)
  • Let x2 (-b vM)/(2a)
  • Step 4 - Print x1 and x2.
  • Step 5 - Stop
  • What does this algorithm solve?
  • Can you design a flow diagram to solve quadratic
    equations?

10
Solving Quadratic Equations
Start
Read a, b, c
Let M b2 4ac
Yes
Is M lt 0
No
Let x1 (-b vM)/(2a)
Print no real Solutions
Let x2 (-b vM)/(2a)
Print x1, x2
Stop
11
Euclids Method for finding HCF
  • Pick two numbers and put them through this
    algorithm.
  • What does the algorithm solve?

Read x, y
Subtract y from x to get new value of x
x gt y ?
Yes
No
Subtract x from y to get new value of y
x lt y ?
Yes
No
x y ?
No
Yes
Output x
12
Homework
  • Can you draw a flow chart that would represent an
    algorithm for making a cup of tea for your
    teacher?

Start
Boil the kettle
Place tea bag in teachers favourite mug
Fill mug to within 1 inch of the top
Add sachet of milk
Stir 4 times
Stop
13
Tea Algorithm
  • The example we have just looked at was a very
    simple (boring) algorithm.
  • It was specific to one person and would apply to
    anyone else.
  • Try to make your algorithm complex.
  • Allow for more freedom and make a general
    algorithm that would suit all.
  • Remember, there can be no ambiguity.

14
Complexity
  • There is often more than one way of solving a
    problem, and this can result in different
    algorithms.
  • It is obvious that some algorithms will be more
    efficient than others.
  • The more efficient, the fewer operations used to
    solve the problem.
  • This can be useful in computing, as more
    efficient programs will use up less storage.
  • Take the example of the Russian peasants
    algorithm (28 x 405) the work involved is more
    efficient is you switch the sum around to 405 x
    28.

15
Complexity
  • To understand complexity we are going to look at
    two examples that involve quadratic equations.
  • Lets first consider the quadratic equation.
  • In the algorithm we looked at earlier we
    calculated
  • M b2 4ac once and then calculated
  • x1 (-b vM)/(2a), and
  • x2 (-b vM)/(2a)
  • If you calculate the answers from the original
    equation then you can see that b2 4ac will be
    evaluated twice and hence is less efficient.

16
Complexity
  • Lets now consider the evaluation of the quadratic
    expression f(a) 3a2 2a 1
  • f(4) 3 x 4 x 4 2 x 4 1 57
  • There are 3 multiplications and 2 additions.
  • We could however re-write f(a) in the form
  • f(a) (3a 2)a 1
  • Now f(4) (3 x 4 2) x 4 1 57
  • Here there are 2 multiplications and 2 additions.

17
Complexity
  • Now let us extend this further to cubic
    polynomials.
  • f(a) 4a3 3a2 2a 1
  • f(4) 4 x 4 x 4 x 4 3 x 4 x 4 2 x 4 1
    313
  • This has 6 multiplications and 3 additions
  • Again we can re-arrange f(a)
  • f(a) 4a3 3a2 2a 1
  • (4a2 3a1 2)a 1
  • ((4a 3)a 2)a 1
  • Now ((4 x 4 3) x 4 2) x 4 1 313
  • This has 3 multiplications and 3 additions.
  • Hopefully you can see that the second way is more
    efficient.

18
Complexity
  • Lets generalise this for any polynomial
  • anan an-1an-1 a1a a0
  • We can use our previous results to help, and make
    a prediction for a polynomial with order 4.
  • You can see from the table that the nested method
    for evaluating the quadratic is the most
    efficient.

19
Complexity
  • Because there is an n2 term in the number of
    multiplications required using the first
    approach, the complexity of that algorithm is
    quadratic.
  • Note To appreciate the effect of this think
    about a polynomial with order 100.
  • Using the straight forward method you would need
    to do (100/2)(100 1) 5050 multiplications and
    100 additions.
  • Using nesting you would only need to do 100
    multiplications and 100 additions.
Write a Comment
User Comments (0)
About PowerShow.com