Obtaining an algorithm [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let's say I have a variable a and b. Now I want to find a value for c such that the fraction a / c is a positive integer, and where c is as close to b as possible.
For example: a = 100 and b = 30.
In this case I want c to be 25; because a / c is an integer, and c is as close as b for which this holds.
Any ideas how I can program a function in C++ which does exactly this?

Find the factors of a. (search web for methods)
Scan resulting list for minimum difference vs b.

Is this a homework assignment? Either way, think about how you would solve this problem without writing any code. A good algorithm comes from a good design. Break the problem down into pieces and walk through some more examples. For example, how would you solve the problem of determining whether the division results in an integer value? Hint: There is a different operator you could use as opposed to division to achieve this easily. Now, how would you solve the problem of determining what number to start at for c in the algorithm? Do not write any code until you have the pseudocode figured out.

Related

Linear Programming - deriving the Dual of the Primal [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 3 years ago.
Improve this question
I've the following linear programming problem:
This is the LP representation of the uncapacitated facility location problem.
This is the dual representation of this problem:
My question is how to derive the dual representation from the primal in this problem.
I tried to follow the step by step tutorial by SĀ“ebastien Lahaie found here
but it won't work because of the summation in the constraint equations.
I'm looking for a pure mechanical way (with no intuition) to do that.
The step by step tutorial actually works quite fine. It looks like I've lost a sign somewhere, but the fourth step will produce:
Where did I get it? I just multiplied the constraint by and added it to the objective clause for every j, then I did the same with the other constraint.
Now I just group the terms by the primal variables. What I get is basically:
note that I use i implicitly for the set F and j for the set D. And sorry for the bad formatting.

Calculating large numbers in C++ without external libraries [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I need to write a program that will perform operations on float numbers higher than 10^100.
I can't use any arbitrary precision mathematics libraries that are not included in GCC package by default.
I have NO idea how how to go about it.
Can you point me in the right direction?
You can create a class that can store larger numbers. 12345678 equals to 1234 * 10e4 + 5678.
For large numbers I use string buffers and do manual computation on it. It is much overhead and slow but you get infinite precision.

find combinations of numbers stored in an array and store those combinations in another array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an array of n size and I want to find combinations of k size. I don't just want to print those combinations but want to store those combinations in other arrays or any container. I read this creating all possible k combinations of n items in C++ but couldn't succeed in storing the combinations in other arrays. It is because I want to perform operations on those combinations. I'm seeking for any hints regarding this.
Thanks in advance.
Lacking a specific reason to choose something else, you probably want to store the results in a vector.
You can pre-compute the number of results quite easily -- N items taken K at a time will produce N!/K!(N-K)! total combinations.
At the risk of sounding condescending (which I don't intend) I'll point out that N! grows very quickly, so if the difference between N and K is very large, the result may easily be quite a bit larger than most computers can reasonably store.

Generate prime factors of a number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to write a function that given an Int greater than one gives a non decreasing list made of the prime factors (with repetition) of that number
Example: n = 12, the output should be [2,2,3]
I don't know where to start.
There are of course well know algorithms for what you want to do, so simple google search would really solve that.
However, I'd like to show you a simple thinking process that might be helpful in the future.
Since the factors have to appear in the ascending order, you might:
Start with the lowest prime (2).
Check if the number can be divided by it. If it can, do it and go back to 1.
If not, replace 2 with a next prime and go back to 2.
Now, it's obvious that the biggest prime you will ever check is the number you've started with. However, the basic multiplication axiom states that if a number can be divided by a:
n / a = b
Then it can also be divided by b! You can use that fact to further narrow the checking range, but I'll leave it to you to figure (or google) the upper bound.
Actual implementation is of course a part of your homework and thus supplying code wouldn't be a wise idea here. However, I don't think that stuff such as next_prime will be hard for you.

Bitwise operations vs. logical operations in C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In C++ are the following statements when comparing two integer values for equality the same? If not, why?
if(a == b)
...do
if(!(a ^ b))
...do
For integer values, yes. Obviously the xor operator will return not-zero if there are any bit differences between A and B, and ! will invert that. For integer data types, the conditions are equivalent.
For floating point values, because of how you can perform two mathematical operations that "should" give the same result, but they may be represented differently as floats, you should not use either of these to compare floats for equality, you should check whether they are the same to within a small margin of error (an "epsilon").
For pointers...I have no idea why you would want to do this to pointers. But if you really want to do it, then yes, they are the same.
However, there is no reason to do this. With optimizations enabled, they will compile to the same code, without, the first will likely be faster. Why would you use the less-clear !(a^b)?
The two comparisons are equivalent: a^b is 0 if and only if a==b, so !(a^b) is true if and only if a and b have the same value.
Whether you can call them "the same" depends on what you mean by two different operations being "same." They probably will not be compiled into the same code, and a==b is definitely easier to read.