Wednesday 13 June 2007

TopCoder

Distancs between 2 points in space:

double distance3d(int x1, int y1, int z1, int x2, int y2, int z2)
{
int x = x1 - x2, y = y1 - y2, z = z1 - z2;
return Math.Sqrt(x * x + y * y + z * z);
}

double distance2d(int x1, int y1, int x2, int y2)
{
int x = x1 - x2, y = y1 - y2;
return Math.Sqrt(x * x + y * y);
}

Triangle area:

double triangleArea(double a, double b, double c)
{
double eps = 1.0e-9;
double s = (a + b + c) / 2.0;
if (s - a <= 0 || s - b <= 0 || s - c <= 0 ) return 0;
if (Math.Abs(a + b - c) < eps || Math.Abs(b + c - a) < eps || Math.Abs(a + c - b) < eps) return 0;
return Math.Sqrt(s * (s - a) * (s - b) * (s - c));
}

The Fibonacci number on position pos
double fibonacci(double pos)
{
return (Math.Pow(1 + Math.Sqrt(5),
pos) - Math.Pow(1 - Math.Sqrt(5), pos)) / (Math.Pow(2, pos) * Math.Sqrt(5));
}


More: http://mathworld.wolfram.com/

No comments: