algorithm described in csharp