Download presentation
Presentation is loading. Please wait.
Published byLinda Johnson Modified over 9 years ago
1
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
2
Today’s Topics: 1. Algorithms 2. Division algorithms 2
3
1. Algorithms Every journey begins with one step 3
4
Algorithms! Multiply 17 x 142. While you do that, think, how can you tell a computer to do that.
5
What is an algorithm? A. A step by step process B. Any way of solving complex problems C. Computer code D. Math 5
6
An algorithm? Definition: a step-by-step process Each basic step is simple Together they can compute very complicated things What properties do we care about? A. It should have the correct format (“compile”) B. It should always terminate C. It should give the correct answer D. All of the above
7
Complexity of algorithms Simplistic model: each basic operation (addition, multiplication, etc) takes unit time Complexity of an algorithm = number of steps = running time Depends on the input (it takes longer to multiply large numbers)
8
Pseudocode Begin, End delimit list of steps. Allowed types of steps Assignment Repeat -loop If-Then-Else-End While -loop Walkthrough / Trace
9
2. Division algorithms 9
10
Notation and terms n DIV d: integer part of dividing n by d n MOD d: the remainder n = (n DIV d) * d + (n MOD d) Example: 7=2*3+1, so 7 DIV 3 = 2 7 MOD 3 = 1
11
Notation and terms We say… d divides n d is a factor of n n is a multiple of d d|n To mean n=a*d for some integer a.
12
Solve the equation For what integer x do we have x DIV 5 = 14 and x MOD 5 = 2 ? A. No integers satisfy these equations at the same time. B. Any multiple of 5 works. C. Any non-multiple of 5 works. D. Only x = 72 works. E. None of the above / more than one of the above.
13
Solve the equation For what integer x do we have x MOD 5 = 2 ? A. -3 (and others) B. -2 (and others) C. All and only the multiples of 5 D. All and only the non-multiples of 5 E. None of the above / more than one of the above.
14
Why do we need DIV,MOD? Round robin scheduling, secret sharing, parallel computation…
15
How to compute DIV,MOD? Write an algorithm that, given two integers n,d, computes n DIV d and n MOD d How fast is your algorithm? 15
16
A DIVMOD algorithm 16
17
A DIVMOD algorithm 17 Loop invariant: n=d*div+mod
18
A DIVMOD algorithm 18 How many steps does the algorithm perform (roughly)? A. n B. n+d C. n*d D. n/d E. Other
19
A faster DIVMOD algorithm? 19
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.