Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Is it possible to simplify (a+b)xor(c+b)? What is the contribution of b to the final result?
Note that I'm mixing boolean algebra with arithmetic, xor is a bitwise exclusive or on corresponding bits and + is a standard addition on 8 bits, that wraps around when overflown.
a, b, c are unsigned char;
We can use an SMT solver to test our hypothesis that your formula can be simplified. You can head over to http://rise4fun.com:
x = BitVec('x', 8)
y = BitVec('y', 8)
z = BitVec('z', 8)
print simplify((x + z) ^ (y + z))
and the result, anticlimactically, is:
x + z ^ y + z
Which means your formula cannot be further simplified.
(a+b)xor(c+b)
--------------
=((not(a+b))*(c+b))+((a+b)*(not(c+b)))
-----------------------
=((not a)*(not b)*(c+b))+((a+b)*(not c)*(not b))
----
=((not a)(not b)*c) + (a*(not c)(not b))
----
=(not b)((not a)c + a(not c))
----
=(not b)(a xor c)
----
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
There is a problem I am supposed to solve that is normally easy, but it has a catch.
There are 2 types of candy. One type weighs m1 kg and is sold for s1 Euro. A second type weighs m2 kg and is sold for s2 Euro. All numbers are integers.
The question is, which type of candy costs more per kg?
The catch is, you can't use divide operation at all, neither / nor %.
For example, if we have the numbers as m1=2, s1=17, m2=3, and s2=14 then the answer needs to be that the first candies are more expensive as 17/2=8.5 and 14/3=4.(3).
As I am a C++ student, I am restricted to use only that which has been taught so far in the class to determine the more expensive candy. The only thing we learned so far was + - / * % and if statement with else. Also == > < && ||.
Compare X ≡ s1 * m2 with Y ≡ s2 * m1. If X > Y, then s1 / m1 > s2 / m2.
No division is required to do the comparison.
The caveat to this solution is that s1, s2, m1, and m2 should all have the same sign, and m1 and m2 should be non-zero.
Let's assume all the values are positive integers (hence, greater than 0). Consequently m1 * m2 is positive as well. Let z be the number such that:
z + (s1 / m1) = s2 / m2
By multiplying by m1 * m2 on both sides, we get:
z' + (s1 * m2) = s2 * m1 ∵ z' ≡ z × (m1 * m2)
Since z and z' have the same sign, the relational order of s1 / m1 and s1 / m2 is the same as the relational order of s1 * m2 and s2 * m1.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
Let X be a binomial random variable with mean Np and variance Np(1−p).
Show that the ratio X/N also has a binomial distribution with mean p and
variance p(1 − p)/N.
Let r = X/N. Since X has a binomial distribution, r also has the
same distribution. The mean and variance for r can be computed as follows:
Mean , E[r] = E[X/N] = E[X]/N = (N p)/N = p .
Variance , E[(r − E[r])2] = E[(X/N − E[X/N])2] = E[(X − E[X])2]/N2 = N p(1 − p)/N2 = p(1 − p)/N
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Needs more focus Update the question so it focuses on one problem only by editing this post.
Improve this question
How can you define a function to calculate the value of a definite integral in C++? For example to solve the integral of the function x^2 * cos(x)?
Interestingly enough, I ran across this article a little while ago explaining one method for calculating numerical integrals using function pointers.
https://helloacm.com/c-function-to-compute-numerical-integral-using-function-pointers/
For something like x^2 * cos(x):
You would need an overloaded integral function:
double integral(double(*f)(double x), double(*g)(double x, double y), double a, double b, int n)
{
double step = (b - a)/n; // width of rectangle
double area = 0.0;
double y = 0; // height of rectangle
for(int i = 0; i < n; ++i)
{
y = f(a + (i + 0.5) * step) * g(a + (i + 0.5) * step, y);
area += y * step // find the area of the rectangle and add it to the previous area. Effectively summing up the area under the curve.
}
return area;
}
To call:
int main()
{
int x = 3;
int low_end = 0;
int high_end = 2 * M_PI;
int steps = 100;
cout << integral(std::powf, std::cosf, low_end, high_end, steps);
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm having the following code and the number of my channels are 3
IplImage* img_crop_mat = cvLoadImage("....", 1);
...
int b = 0;
uchar* rgb = (uchar*) img_crop_mat->imageData;
I would like to have R, G and B matrices in a loop, skimming the entire image:
for (int y = b; y < height - b; y++)
{
???
for (int x = b; x < width - b; x++)
{
????
}
}
The previous forums regarding my question deal with CvMat but not with pointers as my code.
What are the indexes that I must take into account?
You can use the following macro to access an arbitrary pixel of a 3-channel, 8U-image:
CV_IMAGE_ELEM(myImage, unsigned char, y, x*3 + ChannelOfInterest)
This is an lvalue so you can take and use its value, or you can change the pixel's value.
By default,
ChannelOfInterest = 0, blue
ChannelOfInterest = 1, green
ChannelOfInterest = 2, red
The actual data structure is pretty straightforward, look up the definition of CV_IMAGE_ELEM.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I am trying to solve T(n) = 2T(n/2) + log n
substituted n = 2^k
T(2^k) = 2T(2^(k-1)) + k
T(2^k) = 2^2 T(2^(k-1)) + 2(k-1) + k
after k steps
T(2^k) = 2^k T(1) + 2^(k-1) + 2 * (2^(k-2)) +....+k
So basically I need to sum a term of i*2^i where i = 1 to log n - 1.
And I could not find an easy way to sum these terms. Am I doing something wrong ? Is there any other way to solve this recursion ? would master theorem work her ? if yes than how ?
Thanks.
first you should define a recursive export,say T(1)
then:
since T(2^k) = 2T(2^(k-1)) + k; *
we define g(k) = T(2^k)/2^k;
then * come into:
g(k) = g(k-1) + k/2^k = g(1) + sum(i/2^i); i=2,3,4...k
where g(1) = T(1)/2 = c;
where you could then unfold the sum expression and define it = y;
then unfold the expression of y/2;
y-y/2 is a geometric progression, so youcan solve it
as I worked out, sum = 3/2 - (k+2)/2^k;
so T(n) = 2^k * g(k) = (3/2+c)*n - (2+logn)
Wolfram|Alpha gives a closed form solution:
for a constant c_1 that is fixed by the initial condition.
By the way, log(n)/log(2) = lg(n), where lg is the base two logarithm.