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/

Tuesday 5 June 2007

Fast searching for strings in files.

FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
[/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
strings [[drive:][path]filename[ ...]]

/B Matches pattern if at the beginning of a line.
/E Matches pattern if at the end of a line.
/L Uses search strings literally.
/R Uses search strings as regular expressions.
/S Searches for matching files in the current directory and all subdirectories.
/I Specifies that the search is not to be case-sensitive.
/X Prints lines that match exactly.
/V Prints only lines that do not contain a match.
/N Prints the line number before each line that matches.
/M Prints only the filename if a file contains a match.
/O Prints character offset before each matching line.
/P Skip files with non-printable characters.
/OFF[LINE] Do not skip files with offline attribute set.
/A:attr Specifies color attribute with two hex digits. See "color /?"
/F:file Reads file list from the specified file(/ stands for console).
/C:string Uses specified string as a literal search string.
/G:file Gets search strings from the specified file(/ stands for console).
/D:dir Search a semicolon delimited list of directories
strings Text to be searched for.
[drive:][path]filename
Specifies a file or files to search.