How Computers Really Do Arithmetic (. ppt )

advertisement
Alpha
A final word about addition/subtraction chains
(an idea due to Rob Miller)
Improving the 2 (log2 n) - 1 upper bound on the
length of the shortest chain for generating n.
Express n using redundant notation with as few
1’s and -1’s as possible.
Examples: 31 = 0 1 1 1 1 1
31 = 1 0 0 0 0 -1
Generate powers of two: 1, 2, 4, …, 2 |log n| .
Add a power of two for each 1, subtract for each -1.
How many 1’s and -1’s are necessary?
Example: 1 1 1 1 1 -1 -1 0 0 1 -1 0 1
1. Adjacent 1 ’s and -1’s can be eliminated, e.g., 1 -1 => 0 1
1 1 1 1 0 1 -1 0 0 1 1 0 1
1 1 1 1 00 1 0 0 1 1 0 1
How many 1’s and -1’s are necessary?
Example: 1 1 1 1 1 -1 -1 0 0 1 -1 0 1
1. Adjacent 1 ’s and -1’s can be eliminated, e.g., 1 -1 => 0 1
1 1 1 1 0 1 -1 0 0 1 1 0 1
1 1 1 1 00 1 0 0 1 1 0 1
2. Three or more consecutive 1’s or -1’s can be replaced
by 1 0 … 0 -1 or -1 0 … 0 1
1 0 0 0 0 -1 0 0 1 0 1 1 0 1
How many 1’s and -1’s are necessary?
Example: 1 1 1 1 1 -1 -1 0 0 1 -1 0 1
1. Adjacent 1 ’s and -1’s can be eliminated, e.g., 1 -1 => 0 1
1 1 1 1 0 1 -1 0 0 1 1 0 1
1 1 1 1 00 1 0 0 1 1 0 1
2. Three or more consecutive 1’s or -1’s can be replaced
by 1 0 … 0 -1 or -1 0 … 0 1
1 0 0 0 0 -1 0 0 1 0 1 1 0 1
3. Each 0 1 1 can be replaced with 1 0 -1:
1 0 0 0 0 -1 0 0 1 1 0 -1 0 1
1 0 0 0 0 -1 0 1 0 -1 0 -1 0 1
(Go back to 1. if 1 -1, -1 1, 1 1 1, or 0 0 0 is created.)
4. Each 0 -1 -1 can be replaced with -1 0 1
1 0 0 0 0 -1 0 1 0 -1 0 -1 0 1
When this process terminates,
there are no consecutive 1’s or -1’s.
So at most half of the bits are 1’s
and -1’s. There are |log2 n| bits.
New upper bound:
| (3/2) (|log2 n|+1) |
Cool
Total time?
O(log2 n)
Total time? O(log3 n)
Download