I am just curious why would the two functions produce different results.
unsigned long Hash1(const string s)
{
int length = s.length();
unsigned long h = 1;
for (int i = 0; i < length; i++)
h *= 101 + (unsigned long)s[i];
return h;
}
unsigned long Hash2(const string s)
{
int length = s.length();
unsigned long h = 1;
for (int i = 0; i < length; i++)
h = h * 101 + (unsigned long)s[i];
return h;
}
int main()
{
cout << "Value of hash1 = " << Hash1("kelvin@gmail.com") << endl;
cout << "Value of hash2 = " << Hash2("kelvin@gmail.com") << endl;
system("pause");
return 0;
}
Isn't h *= 101 + (unsigned long)s[i]
equals to h = h * 101 + (unsigned long)s[i]
?
This is the output:
Value of hash1 = 1693843456
Value of hash2 = 2162085645
Thank you for your help!!!