Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
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
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I'm creating a program in C and I need to use relative prime numbers in sequence using an algorithm so that the user can select the first number in the sequence.
So far I have managed to create a function that creates relative prime numbers based on one or more inputted by a user but not one that finds the next smallest relative prime.
Either that or a way to produce the smallest relative prime number to a user defined number would be ideal.
Any ideas?
Also, I cannot get gcd to work so I created my own. Do i have to include a specific library other than math.h and stdio.h?
If you want to find the next smallest relative prime, then I think you need to loop from the user's inputted number (e.g. if users input 3, then you need to loop from 4), and then check whether that number is relatively prime.
To check whether two numbers are relatively prime, you can use gcd, and one very famous algorithm to do this is to use Euclid's algorithm. You don't need to include a specific library, it's basically just looping and doing modulo. Take a look at this link.
Related
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.
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.
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 go straight to the facts.
I am studying for my own interest an eventual prediction algorithm for some unexistent lottery.
Let's say they roll out 3 numbers every day.
And those numbers are in range from 0 to 50.
I am asking, what would be the best approach to try to predict next 3 numbers knowing
all previous historic ones?
1. What i have
I have a list of 3 numbers from a range of {0,50} (integers)
<x0,y0,z0>
<x1,y1,z1>
<x2,y2,z2>
<x3,y3,z3>
Those numbers represent winning values of lottery.
2. What i need
I need to predict next 3 lottery numbers(possible WINNERS) by taking previous numbers into consideration
The order of the predicted numbers doesn't mater. It might be 1,2,3 or 3,2,1.
3. Question
Which approach / algorithm should i choose and why?
Super thanks for any help!
If the numbers you roll out are random, there is no way to make a prediction, as the next numbers are not linked in any way to the previous one. The most you can do is a guessing algorithm.
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.
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 am trying to print numbers column by column.
I am trying to run 50 simulations that will produce a list of numbers. For every simulation, the length of such list is unknown.
For every simulation I want to print the numbers down a column.
When the next simulation starts I want the program to go back up to the top of the file and start printing the list down without entering into the previous list.
All I can find are ways to print the numbers by row, but I will not know the numbers down the row unless I do all the simulations.
Any advice would be much appreciated!
You have a few choices:
Write each sim result to a separate file and collate later;
Hold all results in memory until finished;
After each simulation, read the existing file, and output it again with the new results added in;
Knowing the number of simulations you intend to produce, write results in binary and leave padding for those that are not yet done.
I would probably go with 1.
By "goes back to the top of the file", do you actually mean rewind the file pointer to access memory starting at the beginning of the file? I will assume that's what you meant. Since you want the first input after the last output, then you can run your simulations and print them in reverse order, or simply call the appropriate library functions as you need them.