Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Author: Braden Best (2016)
- *
- * License: Do whatever you want unless it's illegal. Please don't use my source code to harvest livers or build a death ray.
- *
- * Description: This is an experiment to test a hypothesis about collisions in Java's hashCode() function.
- *
- * The experiment: Given the same algorithm, will the inputs "DB" and "Ca" generate the same output?
- *
- * Result: Success
- */
- #include <stdio.h>
- #include <string.h>
- int
- power(int a, int b){
- int total = a,
- base = b - 1;
- if(b == 0)
- return 1;
- if(b == 1)
- return a;
- while(base){
- total *= a;
- base--;
- }
- return total;
- }
- int
- hashCode(char *str)
- {
- char *p = str;
- int total = 0;
- while(*p){
- total += (*p) * power(31, strlen(str) - (1 + (p-str)));
- p++;
- }
- return total;
- }
- int
- main()
- {
- printf("DB: %u\n", hashCode("DB"));
- printf("Ca: %u\n", hashCode("Ca"));
- printf("tt: %u\n", hashCode("tt"));
- printf("Russian Dancing Men: %u\n", hashCode("Russian Dancing Men"));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement