Showing posts with label functions. Show all posts
Showing posts with label functions. Show all posts

Sunday, June 24, 2007

Greatest Common Divisor

Although quite simple, this is one of my favourite new features for 0.8. One will now be able to calculate the greatest common divisor of an unlimited amount of numbers. This way, we can now write

gcd( 675; 633312; -57618; 243 )
27

This is particularly useful if you are adding fractions by hand and they all have different denominators, but has certainly a whole bunch of other applications.

Probability theory

After combinatorics, SpeedCrunch has just been enhanced with its first probability support. This was indeed my favourite mathematics subject when I was a student, so I decided to implement some discrete distributions. So for now we've got three available: Binomial, Hypergeometric and Poisson.

Available for each distribution are functions for probability mass, cumulative distribution, mean and variance. If you usually need some others or find them useful even if you don't use them, please tell us.

I've got many more further plans on these topics for the next SpeedCrunch releases, but I'll leave it like this for 0.8. There are still some open higher priority issues and not much time left to 0.8alpha, so let's focus on bug fixing.

Thursday, May 24, 2007

Combinatorics

So now SpeedCrunch has got combinatorics support. Ariya had already implemented the famous nCr function, so I decided to code its cousin nPr.
I'd realized a nice optimization in Ariya's code. The general definition of nCr is given by
nCr = n! / ((n-r)! r!)
n, r ∈ N0 , r ≤ n (applies all bellow)

But we can indeed reduce very much the number of integer multiplications with the derivation
r ∈ {0, n} ⇒ nCr = 1
r = 1 ⇒ nCr = n
r ∈ ]0, n/2] \ {1} ⇒ nCr = (r+1)n / (n-r)!
r ∈ ]n/2, n[ \ {1} ⇒ nCr = (n-r+1)n / r!

Note that (r+1)n, for instance, is a modern factorial notation equivalent to (n)(n-1)...(r+1). I didn't know about this myself until now. This fasts computation a lot, else the very same calculations would be uselessly repeated.
In the very same way, I found a nice simplification for nPr. The general definition of nPr is given by
nPr = n! / (n-r)!

And again we can do some magic
r = 0 ⇒ nPr = 1
r = 1 ⇒ nPr = n
r = n ⇒ nPr = n!
r ∈ ]1, n[ ⇒ nPr = (n-r+1)n

I spent some minutes getting to this conclusion and then I read it somewhere right after. First I felt a bit frustrated, then I smiled. It's funny to develop this program, it's funny to play maths.

I then realized my old Casio FX-880P can compute up to 69! and SpeedCrunch currently up to 96!, that's good enough (150-digit long integer). Nevertheless, when the user tries to calculate factorials, combinations or permutations with huge parameters the program freezes (even by the calc-as-you-type feature). Maybe it's really a good idea to define an upper limit for those functions input.