Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Hash Function 1: Return the digit in the hundreds place of an integer 𝑥
- int hash1(int x) {
- // Extract the hundreds digit by dividing by 100 and taking modulo 10
- return (x / 100) % 10;
- }
- 2. Hash Function 2: Sum of all digits of an integer x, return the remainder when divided by 19
- int hash2(int x) {
- int sum = 0;
- // Calculate the sum of the digits
- while (x > 0) {
- sum += x % 10; // Add the last digit
- x /= 10; // Remove the last digit
- }
- // Return the remainder when sum is divided by 19
- return sum % 19;
- }
- 3. Hash Function 3: Sum of ASCII values of the letters in a name, return the remainder when divided by 49
- #include <string.h> // Required for string operations
- int hash3(char name[]) {
- int sum = 0;
- int length = strlen(name); // Get the length of the string
- // Sum the ASCII values of all characters in the string
- for (int i = 0; i < length; i++) {
- sum += (int)name[i]; // Add ASCII value of each character
- }
- // Return the remainder when sum is divided by 49
- return sum % 49;
- }
Advertisement
Add Comment
Please, Sign In to add comment