Mathematics

Calculating number of combinations

The well-known formula for calculating the number of combinations of n objects with r objects taken a t a time is

but this requires calculating three factorials which may be expensive and may cause overflow. A better way is to use the other definition

and doing multiplication and division alternatively. An example is the C++ code below:

int nCr (int n, int r)
{
   int ncr = n;
   int k = 1;
   int r1 = n - r;

   // Handle special cases
   if (r1 < 0) { return 0;}    // Invalid value
   if (r1 < r) { r = r1;}      // nCr = nC(n-r)
   if (r == 0) { return 1;}    // nC0 = 1

   // To avoid integer overflow, divide as early as possible
   for (int k = 2; k <= r; ++k) {
     ncr *= --n;
     ncr /= k;
   }
   return ncr;
}

C/C++
Mathematics

Comments (0)

Permalink

Circle from three points

The problem

Given three points , and . Find the center and radius of the circle passing through it.

Discussion

Three non-collinear points represent a unique circle on a unique plane. It is easy to construct the circle from 3 points by drawing perpendicular bisectors of any two chords. If the 3 points are not collinear, the bisectors should meet at the center of the circle.

Using vectors, it is possible to devise an algorithm to do this. However, an easier method will be substituting the three points into the general equation of the circle

and solving for , and . Center is and the radius is
.

Starightforward algorithm

This can be solved by substituting the three values in the above equation and solving the set of three simultaneous equations.

The solution to the above set of simultaneous equations yields that the equation of the circle passing through , and is given by equating the value of the following determinanat to zero:

A simpler algorithm

A modified algorithm is given below:

The task will be much simpler if we apply a linear transformation so that one of the points (say ) is , and now the constant term vanishes, and now there are only two variables and two unknowns. We can transform the co-ordinates back after solving.

So, transform , , to

and now the equations are

where

Now, we can solve for g and f here.

Finally, applying the tranformations back, the center is .

The following C function illustrates this algorithm.

#include <math.h>
#define TRUE 1
#define FALSE 0

int  /* Returns TRUE if it is a valid circle, FALSE if the points are collinear */
solve_3_points_circle (
   double  x1, double  y1, /* Point 1 */
   double  x2, double  y2, /* Point 2 */
   double  x3, double  y3, /* Point 3 */
   double* cx, double* cy, /* Center  */
   double* r               /* Radius  */
)
{
   double z1, z2, d;

   /* Apply the transforms */
   x1 -= x3;
   y1 -= y3;
   x2 -= x3;
   y2 -= y3;

   Temporary variables to avoid some computations */
   z1 = x1 * x1 + y1 * y1;
   z2 = x2 * x2 + y2 * y2;
   d = 2.0 * (x1 * y2 - x2 * y1);
   if (fabs(d) < 0.00000001) {
      return FALSE;
   }

   /* Calculate the center of the transformed circle */
   *cx = (y2 * z1 - y1 * z2)/d;
   *cy = (x1 * z2 - x2 * z1)/d;
   r = sqrt(*cx * *cx + *cy * *cy);

   /* Apply the transforms back */
   *cx += x3;
   *cy += y3;
   return TRUE;
}

Algorithm given by Plastock and Kalley

Plastock and Kalley gives the following formulae for solving this problem:

The center and the radius of a circle passing through the points ,
and , are given by:

where

These formulae can be used to find the parameters of the circle.

Solution by following the geometric construction

The line passing through and is

where

The perpendicular bisector of this line passes through and has a slope of .

So, the perpendicular bisector is

Similarly, the perpendicular bisector of the side passing through and is

where

Now, the center of the circle is the point of intersection of these two perpendicular bisectors. So, equating the right hand sides of the above two equations, and solving for x,

Algorithms
C/C++
Mathematics
Programming

Comments (0)

Permalink

Limeric: Another formula

By Chris Boyd:

Four plus the difference between
The factorial of six and the mean
   Of twelve squared and four
   Hundred three (plus one more)
Equals double the square of fifteen.

Limericks
Mathematics

Comments (1)

Permalink

Limerick: Is circle round?

A conjecture both deep and profound
Is whether the circle is round;
   In a paper by Erdo”s,
   Written in Kurdish,
A counterexample is found.

Limericks
Mathematics

Comments (2)

Permalink

Limerick: Cube of infinity

A graduate student from Trinity
Computed the cube of infinity;
   But it gave him the fidgets
   To write down all those digits,
So he dropped math and took up divinity.

Limericks
Mathematics

Comments (0)

Permalink

Limerick: Modular arithmetic and climate

In arctic and tropical climes,
The integers, addition, and times,
   taken (mod p) will yield
   A full finite field,
As p ranges over the primes.

Limericks
Mathematics

Comments (0)

Permalink

Limerick: A formula

This poem was written by John Saxon (an author of math textbooks).

A Dozen, a Gross and a Score,
plus three times the square root of four,
   divided by seven,
   plus five times eleven,
equals nine squared and not a bit more.

Limericks
Mathematics

Comments (0)

Permalink

Limerick: Peeling a Moebius strip

A burleycque dancer, a pip
Named Virginia, could peel in a zip;
   But she read science fiction
   And died of constriction
Attempting a Moebius strip.

Limericks
Mathematics

Comments (0)

Permalink

Limerick: e

If (1+x) (real close to 1)
Is raised to the power of 1
   Over x, you will find
   Here’s the value defined:
2.718281…

Limericks
Mathematics
e

Comments (0)

Permalink

Limerick: Fermat’s Last Theorem

A challenge for many long ages
Had baffled the savants and sages.
   Yet at last came the light:
   Seems old Fermat was right–
To the margin add 200 pages.

Fermat
Limericks
Mathematics

Comments (0)

Permalink