Jotting down stuffs
Posts tagged C#
Project Euler Problem 12 – Faster brute-forcing using Parallel LINQ
Mar 21st
Building on the previous code, the CountFactor method would be a good candidate to be executed in parallel. It is doing the heavy calculation of finding the number of factor a number has, and it has no dependency on any other external factor.
Previously, when there is no PLINQ, we would have to do multithreading manually, and I would have probably came up with something like this: Read on to see the PLINQ code that managed to almost halve the time >
Project Euler Problem 12 – Solution using basic C# and LINQ
Mar 14th
Two days ago I had been looking at re-playing with Project Euler problems, after I solved a few of them last time (quite some time ago). I looked at the problem list, and decided to try solve the top-most unsolved problem I have: Problem 12. I managed to come up with a brute-force code that calculated the answer in about 403 ms on an Intel Core2 Duo T9600 @ 2.80GHz machine.
Understanding the problem
The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, …
Given the example, I guess the mathematical function explanation of the triangle numbers would be something along this line: f(n) –> 1+2+3+…+n where n=1, 2, 3, …
We are then required to come up with something to find the first triangle number to have over 500 divisors. In clearer words, we are required to find the first triangle number to have 501 divisors, within one minute, complying to the Project Euler’s one-minute rule:
I’ve written my program but should it take days to get to the answer?
Absolutely not! Each problem has been designed according to a “one-minute rule”, which means that although it may take several hours to design a successful algorithm with more difficult problems, an efficient implementation will allow a solution to be obtained on a modestly powered computer in less than one minute.
Spoiler alert! Read on to know my solution to this problem >
PGP Single Pass Sign and Encrypt .NET Stream
Mar 8th
In a recent software development project, I was required to be able to read PGP encrypted content, do something, and write PGP encrypted result. The project allowed calling of PGP (or GPG) command line to do the encryption and decryption needed. However that did not feel right to me. Googled a bit and then I found the Bouncy Castle C# Crypto APIs, that among everything else, included support for PGP without having any external dependency.
The compiled assembly for the Bouncy Castle APIs can be downloaded from the website, however it was compiled for the .NET 1.1 runtime version. You can easily download the source code instead and compile it for the other .NET versions if you want to. I managed to compile the source code for the .NET 2.0 runtime version without any changes to the original source code.
The bigger issue at using the Bouncy Castle APIs is that it does not seem to have any proper documentation for it. Read on to get the code for the .NET Stream >