Archive for February, 2008

Prim’s Algorithm

In the Graph Theory, Prim’s algorithm creates an optimal spanning tree. I will show here my implementation of this algorithm. It is used in the Graph Lib that I showed you earlier. I don’t pretend that it is done the best way, but it’s working completely. (more…)

Filled under C#. No Comments.

Graph Lib

As I was preparing for my math exams (discrete math) I decided to create a simple library for graphs. If you are interested in the Graph Theory, you may find this lib useful. It is very simple now and it has a little features. It is beta, so if you find any problems with it, please contact me. (more…)

Filled under C#. No Comments.

Implicit Operator

As I was creating my Fraction class, I needed “an option” for automatic cast. Let’s see here what the problem was. When I want to represent an integer as a fraction, I have to do it like this:

Fraction f = new Fraction(5);

But it is a little boring to type every time new Fraction(…). I wondered if there is a way when I type something like this

Fraction f = 5;

the integer to be automatically converted to a fraction. Well, for my happiness, there is such an option. The implicit keyword is used to declare an implicit user-defined type conversion operator. And here it is my usage.

public static implicit operator Fraction(int number)
{
	return new Fraction(number);
}

Now, I can simply use Fraction f = 5; and I have a fraction object. Really cool!

Filled under C#. No Comments.

GCD and LCM

GCD (Greatest Common Divisor) of two non-zero integers, is the largest positive integer that divides both numbers without remainder. For example, the GCD of the number 10 and 25 is 5 (10 = 5*2 and 25 = 5*5).

LCM (Least Common Multiple) of two integers a and b is the smallest positive integer that is a multiple of both a and b. Since it is a multiple, a and b divide it without remainder. If there is no such positive integer, e.g., if a = 0 or b = 0, then LCM is defined to be zero.

There is a connection between GCD and LCM and here it is: GCD(a, b) * LCM(a, b) = a*b

I will show you a simple implementation of the Euclid’s algorithm to determine the GCD of two integers. After you find the GCD of those two integers, you can find the LCM of them, too. (more…)

Filled under C#. No Comments.

Fractions

When you want to do many calculations with big decimals, you often loose a little information. If you were able to use fractions instead of decimals you’re going to safe some information (concerning the result). Well, with this simple class you can do a lot of manipulations using fractions. (more…)

Filled under C#. No Comments.