Algorithm for selecting the differents pair - algorithm

May be somebody can help me with it.
So, I have a set of pairs x0-y0, x1-y1, etc.
And always x[i]<y[i]. Thus I need a function (or algorithm) for every pair, so
F(x[i],y[i]) = result[i], and each result for a particular pair must be an integer unique value.

Let M = max(y) - min(y) + 1, then use the formula:
F(x, y) = x * M + y
Remarks:
You don't have to use the exact maximum and minimum, you can use an upperbound and a lowerbound, M = U - L + 1 with U larger than all y and L smaller than all y;
Of course you could do it the other way around instead, with K = max(x) - min(x) + 1, and F(x, y) = y * K + x;
When using a finite integer type, be careful with overflow, for instance if x and y both have values larger than 46340, then F(x, y) won't fit in a 32-bit signed integer.

Related

Find a number for minimum sum of nth power of absolute difference in an array

My question is similar to this, but instead if the absolute difference is raised to a power 'c' (which will be given as input) is there an algorithm to find the answer?
For example, given A = {a1, a2,...., an} and c it should find an x such that it minimises |a1 − x|^c +|a2 − x|^c +··· +|an − x|^c.
If c = 1 it's the median of the sorted array and if c = 2 it's the average of the array, but I can't find connection between median and average which we can extend to any value of c.
I assume that c is a positive integer.
If it is not an integer, then the fractional powers are hard to calculate. If it is negative, then as x goes to infinity (either way) the result goes to 0, so there is no global minimum. If it is 0, then x does not matter. So a positive integer is the only thing that makes sense.
Now each term is a convex function. The sum of convex functions is itself convex. Convex functions have the following properties. Suppose that x < y < z. If f(x) = f(z) then the global minimum is between them. If f(x) = f(y) = f(z), that's a straight line segment. And finally, if f(y) < min(f(x), f(z)) then the global minimum is between x and z.
This is sufficient for a variation on binary search.
while z - x > some tolerance:
if z-y > y-x:
y1 = (y + z) / 2
if f(y1) < f(y):
(x, y, z) = (y, y1, z)
elif f(y1) = f(y):
(x, y, z) = (y1, (2*y1 + y)/3, y)
else:
(x, y, z) = (y1, y, z)
else:
y1 = (x + y) / 2
if f(y1) < f(y):
(x, y, z) = (x, y1, y)
elif f(y1) = f(y):
(x, y, z) = (y1, (2*y1 + y)/3, y)
else:
(x, y, z) = (y1, y, z)
As this runs, each iteration reduces the size of the interval to at most 3/4 of what it previously was. And therefore you will narrow in on the answer.
If you special case c = 1, you can do even better. The second derivative will be defined everywhere and be a non-decreasing function. This allows you to do a binary search, but guess where in the interval the minimum is expected to be. If you land close, you know which way you're wrong, and can put a much tighter bound on it.

Finding distinct pairs {x, y} that satisfies the equation 1/x + 1/y = 1/n with x, y, and n being whole numbers

