standard ml sorting 3 elements - list

I'm trying to make a standard ml function which takes 3 elements as input and returns a sorted list which is sorted from smallest to largest. I used 3 helper methods that gets me the min, max and the mid elements. the codes are below:
- fun min3 (a, b, c):real =
if a < b andalso a < c then a
else if b < a andalso b < c then b
else c;
- fun mid3 (a, b, c):real =
if (a < b andalso a > c) orelse (a > b andalso a < c) then a
else if (b < a andalso b > c) orelse (b > a andalso b < c) then b
else c;
- fun max3 (a, b, c):real =
if a > b andalso a > c then a
else if b > a andalso b > c then b
else c;
- fun sort3 (a, b, c):real =
min3(a, b, c)::mid3(a, b, c)::max3(a, b, c)::[];
the following worked perfectly when dealing with ints, but when i changed them to reals, the helper methods returned the correct results but i get error when typing the sort method which is the following (couldnt copy the error text so i took a screen shot) :
what could be wrong in the code?
Thanks
Also, is there another way of sorting 3 elements other than the way i posted here or not?

When changing the types, you've made a mistake with the return value of sort3. The error message is telling you that you declared sort3 to return a real while in fact it returns a list of reals.

Related

RSA exponent d is negative

Im trying to encrypt and decrypt various messages with RSA and while it is working flawlessly while d is positive, it obviously breaks when d is negative as d is supposed to be a natural number.
I am using the EXTENDED_EUCLID algorithm to find it and the code is as follows.
void EXTENDED_EUCLID(cpp_int a, cpp_int b, cpp_int&d, cpp_int&x, cpp_int&y) {
cpp_int n_d = d,
n_x = x,
n_y = y;
if(b == 0) {
d = a;
x = 1;
y = 0;
} else {
cpp_int n_a = a % b;
if (n_a < 0) n_a += b;
EXTENDED_EUCLID(b, n_a, n_d, n_x, n_y);
d = n_d;
x = n_y;
y = n_x - a / b * n_y;
}
}
The 2 lines of code before the recursive call EXTENDED_EUCLID(b, n_a, n_d, n_x, n_y); are from a solution I found on https://crypto.stackexchange.com/questions/10805/how-does-one-deal-with-a-negative-d-in-rsa. Obviously I am doing something wrong here, maybe they need to be positioned somewhere else?
The initial call of the EXTENDED_EUCLID is made with the following parameters EXTENDED_EUCLID(a, n, d, x, y); from a function named MODULAR_LINEAR_EQUATION_SOLVER. a in this case is e(public key if I'm not mistaken) and n or b in this case are φ(n).
Thank you for donating your time to this, hopefully not too silly question.
The solution was to move the 2 lines of code that are above the EXTENDED_EUCLID(b, n_a, n_d, n_x, n_y); recursive call to the function MODULAR_LINEAR_EQUATION_SOLVER, below the initial EXTENDED_EUCLID call. Many thanks to President James K. Polk.

Generalized 7-11 mathematical

I have to write a program that solves the 7-11 problem (if you don't know what this is, Google will explain) but instead of finding the values that add and multiply to make $7.11, I have to find all the unique values of a, b, c and d that add and multiply to make $x.yz between $1.00 and $9.99.
It gets to the if statement where it checks if a, b, c and d multiply to make n but it is never true.
def factors(n):
#Finds all the factors of n and adds them to an array
factors_of_n = []
for i in range(1, n):
if(n % i == 0):
factors_of_n.append(i)
#Runs through the array and checks if they add and multiply to
#equal n
for a in factors_of_n:
for b in factors_of_n:
for c in factors_of_n:
for d in factors_of_n:
if(a < b and b < c and c < d):
if(a + b + c + d == n):
if(a * b * c * d == n * 1000000):
#It never gets into this loop
print "please"
return True
def g711():
min = 100
max = 999
count = 0
for n in range(min, max):
if factors(n):
print "yay"
I just need somebody that is a bit better at maths than I am to check it over and see where I am going wrong.
This can be simplified using itertools.combinations_with_replacement:
from itertools import combinations_with_replacement as cwr
def factors(n):
...
r = {}
for n in range(100, 999):
for a, b, c, d in cwr([f for f in factors(n*1000000) if f < n], r=4):
if a+b+c+d == n and a*b*c*d == n*1000000:
r.setdefault(n, []).append((a,b,c,d))
Results:
{644: [(125, 160, 175, 184)],
651: [(125, 140, 186, 200)],
660: [(110, 150, 200, 200)],
...
711: [(120, 125, 150, 316)],
...
992: [(32, 250, 310, 400)]
}

Range Update - Range Query using Fenwick Tree

http://ayazdzulfikar.blogspot.in/2014/12/penggunaan-fenwick-tree-bit.html?showComment=1434865697025#c5391178275473818224
For example being told that the value of the function or f (i) of the index-i is an i ^ k, for k> = 0 and always stay on this matter. Given query like the following:
Add value array [i], for all a <= i <= b as v Determine the total
array [i] f (i), for each a <= i <= b (remember the previous function
values ​​clarification)
To work on this matter, can be formed into Query (x) = m * g (x) - c,
where g (x) is f (1) + f (2) + ... + f (x).
To accomplish this, we
need to know the values ​​of m and c. For that, we need 2 separate
BIT. Observations below for each update in the form of ab v. To
calculate the value of m, virtually identical to the Range Update -
Point Query. We can get the following observations for each value of
i, which may be:
i <a, m = 0
a <= i <= b, m = v
b <i, m = 0
By using the following observation, it is clear that the Range Update - Point Query can be used on any of the BIT. To calculate the value of c, we need to observe the possibility for each value of i, which may be:
i <a, then c = 0
a <= i <= b, then c = v * g (a - 1)
b <i, c = v * (g (b) - g (a - 1))
Again, we need Range Update - Point Query, but in a different BIT.
Oiya, for a little help, I wrote the value of g (x) for k <= 3 yes: p:
k = 0 -> x
k = 1 -> x * (x + 1) / 2
k = 2 -> x * (x + 1) * (2x + 1) / 6
k = 3 -> (x * (x + 1) / 2) ^ 2
Now, example problem SPOJ - Horrible Queries . This problem is
similar issues that have described, with k = 0. Note also that
sometimes there is a matter that is quite extreme, where the function
is not for one type of k, but it could be some that polynomial shape!
Eg LA - Alien Abduction Again . To work on this problem, the solution
is, for each rank we make its BIT counter m respectively. BIT combined
to clear the counters c it was fine.
How can we used this concept if:
Given an array of integers A1,A2,…AN.
Given x,y: Add 1×2 to Ax, add 2×3 to Ax+1, add 3×4 to Ax+2, add 4×5 to
Ax+3, and so on until Ay.
Then return Sum of the range [Ax,Ay].

Is there a way to set a decision variable to true iff a variable is in a range in an integer linear program?

I have an integer-valued bounded variable, call it X. (Somewhere around 0<=X<=100)
I want to have a binary variable, call it Y, such that Y=1 if X >= A and X <= B, otherwise Y=0.
The best I've come up with thus far is the following (where T<x> are introduced binary variables, and M is a large number)
(minimize Y)
(X - A) <= M*Ta
(B - X) <= M*Tb
Y <= Ta
Y <= Tb
Y >= Ta + Tb - 1
(In other words, introducing two binary variables that are true if the variable satisfies the lower and upper bounds of the range, respectively, and setting the result to the binary multiplication of those variables)
This... Works, sort of, but has a couple major flaws. In particular, it's not rigorously defined - Y can be 1 even if X is outside the range.
So: is there a better way to do this? In particular: is there a way to rigorously define it, or if not, a way to at least prevent false positives?
Edit: to clarify: A and B are variables, not parameters.
I think the below works.
(I) A * Y <= X <= B * Y + 100 * (1 - Y)
(II) (X - A) <= M * Ta
(III) (B - X) <= M * Tb
(IV) Y >= Ta + Tb - 1
So X < A makes:
(I)Y=0
and (II), (III), (IV) do not matter.
X > B makes:
(I) Y = 0
and (II), (III), (IV) do not matter.
A <= X <= B makes:
(I) Y = 1 or Y = 0
(II) Ta = 1
(III) Tb = 1
(IV) Y = 1
Rewriting loannis's answer in a linear form by expanding out multiplication of binary variables with a continuous variable:
Tc <= M*Y
Tc <= A
Tc >= A - M*(1-Y)
Tc >= 0
Tc <= X
Td <= M*Y
Td <= B
Td >= B - M*(1-Y)
Td >= 0
X <= Td + 100*(1-Y)
(X - A + 1) <= M * Ta
(B - X + 1) <= M * Tb
Y >= Ta + Tb - 1
This seems to work, although I have not yet had the chance to expand it out to prove it. Also, some of these constraints may be unnecessary; I have not checked.
The expansion I did was according to the following rule:
If b is a binary variable, and c is a continuous one, and 0 <= c <= M, then y=b*c is equivalent to the following:
y <= M*b
y <= c
y >= c - M*(1 - b)
y >= 0

C++ Algorithm to determine all possible join-orders

bI am currently doing some research in databases.
I need to find all possible join orders for a given join graph. The graph is implemented as a adjancence list.
Example:
SELECT * FROM a, b, c, d WHERE a.x = b.x && b.y = c.y && b.z = d.z
gives us the following graph (edges):
(a,b)
(b,c)
(b,d)
and all possible join orders would be (x is the join operator):
((a x b) x c) x d
((a x b) x d) x c
(a x (b x c)) x d
(a x (b x d)) x c
a x ((b x c) x d)
a x ((b x d) x c)
My first idea was running kind of a PRIM algorithm for each vertex. Everytime, we could add different edges to connect a vertex, which is not part of the MST, to the MST, we found another join order. Does this approach work? Is there a simpler solution to get all possible join orders?
Moritz