diff options
| author | mo khan <mo.khan@gmail.com> | 2020-05-17 15:42:57 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-05-17 15:42:57 -0600 |
| commit | 5938a8022ddb68fa157d1cde969bbbb46f36e8e8 (patch) | |
| tree | 783bf98256d380bde5d9dec49eeaf40c673b55c4 | |
| parent | fa0ed65bf30d1b65cc9afd4004a74f5b4c34b7e4 (diff) | |
Add math notes
| -rw-r--r-- | img/eulers-constant.png | bin | 0 -> 15365 bytes | |||
| -rw-r--r-- | unit/1/README.md | 91 |
2 files changed, 91 insertions, 0 deletions
diff --git a/img/eulers-constant.png b/img/eulers-constant.png Binary files differnew file mode 100644 index 0000000..ed64e0b --- /dev/null +++ b/img/eulers-constant.png diff --git a/unit/1/README.md b/unit/1/README.md index b3a4b95..3f09db2 100644 --- a/unit/1/README.md +++ b/unit/1/README.md @@ -49,3 +49,94 @@ The operations can be implemented with a Deque interface. * `removeFirst()` -> `remove(0)` * `addLast(x)` -> `add(size(), x)` * `removeLast()` -> `remove(size() - 1)` + +### USet + +The `USet` interface represents an unordered set of unique elements, which +mimics a mathematical set. A `USet` contains `n` distinct elements; no +element appears more than once; the elements are in no specific order. A +`USet` supports the following operations: + +* `size()`: return the number, `n`, of elements in the set. +* `add(x)`: add the element `x` to the set if not already present; +* `remove(x)`: remove `x` from the set; +* `find(x)`: find `x` in the set if it exists + +### SSet + +The `SSet` interface represents a sorted set of elements. An `SSet` stores elements +from some total order so that any two elements x and y can be compared. In code +examples, this will be done with a method called `compare(x, y)` in which: + +* < 0 if x < y +* > 0 if x > y +* = 0 if x == y + +An `SSet` supports the `size()` and `add()` and `remove()` methods with +exactly the same semantics as in the `USet` interface. The difference +between a `USet` and an `SSet` is in the `find(x)` method: + +> successor search: locate x in the sorted set; +> find the small element y in the set such that y >= x. +> return y or null if no such element exists. + + +The extra functionality provided by a SSet usually comes with a price that +includes both a larger running time and a higher implementation complexity. +SSet implementations may have a `find(x)` running time of of logarithmic +and a USet may have a running time of constant time. + + +## Math Review + +### Exponentials and Logarithms + +The expression b^x denotes the number `b` raised to the power of `x`. + +* when x is negative, b^x = 1/(b^-x) +* when x is 0, b^x = 1 + + +```text +b^x = b * b * ... x b + |____________| + | + x times +``` + +```ruby +b ** x = (x.times.inject(1) { |m, _| m * b } +``` + +```irb +irb(main):001:0> 2 ** 10 +=> 1024 +irb(main):002:0> 10.times.inject(1) { |m, _| m * 2 } +=> 1024 +``` + +log b(k) deontes base-b logarithm of k. i.e b^x = k + +```text + log b(k) == b^x = k +``` + +```ruby +irb(main):016:0> 2 ** 10 +=> 1024 +irb(main):017:0> Math.log2(1024) +=> 10.0 +``` + +An informal way to think about logarithms is to think of logb(k) as the number +of times we have to divide k by b before the result is less than or equal to 1. + +For example, when one does binary search, each comparison reduces the number of +possible answers by a factor of 2. This is repeated until there is at most one +possible answer. Therefore the number of comparisons done by binary search when there +are initially at most n + 1 possible answers is at most log2(n+1). + +Another logarithm that comes up several times in this book is the natural logarithm. +Here we use the notation ln k to denote log e(k), where e -- Euler's constant -- is given by: + + |
