Generate prime factors of a number [closed] - list

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.

Related

Generating relative Prime number of a user defined prime number? [closed]

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.

Obtaining an algorithm [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
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.

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.

C++ Algorithm Prediction, need to predict 3 numbers [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
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.

Any good recursive tutorials? Python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
Wondering if anyone could point me towards a good recursion tutorial. I am a bit rusty on it as I learned about it in my Data Structures class first semester. Would like to brush up on my recursion...any help?
Consider this.
More seriously…
Recursion is a way of solving problems that have a clearly defined base case (or cases, btu I'm keeping it simple here.)
For examples, the commonly cited factorial problem is a great one.
What does factorial do? Let's see some examples:
factorial(0) = 1
factorial(1) = 1
factorial(2) = 2
factorial(3) = 6
factorial(4) = 24
The factorial of a number is that number multiplied by the factorial of the number that comes before it, unless (now, this is the base case) the number is 0. The factorial of 0 is 1. (You can't take the factorial of a negative number; only positive integers.)
So we have our clearly defined base case. And we know what to do with numbers that aren't our base case (we multiply them times the factorial of the number one less than it.) We're ready to write our function.
def factorial(x):
if x == 0: # this is our base case
return 1 # and this is what we do when we see it
else: # this is what we do with all other numbers
return x * factorial(x-1)
So you
Clearly define your base case.
Find a way to reduce your problem from a non-base case to the base case.
Formally express that in a function that (when it's simple!) looks like
function:
if base case:
this
else:
something + function(something closer to the base case)
If you want something more advanced, Google's got a lot of info.
I would highly recommend watching MIT's intro to programming course.
Lecture 4 talks about recursion.