The point is to "numerically" estimate the ten numbers whose sum is greatest given that the sum of their squares is less than one.
q(a,b,c,d,e,f,g,h,i,j) = a+b+c+d+e+f+g+h+i+j
maximum(q(a,b,c,d,e,f,g,h,i,j) for a in 0:0.0001:1, b in 0:0.0001:1, c in 0:0.0001:1, d in 0:0.0001:1, e in 0:0.0001:1, f in 0:0.0001:1, g in 0:0.0001:1, h in 0:0.0001:1, i in 0:0.0001:1, j in 0:0.0001:1 if a^2+b^2+c^2+d^2+e^2+f^2+g^2+h^2+i^2+j^2 < 1)
My solution is of a brute force form which seemed fine for a smaller problem - the sum of only two such numbers - but is painful for larger n.
q(a,b) = a+b
maximum([q(a,b) for a in 0:0.0001:1,b in 0:0.0001:1 if a^2 + b^2 < 1])
Related
I am trying to formulate a constrain for my math model. the constrain goal is:
if A = 1 and B = 1 then C <= D
otherwise (A or B or both are 0) there is no constraint.
A and B are binary variables. C and D are integer numbers.
so far I was able to come up with this equation:
M(A - 1) - (B - 1) + C <= D (M is a big big number)
this formulation does not hold when A = 1 and B = 0
You could do this in two steps, first introduce a variable X representing logical and of A and B.
X >= A + B - 1
X <= A
X <= B
Then use X to express the inequality:
C - M(1-X) <= D
Maths Puzzle
A = 10 (COST)
B = 20 (Sell Price)
C = 15% (Fee)
D = Tax at 20% or 1/6th if you are taking it away
E = Margin
B x C = 3.60
B / 6 in Reverse = 3.33
E = B - A - (B x C) - (B / 6) = 3.07
Ok The above works correct when i i provide B Sell Price
I want a formula that will Give me E, If i say E = 3.07 It says B = 20
How would i do that, the math
Any boffins can help
Thanks
First:
You did not use D: I suppose instead of B / 6, it should read B x D, where D can be either 20% or 16,666...%
There is a mistake: B x C is not 3.60 for the given values (B=20, C=15%), it is 3.
This is a matter of mathematical deduction:
Given the equation:
E = B - A - (B x C) - (B x D)
Add A to both sides:
E + A = B - (B x C) - (B x D)
Isolate B:
E + A = B x (1 - C - D)
Divide both sides by (1 - C - D). Condition: C + D cannot equal 100%
(E + A) / (1 - C - D) = B
So there is your formula for calculating B. Take note of the condition: this only holds true when C + D is not equal to 100%.
Consider I have a timer that returns a uint32_t value (representing a number of ticks), always counts upwards, and wraps to 0 after reaching UINT32_MAX.
Suppose I need to take an elapsed time from time a to time b, and I don't know how high the timer might be initially and whether it will wrap between a and b. Both a and b are type uint32_t and get assigned to the timer's return value.
Is it a correct statement that we can take (uint32_t)(b-a) to get the elapsed time so long as no more than UINT32_MAX ticks have elapsed — and will it be correct even if the timer wrapped once? What is the proof for this?
Let N = 232. Let A and B be the timestamps of the start and end before wrapping to the [0, N) range, and assume A ≤ B < A + N. Then a = A % N and b = B % N. We are interested in computing the duration D = B - A.
When a ≤ b, it is trivial that D = B - A = b - a.
What about when a > b? Then a ≤ b + N and it must be that D = B - A = b + N - a.
But b - a is of course congruent b + N - a modulo N. Since addition and subtraction between std::uint32_t is all modulo N, you can safely compute your answer as D = b - a. The subtraction operator between two std::uint32_t values is already a std::uint32_t, so there's no reason to specify a cast as in (std::uint32_t)(b - a).
I cant seem to find any kind of answer to this, but if I have an equation like the square root of (X^2-4n) where 4n is a constant, how could I set x so the equation gives a whole number.
I know setting x to n+1 works, but I'm looking for an algorithm that would generate all solutions.
So, the problem is to find all pairs of integers (x, m) such that:
sqrt(x^2 - 4n) = m
We have:
x^2 - 4n = m^2
or
x^2 - mˆ2 = 4n
so
(x + m)(x - m) = 4n
Now, 2 divides 4n and so it must divide (x+m) or (x-m). But if it divides any of them it will divide the other too. Thus a := (x+m)/2 and b := (x-m)/2 are both integers. Therefore
a*b = n
So, it is just a matter of factoring n as a*b in all possible ways and recover x and m from the equations above:
x = a + b.
m = a - b.
Your solution x = n+1 corresponds to the trivial factorization n = n*1 where a=n and b=1.
UPDATE
Here is an algorithm that prints all pairs (x, m)
[Initialize] a := n.
[Check] if n % a = 0 then
b := n / a.
print(a + b), print(a - b)
[Decrement] a := a - 1.
[End?] if a * a > n go to Step 2.
If I have a graph structure that looks like the following
a level-1
b c level-2
c d e level-3
e f g h level-4
...... level-n
a points to b and c
b points to c and d
c points to d and e
and so on
how can i calculate the n from the size(number of existing nodes) of the graph/tree?
The number of nodes present if the height is h is given by
1 + 2 + 3 + ... + h = h(h + 1) / 2
This means that one simple option would be to take the total number of nodes n and do a simple binary search to find the right value of h that such that h(h + 1) / 2 = n.
Alternatively, since n = h(h + 1) / 2, you can note that
n = h(h + 1) / 2
2n = h2 + h
0 = h2 + h - 2n
Now you have a quadratic equation (in h) that you can solve to directly get back the value of h. The solution is
h = (-1 ± √(1 + 8n)) / 2
If you take the minus branch, you'll get back a negative number, so you should take the positive branch and compute
(-1 + √(1 + 8n)) / 2
to directly get back h.
Hope this helps!