[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added permutations and combinations functions (with tests), addressing #99 #128

Merged
merged 7 commits into from
Jan 6, 2014
Merged

Conversation

daniel-levin
Copy link
Contributor

The title says it all. If I have omitted anything, or done anything incorrectly, please let me know

josdejong added a commit that referenced this pull request Jan 6, 2014
Added permutations and combinations functions (with tests), addressing #99
@josdejong josdejong merged commit 574dabe into josdejong:develop Jan 6, 2014
@josdejong
Copy link
Owner

Nice work, thanks Daniel!

@josdejong
Copy link
Owner

I was wondering, shouldn't combinations throw an error when k > x, like permutations does?

@daniel-levin
Copy link
Contributor Author

Yes, you are correct, it should. I'm going to grab a cup of coffee and then
get to work on adding the required code.
On 06 Jan 2014 11:38 AM, "Jos de Jong" notifications@github.com wrote:

I was wondering, shouldn't combinations throw an error when k > x, like
permutations does?


Reply to this email directly or view it on GitHubhttps://github.com//pull/128#issuecomment-31636560
.

@josdejong
Copy link
Owner

Ok great. I just did a commit with some docs added.

I would also not mind renaming x to n, I don't know what you prefer. n denotes integer and x a float? (In that case we should also rename x in factorial)

@josdejong
Copy link
Owner

One other thought: It would be cool to have BigNumber support for these functions too. Should be easy: factorial already supports bignumber, so all you have to do is replace operators /, -, and * with math.divide, math.subtract, and math.multiply, and add some unit tests.

@daniel-levin
Copy link
Contributor Author

I've changed x to n, and added the k > n error, but I'm not going to submit a new PR yet.

I'm going to add BigNumber support. It makes sense because computing the factorial of a number much, much smaller than the upper limit of an integer - like 5 or 6 orders of magnitude smaller - will produce a number much too large to be represented by a 32-bit integer.

That said, the factorial function gets extremely slow with large input. Perhaps a medium-term goal could be to add memoization to the factorial function?

if (!isInteger(x) || x < 0) {
throw new TypeError('Positive integer value expected in function combinations');
}
return parseInt(math.factorial(x) / (math.factorial(k) * math.factorial(x-k)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think, instead of factorial calculation, it is better to calculate multiplication:
x * (x - 1) * (x - 2) * (x - 3) * .... * (x - k + 1) atleast
also, what is a purpose of parseInt here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseInt is the most convenient way of assuring that the JS runtime won't convert the result of the division operation to a floating point value. In spite of the analytical guarantee that the final value will be integral, floating point operations might induce an error. I added it because I got a value of something like 15.00000001, which is not equal to 15.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daniel-levin , so you should use "Math.floor"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is true. I've made the relevant changes in my own branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants