DennisMitchell / jellylanguage

Jelly is a recreational programming language inspired by J.
MIT License
862 stars 47 forks source link

faster combination if y is integer #7

Closed kckennylau closed 8 years ago

DennisMitchell commented 8 years ago

That uses floating point arithmetic, making it inaccurate for large values of n.

kckennylau commented 8 years ago

Not really. This formula can guarantee that if left is integer, then all the intermediate values are integers.

DennisMitchell commented 8 years ago

Ah, I see what your intentions were. There are two problems though.

  1. / is always floating point division in Python 3, even if the result is exact.
  2. Something is off by one. 10C1 should return 10, not 1.
kckennylau commented 8 years ago
  1. Then condition on left to see if we use / or //
  2. Ok I'll fix that (or you'll fix that?)
kckennylau commented 8 years ago

I forgot to add one to the second argument of range().

kckennylau commented 8 years ago

@DennisMitchell Is it ok now?

DennisMitchell commented 8 years ago

Almost. To match the behavior of the previous code, it should return 0 when the right argument is negative. The case of a negative left argument can be considered undefined behavior for now.

kckennylau commented 8 years ago

But negative left argument is really defined... They are also useful in binomial expansions.

DennisMitchell commented 8 years ago

It is. If you want to implement it correctly, go ahead. I just meant that my code doesn't return anything sensible right now, so there's no reason your code should match its behavior.

kckennylau commented 8 years ago

Thanks!

DennisMitchell commented 8 years ago

Thank you for the contribution!

I made one more functional change to the code, so that nCr(2**256, 2**256-1) is also computed instantly.