Long Division and Algorithms, Slides of Computer Science

Various algorithms and code snippets related to long division and finding the greatest common divisor. Topics covered include quotient calculation, finding the maximum number in a sequence, and using the sieve of eratosthenes to find prime numbers.

Typology: Slides

2012/2013

Uploaded on 03/22/2013

dhirendra
dhirendra 🇮🇳

4.3

(78)

268 documents

1 / 81

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithms and Software
Chapter 7
Some of Chapter 8
Sections 17.2, 17.3
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51

Partial preview of the text

Download Long Division and Algorithms and more Slides Computer Science in PDF only on Docsity!

Algorithms and Software

Chapter 7

Some of Chapter 8

Sections 17.2, 17.

What is an Algorithm?

An algorithm for solving a problem is:

  • an unambiguous set of instructions
  • that can be executed in a finite amount of time and
  • that, when finished, has solved the problem.

Long Division

Long Division

quotient(n, m, desired digits):

place decimal point in correct place in answer

while n != 0 and number of iterations < desired digits:

# find the shortest prefix of n into which m goes.

# divide m into it.

write the result as the next digit of answer

n = remainder concatenated with remaining part of m

return(answer)

n

m

Preconditions

When we specify a problem, we specify conditions that the

programmer may assume hold at the time that the program

begins.

Preconditions for our division program:

quotient(n, m):
while n != 0:
divide m into the shortest prefix of n into which it goes:
for i in range(10):
if (i+1)*m > first chunk:
i is the result break
write the result as the next digit of answer
n = remainder
return(answer)

n

m

Swapping Two Values

35

12

An algorithm to swap the values:

X:

Y:

Swapping Two Values

35

12

An algorithm to swap the values:

X:

Y:

z = x

x = y

y = z

Z:

Divide and Conquer

def chess(board): while game_on: internal_board = scan(board) move = choose(internal_board) play(move, board)

Recall the idea: decompose into

pieces that make sense.

Rolling Dice

What is the frequency of

each total?

Rolling Dice

How can we fill in the table

with the totals?

Rolling Dice

What is the frequency of

each total?

Rolling Dice

freqs = [0] * 13

for i in range(6): for j in range(6): tot = dice[i][j] freqs[tot] += 1

Rolling Dice

Do we believe this answer?

  • Count the totals.

Rolling Dice

Do we believe this answer?

  • Count the totals.
  • Simulate throwing a lot of dice.