hash function h(k) = k mod m - data-structures

For general integer keys and a table of size M, a prime number:
• a good fast general purpose hash function is H(K) = K mod M
can someone please explain what H(K) = K mod M means or how it works im really confused what this hash function is supposed to represent

K mod M is the remainder of K when divided by M. In many languages this is computed by the % operator. As K mod M will always be between 0 and M-1, we can always map an integer to one of the M slots.

Related

What's the explanation behind logarithmic solution of count total bits in a number?

There are many solutions to count the total no. of bits of a number and below is one of them:-
int total_bits=log2(num)+1;
Can you explain, what's the use of log2(num) and adding 1?
Thanks & Regards
Let n be a positive integer. Let k be its number of bits.
Then n and k are linked by this mathematical relation:
2^(k-1) ≤ n < 2^k
Where ^ represents exponentiation.
Now, logarithm is a strictly increasing function, so this relation is equivalent to:
log2(2^(k-1)) ≤ log2(n) < log2(2^k)
And since log2 is exactly the inverse function of 2^..., this is the same as:
k-1 ≤ log2(n) < k
Or equivalently:
k ≤ log2(n) + 1 < k+1
In other words, the integer k is the integer part of log2(n) + 1. We can write this as:
k = floor(log2(n) + 1)
Or in language C:
int k = log2(n) + 1;
Note: In this answer, I used ^ to represent exponentiation. In most programming languages, ^ represents bitwise-xor, which is completely unrelated to exponents. Be careful and avoid using ^ for exponents in your programs.
This doesn't count the number of bits, but it may or may not return the index of the highest bit that is set.
"May or may not" because of rounding errors: First, log2(65536) might not return 16, but 15.999999999999999999999 in which case you get the wrong answer. Second, if you need this for 64 bit numbers, then I can guarantee that either log2(0x8000_0000_0000_0000) or log2(0x7fff_ffff_ffff_ffff) will give the wrong result.

how can i prove the following algorithm?

Exp(n)
If n = 0
Return 1
End If
If n%2==0
temp = Exp(n/2)
Return temp × temp
Else //n is odd
temp = Exp((n−1)/2)
Return temp × temp × 2
End if
how can i prove by strong induction in n that for all n ≥ 1, the number of multiplications made by
Exp (n) is ≤ 2 log2 n.
ps: Exp(n) = 2^n
A simple way is to use strong induction.
First, prove that Exp(0) terminates and returns 2^0.
Let N be some arbitrary even nonnegative number.
Assume the function Exp correctly calculates and returns 2^n for every n in [0, N].
Under this assumption, prove that Exp(N+1) and Exp(N+2) both terminate and correctly return 2^(N+1) and 2^(N+2).
You're done! By induction it follows that for any nonnegative N, Exp(N) correctly returns 2^N.
PS: Note that in this post, 2^N means "two to the power of N" and not "bitwise xor of the binary representations of 2 and N".
The program exactly applies the following recurrence:
P[0] = 1
n even -> P[n] -> P[n/2]²
n odd -> P[n] -> P[(n-1)/2]².2
the program always terminates, because for n>0, n/2 and (n-1)/2 < n and the argument of the recursive calls always decreases.
P[n] = 2^n is the solution of the recurrence. Indeed,
n = 0 -> 2^0 = 1
n = 2m -> 2^n = (2^m)²
n = 2m+1 -> 2^n = 2.(2^n)²
and this covers all cases.
As every call decreases the number of significant bits of n by one and performs one or two multiplications, the total number does not exceed two times the number of significant bits.

Minimize number of divisors of an integer within an interval

I have recently stumbled upon an algorithmic problem and I can't get the end of it. You're given a positive integer N < 10^13, and you need to choose a nonnegative integer M, such that the sum: MN + N(N-1) / 2 has the least number of divisors that lie between 1 and N, inclusive.
Can someone point me to the right direction for solving this problem?
Thank you for your time.
Find a prime P greater than N. There are a number of ways to do this.
If N is odd, then M*N + N*(N-1)/2 is a multiple of N. It must be divisible by any factor of N, but if we choose M = P - (N-1)/2, then M*N + N*(N-1)/2 = P*N, so it isn't divisible by any other integers between 1 and N.
If N is even, then M*N + N*(N-1)/2 is a multiple of N/2. It must be divisible by any factor of N/2, but if we choose M = (P - N + 1)/2 (which must be an integer), then M*N + N*(N-1)/2 = (P - N + 1)*N/2 + (N-1)*N/2 = P*N/2, so it isn't divisible by any other integers between 1 and N.

Calculating inverse Mod where Mod is not prime

I want to calculate value of
F(N) = (F(N-1) * [((N-R+1)^(N-R+1))/(R^R)]) mod M for given values of N,R and M.
Here A^B shows A power B and NOT any Bitwise operation
Here M need not to be prime.How to approach this question?Please help because if M was prime that it would not have beed so difficult to find inverse of R^R mod M.
But as M can be any value ranging from 1 to 10^9.I am not able to tackle this problem.
N can be between 1 and 10^5 and R is less than or equal to N.
Assuming you know somehow that the result of the division is an integer:
As N and R are small, you can do this by computing the prime factorisation of N-R+1 and R.
If we know that R=p^a...q^b then R^R = p^(Ra)...q^(Rb).
Similarly you can work out the power of each prime in (N-R+1)^(N-R+1).
Subtract the powers of the primes in R^R from the powers of the primes in (N-R+1)^(N-R+1) gives you the powers of each prime in the result.
You can then use a standard binary exponentiation routine to compute the result modulo M with no inverses required.

how to solve recursion of given algorithm?

int gcd(n,m)
{
if (n%m ==0) return m;
n = n%m;
return gcd(m,n);
}
I solved this and i got
T(n, m) = 1 + T(m, n%m) if n > m
= 1 + T(m, n) if n < m
= m if n%m == 0
I am confused how to proceed further to get the final result. Please help me to solve this.
The problem here is that the size of the next values of m and n depend on exactly what the previous values were, not just their size. Knuth goes into this in detail in "The Art of Computer Programming" Vol 2: Seminumerical algorithms, section 4.5.3. After about five pages he proves what you might have guessed, which is that the worst case is when m and n are consecutive fibonacci numbers. From this (or otherwise!) it turns out that in the worst case the number of divisions required is linear in the logarithm of the larger of the two arguments.
After a great deal more heavy-duty math, Knuth proves that the average case is also linear in the logarithm of the arguments.
mcdowella has given a perfect answer to this.
For an intuitive explaination you can think of it this way,
if n >= m, n mod m < n/2;
This can be shown as,
if m < n/2, then:
n mod m < m < n/2
if m > n/2, then: n mod m = n-m < n/2
So effectively you are halving the larger input, and in two calls both the arguments will be halved.

Resources