Documente Academic
Documente Profesional
Documente Cultură
Zeph Grunschlag
Agenda
Euclidean Algorithm for GCD
Number Systems
Arithmetic Algorithms
Addition
Multiplication
Subtraction 1s and 2s complement
L11
Euclidean Algorithm
m , ngcd(m,n)
Euclidean
Algorithm
L11
Euclidean Algorithm.
Example
gcd(33,77):
Step
r = x mod y
33
77
L11
Euclidean Algorithm.
Example
gcd(33,77):
Step
r = x mod y
33
77
33 mod 77
= 33
77
33
L11
Euclidean Algorithm.
Example
gcd(33,77):
Step
r = x mod y
33
77
77
33
33
11
1
2
L11
33 mod 77
= 33
77 mod 33
= 11
Euclidean Algorithm.
Example
gcd(33,77):
Step
r = x mod y
33
77
77
33
33
11
11
1
2
3
L11
33 mod 77
= 33
77 mod 33
= 11
33 mod 11
=0
Euclidean Algorithm.
Example
gcd(244,117):
Step
r = x mod y
244
117
L11
Euclidean Algorithm.
Example
gcd(244,117):
Step
0
r = x mod y
x
244
y
117
117
10
L11
Euclidean Algorithm.
Example
gcd(244,117):
Step
0
r = x mod y
1
2
L11
x
244
y
117
117
10
10
10
Euclidean Algorithm.
Example
gcd(244,117):
Step
0
r = x mod y
1
2
3
L11
x
244
y
117
117
10
10
7
7
3
11
Euclidean Algorithm.
Example
gcd(244,117):
Step
0
r = x mod y
1
2
3
4
L11
x
244
y
117
117
10
10
7
3
7
3
1
12
Euclidean Algorithm.
Example
gcd(244,117):
Step
0
r = x mod y
x
244
y
117
13
Euclidean Algorithm
Correctness
The reason that Euclidean algorithm
works is gcd(x,y ) is not changed
from line to line. If x, y denote the
next values of x , y then:
gcd(x,y) = gcd(y, x mod y)
= gcd(y, x + qy)
(the useful fact)
= gcd(y, x )
(subtract y -multiple)
= gcd(x,y)
L11
14
Euclidean Algorithm
Running Time
EX: Compute the asymptotic
running time of the Euclidean
algorithm in terms of the number
of mod operations:
L11
15
Euclidean Algorithm
Running Time
Assuming mod operation is O (1):
integer euclid(m,
n)
x = m, y = n
while( y > 0)
r = x mod y
x=y
y=r
return x
O (1) +
? ( O (1) +
O (1)
+ O (1)
+ O (1) )
+ O (1)
= ? O(1)
16
Euclidean Algorithm
Running Time
Facts: (x = next value of x, etc. )
1. x can only be less than y at very
beginning of algorithm
once x > y, x = y > y = x mod y
2. When x > y, two iterations of while loop
guarantee that new x is < original x
because x = y = x mod y. Two cases:
I.
II.
L11
17
Euclidean Algorithm
Running Time
(1&2) After first iteration, size of x
decreases by factor > 2 every
two iterations.
I.e. after 2m+1 iterations,
x < original_x / 2m
Q: When in terms of m does this
process terminate?
L11
18
Euclidean Algorithm
Running Time
After 2m+1 steps, x < original_x / 2m
A: While-loop exits when y is 0, which is
right before would have gotten x =
0. Exiting while-loop happens when
2m > original_x, so definitely by:
m = log2 ( original_x )
Therefore running time of algorithm is:
O(2m+1) = O(m) = O (log2 (max(a,b)) )
L11
19
Euclidean Algorithm
Running Time
Measuring input size in terms of n = number
of digits of max(a,b):
n = (log10 (max(a,b)) ) = (log2 (max(a,b)) )
Therefore running time of algorithm is:
O(log2 (max(a,b)) ) = O(n)
(assumed naively that mod is an O(1)
operation, so estimate only holds for
fixed-size integers such as ints and
longs)
L11
20
Number Systems
Q: What does the string of symbols
2134
really mean as a number and
why?
L11
21
Number Systems
A: 2 thousands 1 hundreds 3 tens
and 4
= 2 103 + 1 102 + 3 101 + 4
100
But on the planet Ogg, the
intelligent life forms have only one
arm with 5 fingers.
L11
22
Number Systems
So on Ogg, numbers are counted
base 5. I.e. on Ogg 2134 means:
2 53 + 1 52 + 3 5 1 + 4 50
To distinguish between these
systems, subscripts are used:
(2134)10 for Earth
(2134)5 for Ogg
L11
23
Number Systems
DEF: A base b number is a string of
symbols
u = ak ak-1 ak-2 a2 a1 a0
With the ai in {0,1,2,3,,b-2,b-1}.
The string u represents the number
(u )b = ak bk + ak-1 bk-1 + . . . + a1 b + a0
NOTE: When b > 10, run out of
decimal number symbols so after 7,
8, 9 use capital letters, starting from
A, B, C,
L11
24
Number Systems
EG: base-2 (binary) 101, 00010
base-8 (octal ) 74, 0472
base-16 (hexadecimal ) 12F,
ABCD
Q: Compute the base 10 version of
these.
L11
25
Number Systems
A: base-2 (binary) 101, 00010
(101)2 = 1 22 + 0 21 + 1 20 = 5
(00010)2 = 0(24+23+22+20) + 121 = 2
base-8 (octal ) 74, 0472
(74)8 = 7 81 + 4 80 = 60
(0472)8 = 4 82 + 7 81 + 2 80 = 314
base-16 (hexadecimal ) 12F, ABCD
(12F)16 = 1162+2161+15160 = 303
(ABCD)16=10163+11162+12161+13160
= 43981
L11
26
Number Systems
Binary most natural system for bitstrings and hexadecimal
compactifies byte-strings (1 byte = 2
hexadecimals)
EG in HTML:
<font color="ff00ff"> Nice Color
</font>
Q: What color will this become?
L11
27
Number Systems
A: "f00f" represents the rgb value:
The first byte is for redness, the second
byte is for green-ness, and the last for
blue-ness. The HTML above specifies
that 1516 + 15 = 255 redness and
blueness values, but 016 + 0 = 0
green-ness. Red and blue give purple,
and 255 is the top brightness so this is
bright purple.
L11
28
Number Systems
Reverse Conversion
Convert arbitrary decimal numbers
into various bases, (calculatorfunctions typically limited to base-2,
8, 16 and 10).
EG: Back at Ogg. Convert 646 to
base-5. Try to do all operations base5.
L11
29
Number Systems
Reverse Conversion
Back at Ogg. Convert 646 to base-5. Try
to do all operations as an Oggian (base5):
(646)10 = (6)10(10)102 + (4)10(10)10+ (6)10
Each quantity easy to convert into base-5:
(6)10 =(11)5 since 6 = 5 + 1
(4)10 =(4)5 since 4 < 5
(10)10 =(20)5
since 10 = 25 + 0
So convert whole expression and do
Oggian arithmetic:
L11
30
Number Systems
Reverse Conversion
Back at Ogg. Convert 646 to base-5.
Try to do all operations as an Oggian
(base-5):
(646)10 = (11)5(20)52 + (4)5(20)5+ (11)5
=
L11
31
Number Systems
Reverse Conversion
Back at Ogg. Convert 646 to base-5.
Try to do all operations as an Oggian
(base-5):
(646)10 = (11)5(20)52 + (4)5(20)5+ (11)5
= (11)5(400)5 + (130)5+ (11)5
=
L11
32
Number Systems
Reverse Conversion
Back at Ogg. Convert 646 to base-5.
Try to do all operations as an Oggian
(base-5):
(646)10 = (11)5(20)52 + (4)5(20)5+ (11)5
= (11)5(400)5 + (130)5+ (11)5
= (4400)5 + (141)5
=
L11
33
Number Systems
Reverse Conversion
Back at Ogg. Convert 646 to base-5. Try
to do all operations as an Oggian
(base-5):
(646)10 = (11)5(20)52 + (4)5(20)5+ (11)5
= (11)5(400)5 + (130)5+ (11)5
= (4400)5 + (141)5
= (10041)5
Thinking like an Oggian hurts brain too
much
L11
34
Number Systems
Reverse Conversion
Given an integer n and a base b find the
string u such that (u )b = n.
Pseudocode:
string represent(pos. integer n, pos. integer b)
q = n, i = 0
while( q > 0 )
ui = q mod b
q = q/b
i = i +1
return ui ui-1 ui-2 u2 u1 u0
L11
35
Number Systems
Reverse Conversion
EG: Convert 646 to Oggian (base-5):
i
ui = q mod b
q = q/b
646
L11
36
Number Systems
Reverse Conversion
EG: Convert 646 to Oggian (base-5):
i
ui = q mod b
q = q/b
646
646 mod 5 = 1
646/5 =129
L11
37
Number Systems
Reverse Conversion
EG: Convert 646 to Oggian (base-5):
i
ui = q mod b
q = q/b
0
1
646
646 mod 5 = 1
129 mod 5 = 4
646/5 =129
129/5 =25
L11
38
Number Systems
Reverse Conversion
EG: Convert 646 to Oggian (base-5):
i
ui = q mod b
q = q/b
0
1
2
646
646 mod 5 = 1
129 mod 5 = 4
25 mod 5 = 0
646/5 =129
129/5 =25
25/5 =5
L11
39
Number Systems
Reverse Conversion
EG: Convert 646 to Oggian (base-5):
i
ui = q mod b
q = q/b
0
1
2
3
646
646 mod 5 = 1
129 mod 5 = 4
25 mod 5 = 0
5 mod 5 = 0
646/5 =129
129/5 =25
25/5 =5
5/5 = 1
L11
40
Number Systems
Reverse Conversion
EG: Convert 646 to Oggian (base-5):
i
ui = q mod b
q = q/b
0
1
2
3
4
646
646 mod 5 = 1
129 mod 5 = 4
25 mod 5 = 0
5 mod 5 = 0
1 mod 5 = 1
646/5 =129
129/5 =25
25/5 =5
5/5 = 1
1/5 =0
41
Number Systems
In-Class Exercise
Some number-theory facts are basedependent. For example First-Grade
Teachers Rule:
A base-10 number is divisible by 3 iff the
sum of its digits are. Formally, let
n = (uk uk-1 uk-2 u2ku1 u 0)10. Then:
n mod 3
u
i 0
mod 3
42
Arithmetical Algorithms
Addition
Numbers are added from least
significant digit to most, while
carrying any overflow resulting
from adding a column:
base-10
base-16
Carry:
x
+y
L11
9
44
Arithmetical Algorithms
Addition
Numbers are added from least
significant digit to most, while
carrying any overflow resulting
from adding a column:
base-10
1
Carry:
x
+y
base-16
L11
9
9
45
Arithmetical Algorithms
Addition
Numbers are added from least
significant digit to most, while
carrying any overflow resulting
from adding a column:
base-10
1
Carry:
x
+y
base-16
L11
46
Arithmetical Algorithms
Addition
Numbers are added from least
significant digit to most, while
carrying any overflow resulting
from adding a column:
base-10
1
Carry:
x
+y
base-16
1
L11
47
Arithmetical Algorithms
Addition
Numbers are added from least
significant digit to most, while
carrying any overflow resulting
from adding a column:
base-10
Carry:
x
+y
base-16
1
L11
48
Arithmetical Algorithms
Addition
Numbers are added from least
significant digit to most, while
carrying any overflow resulting
from adding a column:
base-10
Carry:
x
+y
base-16
L11
49
Addition of Positive
Numbers
string add(strings xk xk-1x1x0, yk yk-1y1y0 , int
base)
carry = 0, xk+1 = yk+1 = 0
for(i = 0 to k+1)
digitSum = carry + xi + yi
zi = digitSum mod base
carry = digitSum /base
return zk+1zk zk-1z1z0
L11
50
1s Complement
2s Complement
The binary number system makes some
operations especially simple and
efficient under certain representations.
Two such representations are
1s complement
2s complement
51
1s Complement
Fix k bits. (EG, k = 8 for bytes)
Represent numbers with |x | < 2k-1
Left-most bit tells the sign
L11
52
1s Complement
Examples
k = 8:
00010010 represents 18
11101101 represents -18
Notice: when add these representations as
usual get 11111111, i.e. negative
00000000 or -0 = 0.
Guess: adding numbers with mixed sign
works the same as adding positive numbers
Trade-off: 0 not unique
L11
53
1s Complement
Addition
Addition is the same as usual binary addition except:
if the final carry is 1, cycle the carry to the least
significant digit:
00010010 represents 18, 11110011 represents -12
Sum 00000110 represents 6:
Carry:
x 0
+y 1
pre-sum
overflow
answerL11
0
1
0
1
1
1
0
0
0
0
1
1
0
1
54
1s Complement
Addition
Addition is the same as usual binary addition except:
if the final carry is 1, cycle the carry to the least
significant digit:
00010010 represents 18, 11110011 represents -12
Sum 00000110 represents 6:
Carry:
x 0
+y 1
pre-sum
overflow
answerL11
0
1
0
1
1
1
0
0
0
0
1
1
0
1
1
55
1s Complement
Addition
Addition is the same as usual binary addition except:
if the final carry is 1, cycle the carry to the least
significant digit:
00010010 represents 18, 11110011 represents -12
Sum 00000110 represents 6:
Carry:
x 0
+y 1
pre-sum
overflow
answerL11
0
1
0
1
1
1
0
0
0
0
1
1
0
0
1
1
56
1s Complement
Addition
Addition is the same as usual binary addition except:
if the final carry is 1, cycle the carry to the least
significant digit:
00010010 represents 18, 11110011 represents -12
Sum 00000110 represents 6:
Carry:
x 0
+y 1
pre-sum
overflow
answerL11
0
1
0
1
1
1
0
0
0
0
1
1
1
0
0
1
1
57
1s Complement
Addition
Addition is the same as usual binary addition except:
if the final carry is 1, cycle the carry to the least
significant digit:
00010010 represents 18, 11110011 represents -12
Sum 00000110 represents 6:
Carry:
x 0
+y 1
pre-sum
overflow
answerL11
0
1
0
1
1
1
0
0
0
0
0
1
1
1
0
0
1
1
58
1s Complement
Addition
Addition is the same as usual binary addition except:
if the final carry is 1, cycle the carry to the least
significant digit:
00010010 represents 18, 11110011 represents -12
Sum 00000110 represents 6:
Carry:
x 0
+y 1
pre-sum
overflow
answerL11
0
1
0
1
1
1
0
0
0
0
0
0
1
1
1
0
0
1
1
59
1s Complement
Addition
Addition is the same as usual binary addition except:
if the final carry is 1, cycle the carry to the least
significant digit:
00010010 represents 18, 11110011 represents -12
Sum 00000110 represents 6:
Carry:
x 0
+y 1
pre-sum
overflow
answerL11
0
1
0
1
0
1
1
0
0
0
0
0
0
1
1
1
0
0
1
1
60
1s Complement
Addition
Addition is the same as usual binary addition except:
if the final carry is 1, cycle the carry to the least
significant digit:
00010010 represents 18, 11110011 represents -12
Sum 00000110 represents 6:
Carry:
1
x 0
+y 1
pre-sum
overflow
answerL11
0
1
0
0
1
0
1
1
0
0
0
0
0
0
1
1
1
0
0
1
1
61
1s Complement
Addition
Addition is the same as usual binary addition except:
if the final carry is 1, cycle the carry to the least
significant digit:
00010010 represents 18, 11110011 represents -12
Sum 00000110 represents 6:
Carry:
1
x 0
+y 1
pre-sum 0
overflow
answerL11
0
1
0
0
1
0
1
1
0
0
0
0
0
0
1
1
1
0
0
1
1
62
1s Complement
Addition
Addition is the same as usual binary addition except:
if the final carry is 1, cycle the carry to the least
significant digit:
00010010 represents 18, 11110011 represents -12
Sum 00000110 represents 6:
Carry:
1
x 0
+y 1
pre-sum 0
overflow
answerL11
0
1
0
0
1
0
1
1
0
0
0
0
0
0
1
1
1
0
0
1
1
1
63
1s Complement
Addition
Addition is the same as usual binary addition except:
if the final carry is 1, cycle the carry to the least
significant digit:
00010010 represents 18, 11110011 represents -12
Sum 00000110 represents 6:
Carry:
1
x 0
+y 1
pre-sum 0
overflow
answerL11
0
1
0
0
1
0
1
1
0
0
0
0
0
0
1
1
1
0
1
0
1
1
1
64
1s Complement
Addition
Addition is the same as usual binary addition except:
if the final carry is 1, cycle the carry to the least
significant digit:
00010010 represents 18, 11110011 represents -12
Sum 00000110 represents 6:
Carry:
1
x 0
+y 1
pre-sum 0
overflow
answerL11
0
1
0
0
1
0
1
1
0
0
0
0
0
0
1
1
1
0
1
0
1
1
1
65
1s Complement
Addition
Addition is the same as usual binary addition except:
if the final carry is 1, cycle the carry to the least
significant digit:
00010010 represents 18, 11110011 represents -12
Sum 00000110 represents 6:
Carry:
1
x 0
+y 1
pre-sum 0
overflow
answerL11
0
1
0
0
1
0
1
1
0
0
0
0
0
0
1
1
1
0
1
0
1
1
1
66
1s Complement
Addition
Addition is the same as usual binary addition except:
if the final carry is 1, cycle the carry to the least
significant digit:
00010010 represents 18, 11110011 represents -12
Sum 00000110 represents 6:
Carry:
1
x 0
+y 1
pre-sum 0
overflow
answerL110
0
1
0
0
1
0
1
1
0
0
0
0
0
0
1
1
1
0
1
0
1
1
1
67
2s Complement
Fixes the non-uniqueness of zero
problem
Adding mixed signs still easy
No cycle overflow (pre-computed)
Javas approach (under the hood)
Same fixed length k , sign convention,
and definition of positive numbers as
with 1s complement
Represent numbers with -2(k-1) x < 2(k-1)
2s Complement
Negatives (slightly harder than 1s comp.):
1. Compute 1s complement
2. Add 1
Summarize: -x = x + 1.
00010010 represents 18
11101101 + 1 = 11101110 represents -18.
Add together without over-flow: 00000000
Q: What are the ranges of Javas 32-bit int
and 64-bit long? (All of Javas integer
types use 2s complement)
L11
69
2s Complement
A:
70
2s Complement
Addition
Addition is the same as usual binary
addition no exceptions!!
11101110 = (-18)10, 11110100 = (-12)10
Sum together (11100010) = (-30)10:
Carry:
x 1
+y
L11
1
1
1
1
0
1
1
0
1
1
1
0
0
0
71
2s Complement
Addition
Addition is the same as usual binary
addition no exceptions!!
11101110 = (-18)10, 11110100 = (-12)10
Sum together (11100010) = (-30)10:
Carry:
x 1
+y
L11
1
1
1
1
0
1
1
0
1
1
1
0
0
0
0
72
2s Complement
Addition
Addition is the same as usual binary
addition no exceptions!!
11101110 = (-18)10, 11110100 = (-12)10
Sum together (11100010) = (-30)10:
Carry:
x 1
+y
L11
1
1
1
1
0
1
1
0
1
1
1
0
1
0
0
0
73
2s Complement
Addition
Addition is the same as usual binary
addition no exceptions!!
11101110 = (-18)10, 11110100 = (-12)10
Sum together (11100010) = (-30)10:
Carry:
x 1
+y
L11
1
1
1
1
0
1
1
0
1
1
0
1
0
1
0
0
0
74
2s Complement
Addition
Addition is the same as usual binary
addition no exceptions!!
11101110 = (-18)10, 11110100 = (-12)10
Sum together (11100010) = (-30)10:
Carry:
x 1
+y
L11
1
1
1
1
0
1
1
0
0
1
1
0
1
0
1
0
0
0
75
2s Complement
Addition
Addition is the same as usual binary
addition no exceptions!!
11101110 = (-18)10, 11110100 = (-12)10
Sum together (11100010) = (-30)10:
Carry:
x 1
+y
L11
1
1
1
1
0
1
0
1
0
0
1
1
0
1
0
1
0
0
0
76
2s Complement
Addition
Addition is the same as usual binary
addition no exceptions!!
11101110 = (-18)10, 11110100 = (-12)10
Sum together (11100010) = (-30)10:
Carry:
x 1
+y
L11
1
1
1
1
1
0
1
0
1
0
0
1
1
0
1
0
1
0
0
0
77
2s Complement
Addition
Addition is the same as usual binary
addition no exceptions!!
11101110 = (-18)10, 11110100 = (-12)10
Sum together (11100010) = (-30)10:
Carry: 1
x 1
+y
L11
1
1
1
1
1
1
0
1
0
1
0
0
1
1
0
1
0
1
0
0
0
78
2s Complement
Addition
Addition is the same as usual binary
addition no exceptions!!
11101110 = (-18)10, 11110100 = (-12)10
Sum together (11100010) = (-30)10:
Carry: 1
x 1
+y
1
1
L11
1
1
1
1
1
1
0
1
0
1
0
0
1
1
0
1
0
1
0
0
0
79
2s Complement
Addition
Addition is the same as usual binary addition no
exceptions!!
11101110 = (-18)10, 11110100 = (-12)10
Sum together (11100010) = (-30) 10:
Carry: 1
x 1
+y
1
1
1
1
1
1
1
1
0
1
0
1
0
0
1
1
0
1
0
1
0
0
0
L11
80
Positive Binary
Multiplication
Long multiplication simplifies in binary because
multiplying by 2k amounts to left-shifting kplaces (<<k), and each time multiply either by
02k or 12k.
x
1 0 1 1
EG:
y
1(x0)
0(x1)
0(x2)
1(x3)
Add rows:
L11
81
Positive Binary
Multiplication
Long multiplication simplifies in binary because
multiplying by 2k amounts to left-shifting kplaces (<<k), and each time multiply either by
02k or 12k.
x
1 0 1 1
EG:
y
1(x0)
0(x1)
0(x2)
1(x3)
Add rows:
L11
1
1
0
0
0
1
1
1
82
Positive Binary
Multiplication
Long multiplication simplifies in binary because
multiplying by 2k amounts to left-shifting kplaces (<<k), and each time multiply either by
02k or 12k.
x
1 0 1 1
EG:
y
1(x0)
0(x1)
0(x2)
1(x3)
Add rows:
L11
1
1
0
0
0
0
0
1
0
1
1
83
Positive Binary
Multiplication
Long multiplication simplifies in binary because
multiplying by 2k amounts to left-shifting kplaces (<<k), and each time multiply either by
02k or 12k.
x
1 0 1 1
EG:
y
1(x0)
0(x1)
0(x2)
1(x3)
Add rows:
L11
0
0
1
1
0
0
0
0
0
0
0
1
0
1
1
84
Positive Binary
Multiplication
Long multiplication simplifies in binary because
multiplying by 2k amounts to left-shifting kplaces (<<k), and each time multiply either by
02k or 12k.
x
1 0 1 1
EG:
y
1(x0)
0(x1)
0(x2)
1(x3) 1
Add rows:
L11
0
0
0
0
1
1
1
0
0
1
0
0
0
0
0
1
0
1
1
85
Positive Binary
Multiplication
Long multiplication simplifies in binary because
multiplying by 2k amounts to left-shifting kplaces (<<k), and each time multiply either by
02k or 12k.
x
1 0 1 1
EG:
y
1(x0)
0(x1)
0(x2)
1(x3) 1
Add rows: 1
L11
0
0
1
0
0
1
0
1
1
0
0
1
0
0
0
0
0
0
1
0
1
1
86
Arithmetical Algorithms
Binary Multiplication
bitstring multiply(bitstrings xk xk-1x1x0, yk yk-1
y 1 y 0)
x = xk xk-1x1x0
p = 0 // the partial product
for(i = 0 to k+1)
if(yi == 1)
p = add(p , x i ) // prev. algorithm
return p
L11
87