Permutation

Hey guys messing around with a stat calculator and Im having an issue or how to approach my permutation function. What I want to do is have the user enter in two long values N and X so on paper this formula would look like this
N!/(N-X)! I think? I'm just having an issue figuring out how I could write this into a loop to do this equation, any help or guidance would be appreciated! Thanks.
Last edited on
Can you show us what you have so far (and remember to use code tags)?

What you could do is have a function computes the number of permutations as given by your formula. You will also likely want a function that calculates the factorial of a number. In your main method you can have a loop that iterates until some condition is met (a simple one would be that the user enters a negative value for N or X).

Hope that helps.
If you're going to do this the simple way, which is compute N! and compute (N-X)! then divide them, then you might want to take care of how to operate with long numbers.

I mean 10!=3628800 is already bigger than max int, these things grows crazy, 20!=2432902008176640000, I believe this is bigger than max long. The biggest number type I've seen in C++ is int_64 which can hold a number up to 2^64, you might want to check it out.

I've seen one of the solution of these large number is the store them digits by digits in an array, and define functions to do plus, div, multiply, minus yourself. It's called High Precision Arithmetic, I believe.
Last edited on
Topic archived. No new replies allowed.