Apr 12
homework
So : how was the C hash table stuff?
If issues with strmap C code, here's
some help:
hash table 2 .
That folder has
- string to integer, integer to string example, since strmap stores (string,string) pairs, not (string,integer) pairs
- a DEBUG option I added within strmap.c
- a strmap_dump function I added to strmap
- a strmap_get_collisions() function I added
Browse through the code a bit, discussing what it's doing compared with the python one we did last time. Similarities? Differences?
Using the wikipedia categories of hash tables, what kind is this
one?
Note idiomatic C such as this in hash() :
static unsigned long hash(const char *str){
unsigned long hash = 5381;
int c;
while (c = *str++) {
hash = ((hash << 5) + hash) + c;
}
if (DEBUG) printf("hash: str=\"%s\", hash=%lu \n", str, hash);
return hash;
}
Explain the meaning of *str++ : what is getting incremented? What is the order of operations here?
crypto and number theory
Resources
1. Discus basic ideas of symmetric vs public key encryption
2. Back up and talk about some algorithmic number theory, as a warm-up :
a) GCD (greatest common divisor)
- naive approaches : prime factors ... but they're hard to find
- Euclid's GCD algorithm :
- no change when smaller is subtracted from larger
- so reduce GCD(a,b) to GCD(b, a mod b) until
- oldest numeric algorithm still in use
- Do an example on the board
- What is the O() efficiency of this?
b) lists of prime numbers
- naive approaches : testing possible factors of each integer
- Sieve of Erastosthenes is much better
- ... but even that takes too long for really big primes
- instead: probabilistic prime tests
prime tests
The general idea (there are several variations) looks like this :
If p is prime, then a**(p-1) mod p = 1 . (Fermat's little theorem)
The key piece here is (a**n mod p). Do a few examples and discuss what's going on.
Can we turn this around? No. But *almost*.
If we pick a number B, and find that x < B have the above property, the probably B is prime. And the more x's that work, the more likely B is to be prime.
More coming ...