How to get common factors with c++? [closed] - c++

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 8 years ago.
Improve this question
I'm having trouble starting with the code i have already read some other questions here but im still stuck on how am i even gonna start this one :/.
So i made 3 inputs to fill the expression (ax^2+bx+c)
.......
cout<<"This Program runs onlyy the expression (ax^2+bx+c)"<<endl;
cout<<"\nEnter The first Integer[a]";
cin>>ina;
cout<<"Enter the second Integer[b]";
cin>>inb;
cout<<"Enter The third Integer[c]";
cin>>inc;
cout<<"Your Expression is"<<endl;
cout<<ina<<"x^2 + "<<inb<<"x + "<<inc<<endl;
........
Now how would i make my program show its common factor..
example is (x^2+4x+3) how can i make it show that its common factor is (x+3) and (x+1)?

Well how to factor a polynomial has very little to do with C++.
If Ax^2 + Bx + C can be expressed as A*(x - x1)*(x - x2) (and it can always be so expressed), then clearly plugging the value x1 in for x makes the original equation zero, since the first term of (x1 - x1)*(x1 - x2) is then 0. And ditto for x2. And conversely, if you have two values that make the equation zero, then they are x1 and x2.
There is a standard formula for solving quadratic equations. Computing that in a program one should be aware that subtracting a number from a roughly equal size number can produce a less precise result. So how that formula is expressed in the code, can make a difference wrt. to the accuracy of the results. You can find more information about that on the net. Including examples for quadratic equation formula.

Related

Understanding the algorithm for Newton Fractals [closed]

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
I am having trouble understanding the equation for newton fractals. My professor gave a a general format to follow, but it is still coming out a bit confusing when I am reading over it. I was wondering if anyone can help clarify or simplify what he is talking about. So the parts I am most confused with is
Whatis the 'a'?
What are the conditions for the loop and why would I be resetting counter if it is my condition to leave the loop?
Is (x, y) the (real, imaginary) part of the complex number?
This is the picture of the algorithm he gave: https://i.stack.imgur.com/UJH41.jpg
What is the 'a'?
A complex number that generalizes the formula. Choose 1 for a Newton iteration.
What are the conditions for the loop and why would I be resetting counter if it is my condition to leave the loop?
You seem to miss the fact that you are trying to generate an image, setting the color of each pixel.
So, you have two nested loops that scan the pixels and, for each pixel, a loop that iterates the formula. That's the one that resets the counter and the initial value of z each time.
The condition of that loop are clearly expressed after this statement:
The iteration should continue while each of the following conditions is true.
Basically they limit the number of iterations and extablish a minimum tolerance. Note that z1, z2 and z3 are the roots of the third degree polynomial.
Is (x, y) the (real, imaginary) part of the complex number?
Not quite. The values of x and y are the coordinates of the pixels in the resulting image and are used to generate the real and imaginary part of the intitial value of z.
More details are probably written in the previous page, that you haven't posted.

Solving system of equation modulo 2 [closed]

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 3 years ago.
Improve this question
I make a program in C++, that factorizes natural numbers. The only problem is to create a function that does the following:
input: it receives a matrix vector< vector< int> > M.
output: it gives a vector v so that a result of multiplying v and M is a vector that all its coordinates are equal to 0.
Everything must be modulo 2, so coefficients of M and v consists only 0s and 1s
Schould I use a Gauss elimination method? If so, how do this? The problem that implementations I saw on the net don't use vectors and the vectors are necessary in my main program
I would be grateful if someone helped me.
Regards
This is an interesting problem. The steps to be taken can be found in this exercise. link.
The tricky part is to understand that there are always only a finite number of solutions, i.e, only the trivial solution exists or non-trivial solutions exist.
Once you finish the row reduction steps and if non-trivial solutions exist, there is always going to be at least one independent variable (it can take any value 0/1) and the rest of the variables depend on the independent variables.

Most frequent value in dataset (with variation) [closed]

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 7 years ago.
Improve this question
This is a part programming, part statistical math question.
I have a dataset where I want to get the most frequent number (mode), the problem is that I am dealing with values with slight variation.
So normally {1,2,50,50,90} the most frequent number would be 50
But in my case the numbers look like this:
{1,2,49,50,51,90} but the result is still 50
So my question is how can I efficiently calculate this number and is there a statistical term for this number?
Some pseudo code:
Float items.val[] = {1,2,49,50,51,90};
Float threshold = 4;
For (item in items) {
For (subitem in items){
Float dist=Distance(time,subitem)
If (dist < threshold){
item.dist += dist
}
}
}
Output=Sort(item.dist)[0]
There are various ways to go about this.
(1) the most careful, exact way is to assume a probabilistic model for the observed values, and look for the mode (as the expected value or most probable or some other criterion) of the inferred values. I am going to guess this is far too much work in this case, although given unlimited time I would certainly want to approach it that way.
(2) construct a histogram, and look for the bin which has the greatest density (with density = (#items in bin)/(width of bin)). This doesn't necessarily yield a single value.
(3) fit a parametric distribution to the observed values, and report the mode of the fitted distribution.
You might get more traction for this question at stats.stackexchange.com. Good luck and have fun.
EDIT: After looking at your example code, I see it is not too different from (2) above. It seems like a reasonable and workable approach.

Faster edit distance algorithm [closed]

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 8 years ago.
Improve this question
Problem: I know the trivial edit distance DP formulation and computation in O(mn) for 2 strings of size n and m respectively. But I recently came to know that if we only need to calculate the minimum value of edit distance f and it is bounded |f|<=s, then we can calculate it in O(min(m,n) + s^2) or O(s*min(m,n)) [wikipedia] time.
Please explain the dp formulation behind it if this is DP based or explain the algorithm .
Look at the improved algorithm section of the
link: http://en.wikipedia.org/wiki/Edit_distance .
one more link about improved UKKONEN'S algorithm http://www.berghel.net/publications/asm/asm.php
Thanks in advance.
You can calculate edit distance in O(min(n, m) * s) time use next simple idea:
Consider the i-th string in DP-table.
So, if we know, that answer <= s, then we are intersted in cells with coordinates (i, i - s), (i, i - s + 1), ... ,(i, i + s). Because in other cells answer strictly greater than s.
For example, suppose we know, that edit distance between "abacaba" and "baadba" less than 3.
So, we can skip red cells, because they have value more than s.
Asymptotic of the algorithm O(min(n, m) * s) because we calculate s cells to the left and right of the main diagonal.

A computing trick to calculate for e.g number of boxes required to place N objects given each box can hold M objects? [closed]

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 9 years ago.
Improve this question
As a part of a recent topcoder SRM problem we had to compute number of buses "B" required to carry "N" people given that each bus has "S" seats.
What is the smartest way to compute this in C++?
The obvious way is to do:
if(N%S==0){B=N/S;}
else{ B=N/S + 1;}
^ ALL VARIABLES ARE INTEGERS, N AND S ASSIGNED APPROPRIATE VALUES
However I cant understand the logic behind the following code which is one particular topcoder user's solution which I was checking out;
B = (N + (S-1))/S;
How does this work?
The code
B = (N + (S-1))/S;
is a common rounding trick. We know that in integer division, the remainder is cut-off, essentially what floor does. In this case, we enforce a ceil operation by adding S-1 first.
This is similiar to the common way of rounding floating point numbers:
n = floor(n + 0.5);