Sie sind auf Seite 1von 20

Decision Maths

Algorithms

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.

Zeller`s 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.

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.

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
48
96
192
384
768
1536
3072
3912

163
81
40
20
10
5
2
1

Russian Peasants Algorithm


The order of the sum is not important.

163
326
652
1304
2608
3912

24
12
6
3
1

Russian Peasants Algorithm


Now try these ones
76 x 53

76
152
304
608
1216
2432
4028

53
26
13
6
3
1

28 x 405

28 405
56 202
112 101
224 50
448 25
896 12
1792
3584

7168
11340

6
3

Decision Maths
Algorithms: Flow charts

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.

Flow diagrams
Below is an algorithm expressed in psuedo code.
Step 1 - Let M = b2 4ac.
Step 2 - If M < 0 print no real solutions and go
to step 5.
Step 3 - Let x1 = (-b + M)/(2a)

Let x2 = (-b M)/(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?

Solving Quadratic Equations


Start
Read a, b, c
Let M = b2 4ac
Is M < 0
No

Yes

Let x1 = (-b + M)/(2a)


Let x2 = (-b M)/(2a)
Print x1, x2
Stop

Print
no real Solutions

Euclids Method for finding HCF


Read x, y

x>y?

Yes

Subtract y from
x to get new
value of x

Yes

Subtract x from
y to get new
value of y

No
x<y?
No
No

x=y?
Yes
Output x

Pick two numbers and put

them through this algorithm.


What does the algorithm
solve?

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

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.

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.

Complexity
To understand complexity we are going to look at two examples

that involve quadratic equations.


Lets first consider the quadratic equation.
b b 2 4ac
x
2a
In the algorithm we looked at earlier we calculated

M = b2 4ac once and then calculated


x1 = (-b + M)/(2a), and
x2 = (-b M)/(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.
b b 2 4ac
x1
2a

b b 2 4ac
x2
2a

Complexity
Lets now consider the evaluation of the

quadratic expression f() = 32 + 2 + 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() in the form
f() = (3 + 2) + 1
Now f(4) = (3 x 4 + 2) x 4 + 1 = 57
Here there are 2 multiplications and 2
additions.

Complexity
Now let us extend this further to cubic polynomials.
f() = 43 + 32 + 2 + 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()
f() = 43 + 32 + 2 + 1
= (42 + 31 + 2) + 1
= ((4 + 3) + 2) + 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.

Complexity
Lets generalise this for any polynomial
ann + an-1n-1 + + a1 + a0
We can use our previous results to help, and make a

prediction for a polynomial with order 4.

Degree of
polynomial

Method 1
(Straight forward)

Method 2
(Nested)

Multiplications

Additions

Multiplications

Additions

10

(n/2)(n+1)

You can see from the table that the nested method for

evaluating the quadratic is the most efficient.

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.

Das könnte Ihnen auch gefallen