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:

  1. static void Main(string[] args)
  2. {
  3. static void Main(string[] args)
  4. {
  5. int limit = 1001 * 1001;
  6. int count = 0;
  7. int loopCount = 2;
  8.  
  9. int sum = 1; //1 coz the centervalue is 1. In loop counting from 3.
  10. // every 4 iteration the increment count is adding +2
  11. for (int i = 3; i <= limit; i += loopCount)
  12. {
  13. sum += i;
  14. count += 1;
  15. if (count >= 4)
  16. {
  17. loopCount += 2;
  18. count = 0;
  19. }
  20. }
  21. Console.WriteLine(sum);
  22. Console.ReadLine();
  23. }
  24. }

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:

  1. static void Main(string[] args)
  2. {
  3. int count = 3;
  4. Dictionary F = new Dictionary();
  5. F.Add(0, 0);
  6. F.Add(1, 1);
  7. F.Add(2, 1);
  8. while (true)
  9. {
  10. BigInteger index1 = count - 1;
  11. BigInteger index2 = count - 2;
  12. BigInteger val = F[index1] + F[index2];
  13. F.Add(count, val);
  14. if (val.ToString().Length >= 1000)
  15. {
  16. break;
  17. }
  18. count += 1;
  19. }
  20. Console.WriteLine(count);
  21. Console.ReadLine();
  22. }

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.
  1. public static string GetHashValue(byte[] content)
  2. {
  3. string hash;
  4. using (System.Security.Cryptography.SHA1CryptoServiceProvider sha = new System.Security.Cryptography.SHA1CryptoServiceProvider())
  5. {
  6. hash = Convert.ToBase64String(sha.ComputeHash(content));
  7. }
  8. return hash;
  9. }

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.
  1. public static string GetMimeTypeFromExtension(string extension)
  2. {
  3. string mimeType = string.Empty;
  4. RegistryKey key;
  5. object value;
  6. if (!string.IsNullOrEmpty(extension))
  7. {
  8. if (!extension.StartsWith("."))
  9. {
  10. extension = "." + extension;
  11. }
  12. key = Registry.ClassesRoot.OpenSubKey(extension, false);
  13. value = key != null ? key.GetValue("Content Type", null) : null;
  14. mimeType = value != null ? value.ToString() : string.Empty;
  15. }
  16. return mimeType;
  17. }
While calling you can pass the parameter like,
 " .pdf "
Or
"  pdf  "

our code will handle the dot !.