The task is to find the amount of distinct pairs of {x, y} that fits the equation 1/x + 1/y = 1/n, with n being the input given by the user. Different ordering of x and y does not count as a new pair.
For example, the value n = 2 will mean 1/n = 1/2. 1/2 can be formed with two pairs of {x, y}, whcih are 6 and 3 and 4 and 4.
The value n = 3 will mean 1/n = 1/3. 1/3 can be formed with two pairs of {x, y}, which are 4 and 12 and 6 and 6.
The mathematical equation of 1/x + 1/y = 1/n can be converted to y = nx/(x-n) where if y and x in said converted equation are whole, they count as a pair of {x, y}. Using said converted formula, I will iterate n times starting from x = n + 1 and adding x by 1 per iteration to find whether nx % (x - n) == 0; if it yields true, the x and y are a new distinct pair.
I found the answer to limit my iteration by n times by manually computing the answers and finding the number of repetitions 'pattern'. x also starts with n+1 because otherwise, division by zero will happen or y will result in a negative number. The modulo operator is to indicate that the y attained is whole.
Questions:
Is there a mathematical explanation behind why the iteration is limited to n times? I found out that the limit of iteration is n times by doing manual computation and finding the pattern: that I only need to iterate n times to find the amount of distinct pairs.
Is there another way to find the amount of distinct pairs {x, y} other than my method above, which is by finding the VALUES of distinct pairs itself and then summing the amount of distinct pair? Is there a quick mathematical formula I'm not aware of?
For reference, my code can be seen here: https://gist.github.com/TakeNoteIAmHere/596eaa2ccf5815fe9bbc20172dce7a63
Assuming that x,y,n > 0 we have
Observation 1: both, x and y must be greater than n
Observation 2: since (x,y) and (y,x) do not count as distinct, we can assume that x <= y.
Observation 3: x = y = 2n is always a solution and if x > 2n then y < x (thus no new solution)
This means the possible values for x are from n+1 up to 2n.
A little algebra convers the equation
1/x + 1/y = n
into
(x-n)*(y-n) = n*n
Since we want a solution in integers, we seek integers f, g so that
f*g = n*n
and then the solution for x and y is
x = f+n, y = g+n
I think the easiest way to proceed is to factorise n, ie write
n = (P[1]^k[1]) * .. *(P[m]^k[m])
where the Ps are distinct primes, the ks positive integers and ^ denotes exponentiation.
Then the possibilities for f and g are
f = P[1]^a[1]) * .. *(P[m]^a[m])
g = P[1]^b[1]) * .. *(P[m]^b[m])
where the as and bs satisfy, for each i=1..m
0<=a[i]<=2*k[i]
b[i] = 2*k[i] - a[i]
If we just wanted to count the number of solutions, we would just need to count the number of fs, ie the number of distinct sequences a[]. But this is just
Nall = (2*k[1]+1)*... (2*[k[m]+1)
However we want to count the solution (f,g) and (g,f) as being the same. There is only one case where f = g (because the factorisation into primes is unique, we can only have f=g if the a[] equal the b[]) and so the number we seek is
1 + (Nall-1)/2

Other Models of Computation

can anyone explain this concept to me? and help me out in figuring out the answer? as I don't quite get it yet but any help will do, thank you
There exists two really cool primitive recursive functions:
T(z, x1, x2, ..., xn, y) returns 0 if z is an encoding of a Turing Machine, and its computation with
inputs x1, ... xn encodes to y. The function returns 1 otherwise. U(y) returns the result of the computation y.
Goal:Use these two functions and the µ(mu) operator to get a definition of a function f(x1, x2, ...xn) which returns the output of the TM encoded by z
what I have so far:
using these two functions and the µ operator we can then generate a definition for the function f(x1,x2, ...xn) that returns the output of the TM encoded by z such that we have a TM that we can define as T(n) where this computes the number of primes which are less than or equal n. z = 0 which is the number of primes found, y = 1 which is for each y we test whether it is prime, while k <= n:, j = 1 are the possible divisors of y, d = 0 the number of divisors of k found, while j <= n: if y % j == 0:, d = d + 1, j = j + 1, if d== 2:, z= z+1,y=y+1, then return z which is the output of the TM encoded by z

How to prove the correctness of the algorithm for "Arrange given numbers to form the biggest number"?

Arrange given numbers to form the biggest number gives the algorithm.
It uses the following text to prove the correctness of the algorithm:
So how do we go about it? The idea is to use any comparison based sorting algorithm. In the used sorting algorithm, instead of using the default comparison, write a comparison function myCompare() and use it to sort numbers. Given two numbers X and Y, how should myCompare() decide which number to put first – we compare two numbers XY (Y appended at the end of X) and YX (X appended at the end of Y). If XY is larger, then X should come before Y in output, else Y should come before. For example, let X and Y be 542 and 60. To compare X and Y, we compare 54260 and 60542. Since 60542 is greater than 54260, we put Y first.
Consider three numers: X, Y and Z. Use X -> Y to indicate that X should come before Y. A comparison based algorithm can use the following two comparisons to sort X, Y and Z into XYZ: XY >= YX => X -> Y and YZ >= ZY => Y -> Z. But these two comparisons do not necessarily ensure that XYZ is the largest number. In other words, the fact that X should come before Y and Y should come before Z does not necessarily ensure that XYZ form the largest number. Take YZX as an example. To prove XYZ >= YZX, we need to prove that X(YZ) >= (YZ)X which meains that X should before YZ as a whole to form a bigger number.
Can anyone give a formal proof of the correctness of the algorithm?
First we will prove that if X "<" Y and Y "<" Z then X "<" Z. Assuming that they have p, q and r digits respectively, the first two relations reduce to
X * 10^q + Y ≥ Y * 10^p + X ⇒ X * (10^q - 1) ≥ Y * (10^p - 1)
Y * 10^r + Z ≥ Z * 10^q + Y ⇒ Y * (10^r - 1) ≥ Z * (10^q - 1)
We want to prove
X * 10^r + Z ≥ Z * 10^p + X which is equivalent to X * (10^r - 1) ≥ Z * (10^p - 1)
But this can be proved simply by multiplying the first two inequalities and cancelling off common terms.
Now that we have shown that the relation is transitive (and thus can be used to define a sort order), it is easy to show that it works to solve the problem.
Suppose the numbers given are A, B, C … such that A "<" B "<" C "<" D…. We will show that A has to come first in the final number. If not, we have a string like (some prefix)XA(some suffix) as the final number. Easily, (some prefix)AX(some suffix) is a larger number because A "<" X for all X due to transitivity. Continuing in this fashion A bubbles to the left till it becomes the first element.
Now that we have fixed the first element, the same argument can be applied to B and so on to show that the best solution is ABCD…

Combinations of a sequence with any adjacent product no more than a given number

I was wondering whether there is an analytic solution or recursive solution of the combination calculation of natural number sequence with length X, where the multiplications of any adjacent numbers in the sequence is no more than Y?
For example when X = 3, Y = 3, the sequences are (1,1,1)(1,1,2)(1,1,3)(1,2,1)(2,1,1)(1,3,1)(3,1,1),(3,1,2)(2,1,3)(3,1,3)(2,1,2).
I know when X = 2, such combination is
Y + [Y/2] + [Y/3] + ... +[Y/Y]
how to recursively derive from X to X+1 then? Or is there some direct expression of the solution?
Let's P(Y, X, K) is number of combinations with length X, ending with K. Then
P(Y, X + 1, M) = Sum(k=1..[Y/M] P(Y, X, K))
with starting point:
P(Y, 1, K = 1..Y) = 1

Resources