In K&R, chapter 6, section 6, there is a funtion called hash that hashes a
string, which will be stored in a linked list. The function in question is on
page 144, but here it is for those of you who don't have K&R handy.

        /* hash:  form hash value for string s */
        unsigned
        hash(char *s)
        {
                unsigned hashval;
                
                for(hashval = 0; *s != '\0'; s++)
                        hashval = *s + 31 * hashval;
                return hashval % HASHSIZE;
        }

So what is the purpose of the magic 31? The only thing that I think might be a
reference to what it may be for is the paragraph prior, which states

        The hashing function, ..., adds each character value in the string to a
        scrambled combination of the previous ones and returns the remainder
        modulo the array size.

Does the magic 31 have to do with scrambling?

Attachment: pgpcMVHU7rsZH.pgp
Description: PGP signature

Reply via email to