Wednesday, September 17, 2014

Project Euler Solution using C#: Problem 28 : Number Spiral Diagonals

Problem:

Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:

21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13

It can be verified that the sum of the numbers on the diagonals is 101.

What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way? My Solution:

static void Main(string[] args)
{
    static void Main(string[] args)
    {
        int limit = 1001 * 1001;
        int count = 0;
        int loopCount = 2;

        int sum = 1;   //1 coz the centervalue is 1. In loop counting from 3. 
        // every 4 iteration the increment count is adding +2
        for (int i = 3; i <= limit; i += loopCount)
        {
            sum += i;
            count += 1;
            if (count >= 4)
            {
                loopCount += 2;
                count = 0;
            }
        }
        Console.WriteLine(sum);
        Console.ReadLine();
    }
}

Note: You can simplifies the coding :)

Project Euler Solution using C#: Problem 25: 1000 Digit Fibonacci Number

Problem:

The Fibonacci sequence is defined by the recurrence relation:

Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
Hence the first 12 terms will be:

F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
F6 = 8
F7 = 13
F8 = 21
F9 = 34
F10 = 55
F11 = 89
F12 = 144

The 12th term, F12, is the first term to contain three digits.
What is the first term in the Fibonacci sequence to contain 1000 digits? My Solution:

static void Main(string[] args)
{
    int count = 3;
 Dictionary F = new Dictionary();
 F.Add(0, 0);
 F.Add(1, 1);
 F.Add(2, 1);
 while (true)
 {
  BigInteger index1 = count - 1;
  BigInteger index2 = count - 2;
  BigInteger val = F[index1] + F[index2];
  F.Add(count, val);
  if (val.ToString().Length >= 1000)
  {
   break;
  }
  count += 1;
 }
 Console.WriteLine(count);
 Console.ReadLine();
}

Note: You can simplifies the coding :)

Get the Hash value of a file byte[] (byte array) in CSharp ( C# )

Here is a simple code to get the Hash value of a byte[] (byte array). The byte[] can be from any file. You can see my previous post to get the hash value directly from file. Click Here.
public static string GetHashValue(byte[] content)
{
    string hash;
    using (System.Security.Cryptography.SHA1CryptoServiceProvider sha = new System.Security.Cryptography.SHA1CryptoServiceProvider())
    {
        hash = Convert.ToBase64String(sha.ComputeHash(content));
    }
    return hash;
}

Happy coding :)

Thursday, September 11, 2014

Get MIME Content Type From Extensions C#

I have seen most of the people hard coding the MIME Content Type as a Constant or List to use it in their code. It's not possible to hard code all MIME Content types and use it. So here is a simple code to get those MIME Content type from the System itself based on the extension you are giving.
public static string GetMimeTypeFromExtension(string extension)
{
   string mimeType = string.Empty;
   RegistryKey key;
   object value;
   if (!string.IsNullOrEmpty(extension))
   {
      if (!extension.StartsWith("."))
      {
      extension = "." + extension;
      }
key = Registry.ClassesRoot.OpenSubKey(extension, false); value = key != null ? key.GetValue("Content Type", null) : null; mimeType = value != null ? value.ToString() : string.Empty; }
return mimeType; }
While calling you can pass the parameter like,
 " .pdf "
Or
"  pdf  "

our code will handle the dot !.