There is a standard hash function where we take the mod from the key in order to define the address. That is, h(k) = k(mod) p (where p is some prime number).This is called the division method.
I have seen a variation of this "division method" where you first multiply the key by a prime and then you take the mod. For example: h(k) = (k*17) mod 11.
What is the purpose to multiply the key by a prime (17,37...) before calculating the mod?? is it to improve the distribution of the keys?
Related
As I am new on MatLab and Mathematica, I am trying to solve two (easy) problems using one of these two programmes.
"In number theory, Lagrange’s four-square theorem, states that every natural number n can be written as n= a^2+ b^2 + c^2 + d^2, where a, b, c, d are integers.
Given a natural number n, display all possible integers a, b, c, d.
The number of ways to write a natural number
n as the sum of four squares is denoted by r4(n). Using Jacobi's theorem, plot the function r4(n)
and compare it with the function 8n√(log n)."
This is a partial answer using Mathematica build-in functions
PowerRepresentations[n,k,p] gives the distinct representation of the integer n as sum of k non-negative p th integer powers.
Attention: by distinct we mean: if n = n1^p + n2^p + n3^p ... the function returns k-tuples such that n1<=n2<=n3...
Example:
PowerRepresentation[20,4,2]
gives
{{0,0,2,4},{1,1,3,3}}
To get the number of possible representations of integer n as a sum of d squares you can use the SquaresR[d,n] function (your rd(n) functions).
Example:
SquaresR[4,20]
prints
144
However as you explained there is still some works because rd(n) also returns negative solutions and permuted ones.
For instance:
SquaresR[2,20]
returns
8
You must understand 8 as counting without distinction:
4 sign changes:
{2,4},{2,-4},{-2,4},{-2,-4}
times
2 permutations
{2,4},{4,2}
I want to find the smallest integer P, such that the number of primes in the set {1,2,..., P} is less than P/6.
I think have the answer via (long) trial and error but would like to know how to verify this through MATLAB.
You can use isprime to check if any value in an array is a prime number. If we want to check all integers up until the integer N we can do
% You can change this to the maximum number that you'd like to consider for P
N = 2000;
possible_P_values = 2:N; % We omit 1 here since it's not a prime number
primes = isprime(possible_P_values);
To determine how many primes have occured up to a given integer N we can use cumsum of this logical matrix (the cumulative sum)
nPrimes_less_than_or_equal_to_P = cumsum(primes);
Then we can divide possible_P_values by 6 and check where the number of primes up to a certain point is less than that number.
is_less_than_P_over_6 = nPrimes_less_than_or_equal_to_P < (possible_P_values ./ 6);
Then we can identify the first occurance with find
possible_P_values(find(is_less_than_P_over_6, 1, 'first'))
% 1081
Given an integer m, a hash function defined on T is a map T -> {0, 1, 2, ..., m - 1}. If k is an element of T and m is a positive integer, we denote hash(k, m) its hashed value.
For simplicity, most hash functions are of the form hash(k, m) = f(k) % m where f is a map from T to the set of integers.
In the case where m = 2^p (which is often used to the modulo m operation is cheap) and T is a set of integers, I have seen many people using f(k) = c * k with c being a prime number.
I understand if you want to choose a function of the form f(k) = c * k, you need to have gcd(c, m) = 1 for every hash table size m. Even though using a prime number fits the bill, c = 1 is also good.
So my question is the following: why do people still use f(k) = prime * k as their hash function? What kind of nice property does it have?
You don't need it to be prime. One of the most efficient hash functions with provable collision resistance just multiplies with a random number: https://en.wikipedia.org/wiki/Universal_hashing#Avoiding_modular_arithmetic. You do however need it to be odd.
From a computation in Mathematica I got a huge matrix with numbered variables of the form a[1],...,a[100] in some of the entries. I would like to import this matrix as a template to matlab and then substitute random numbers (normally distributed) in place of the variables. I am completely unfamiliar with the support of symbolic variables in Matlab and am not sure whether it supports indexed symbolic variables. I would need some function that searches for the a[k] and replaces them with a random number.
In Mathematica I have matrices square matrices of length 2^n where which get more and more sparse as n grows and depend on 5*n (yet symbolic) variables a[k]. For n=2 the matrix is not yet sparse at all and looks like (in Mathematica-Code):
{{a[3] + a[3], a[7] - I a[8], a[10], I a[8]},
{I a[8], +a[6], I a[5], -I a[9] - a[8]},
{a[7] + I a[8], +a[2], I a[5], -a[7]},
{I a[8], a[2], a[2] + I a[15], -a[8]}}
There exists a script ToMatlabwhich converts the Mathematica notation for matrices to the Matlab notation. I have basically all freedom in renaming the variables as it is most suitable for use in Matlab. Now I would like to create a function in Matlab which returns this exact matrix (for fixed n would be sufficient for now, so the matrix is really fixed) and replaces the a[k] with a normally distributed random number.
Assuming you have a cell array of strings (much bigger than this simple example):
import = {'a1','a2';'a2','a4'};
Then you can replace 100 values with normally distributed random numbers as follows (obviously you will want to replace constants etc with the values you need):
newMatrix = zeros(size(import));
% generate 100 random numbers:
mean = 123.45;
stdDev = 21.0;
N = 100;
randVals = randn(1,N) * stdDev + mean;
for ii=1:N
indx = find(ismember(import, sprintf('a%d',ii)));
newMatrix(indx) = randVals(ii);
end
The new values will be in the matrix newMatrix
This is not super efficient; but it may be a start, and it may get other answers flowing (if you can confirm that this does indeed do what you intend - still not 100% sure I understand your question).
I am trying to solve an optimization problem on MATLAB or CPLEX. We have two sets A (n elements), and B (m elements). We have to assign exactly one element in A to one element in B.
A single element in B can be assigned as many elements in A as needed (maximum n). There is a cost of assigning an element i in A to an j element in B = cij.
Moreover, there is another cost associated with the NUMBER of elements in A assigned to a element in B (load). That cost is: lj = ( sum (number of elements assigned to j) ^2 )
The overall cost is therefore: sum (cij+lj)
We would like to find the optimal assignment such that: sum (cij+lj) is minimized.
The problem can be formulated as a binary integer programming IF there was no load. My concern is how can I write such a function in either MATLAB or CPLEX.
You wish to minimize the cost of this assignment from one vector to another, subject to the number of variables to be assigned, from 1 to n inclusive? If so:
[x,fval] = fminsearch(#(x) sum(arrayfun( #(y) y*cij+y^2,1:n)),1)
or
function out = minFunc(x,n)
out = 0
for ii=1:n
out = out + cij*ii + n^2;
end
end
Where cij=1 for demonstration purposes and Ij = N^2, N = number of elements assigned to j. This seems to simplistic for your needs since it will always return x = 1. However, the variable to be minimized x, is not used, so I'm not sure what you are trying to minimize. Please let me know if I may help further.