Tag Archives: c-sharp

Primality testing with Fermat’s little theorem and Miller-Rabin in C#

Prime numbers have many applications. For example, in cryptography. The RSA algorithm for public-key cryptography uses large prime numbers for generating keys. Quote: The RSA algorithm works as follows: take two large primes, p and q, and compute their product n = pq; n is called the modulus. Choose a number, e, less than n [...]

Posted in C# | Also tagged , , , , , , , , , , , , , , , , | 2 Comments

(C#) Download text or bytes via HTTP

Downloads contents as a byte array or string, depending on need.

Posted in C# | Also tagged , , , , , , , , , | Leave a comment

C# code execution at runtime with the Microsoft Roslyn CSharp ScriptEngine class

Microsoft has released the Roslyn CTP which allows developers to use the Visual Basic and C# compilers as APIs. The snippet below can be used to execute C# code at runtime.

Posted in C# | Also tagged , , , , , , | Leave a comment

Huffman coding in C#

Huffman coding is an encoding algorithm used for data compression. So called Huffman trees are used for encoding and decoding of data. The leaves of these trees contain the symbols and their frequencies. Each parent node contains the sum of the frequencies of its children.

Posted in C# | Also tagged , , , , , , , , | 11 Comments

(C#) Factorial prime numbers factorization

Find how many factors there is in N! for a prime P Ex: 5! = (2^3)*(3^1)*(5^1) mult(5,2) = 3 mult(5,3) = 1 mult(5,5) = 1 all else = 0

Posted in C# | Also tagged , , , , , | Leave a comment

(C#) Prime Number Generator

 

Posted in C# | Also tagged , , , , , , | Leave a comment

(C#) Key-Value Pair System.Collections.Generic

Simple example of a Key Value Pair using System.Collections.Generic.

Posted in C# | Also tagged , , , , , , , , , | Leave a comment

Minimax algorithm with alpha-beta pruning in C#

Minimax is an algorithm where two players (min and max) play against each other for minimizing loss and maximizing gain. The following picture illustrates minimax with a game of tic-tac-toe. The algorithm uses a tree for the moves and scores of each player. In some cases, it is unnecessary for the algorithm to check certain subtrees. [...]

Posted in C# | Also tagged , , , , , , , , , , , , , , , , | 1 Comment

(C#) Windows Forms Keyboard Shortcut on Control

Easily create Windows Forms Shortcuts with this class. For example: A shortcut to catch pasting via Ctrl + V. Shortcut sc = new Shortcut(this, new Action(() =>¬† MessageBox.Show(“Data pasted!”))); sc.Keys.Add(Keys.ControlKey); sc.Keys.Add(Keys.V);

Posted in C# | Also tagged , , , , , , , | Leave a comment

(C#) LazyLoader – a simple, thread-safe class that can be used to load data on demand

A simple, thread-safe wrapper class for lazy-loading data into an instance on-demand (i.e. when the instance is first accessed.) Usage: LazyLoader l = new LazyLoader(() => Foo.LoadFromDataSource(“DB Connection String”)); // For access to the lazy-loaded instance: var x = l.Instance.Bar; // Need to ensure that the data is loaded deterministically? Use this: l.EnsureLoad();

Posted in C# | Also tagged , , , , , , , , | Leave a comment