There’s this code from here
ub4 additive(char *key, ub4 len, ub4 prime)
{
ub4 hash, i;
for (hash=len, i=0; i<len; ++i)
hash += key[i];
return (hash % prime);
}
They say this takes 5n+3 instructions. How did they calculate it? How should i calculate it?
To make this calculation You need some base primitive system. For instance in The art of Computer programming, Knuth uses the MIX computer to do these kind of calculations. Different base computers might have different instructions, and different outcomes to this kind of calculation. In your particular example a common way to set it up would be:
and this would sum up as 5n + 3.
Variations might be along the lines of:
hashandi, might be time consuming. A normal cpu might not need to do extra work because of the declarations, think register/stack storage.hash += hash + key[i]might be counted as one operation on the base system, and so on.Edit: Note that these kind of calculations are mostly useful as thought experiments on hypothetical hardware. A real life processor would quite likely not behave exactly as these calculations other than in very rare cases.