Project Euler - Problem 007

Problem 7 builds on the previous Prime computations and some more LINQ.

Starting with the infinite prime number sequence from Problem 3, we'll create a new helper method that returns the Nth prime.

              private static BigInteger NthPrime(BigInteger n)
              {
                  return Numbers.Primes
                                .SkipWhile((value, index) => { return index < n - 1; })
                                .Take(1)
                                .Single();
              }
          

Which allows for a nice and simple method to get the answer.

              private static BigInteger GetAnswerToProblem6()
              {
                  return Mathematics.NthPrime(10001);
              }
          

Minifying JavaScript and Cascading Style Sheets

I think I've finally figured out how to work JavaScript and stylesheet minification into my development workflow. I've been looking for something that will preserve the ability to work in a formatted file, generates a minified file, and doesn't leave Visual Studio complaining about missing files.

The set up involves the Microsoft Ajax Minifier, a batch file, and some Visual Studio setup.

  1. First download and install Microsoft Ajax Minifier. Make note of the install folder (it was "C:\Program Files (x86)\Microsoft\Microsoft Ajax Minifier\" for me).

  2. Add a Batch script to the Visual Studio project. Make sure to set the "Build Action" to "None". I'm calling my Batch script "minify.bat".

  3. The Batch script is what minifies the JavaScript and stylesheets for me. The contents look like:

                    set PATH=%~dp0;"C:\Program Files (x86)\Microsoft\Microsoft Ajax Minifier\"
    
                    pushd "<full path to my project here>"
    
                    ajaxmin -clobber
                            Styles\layout.css
                            Styles\style.css
                            -o Styles\wickedstrategery.min.css
    
                    popd
                  

    I've broken out the lines for readability only.

  4. Setup the project to run the Batch script when the project is built via the Post-build Build Events in the Project Properties:

                    call "$(ProjectDir)minify.bat"
                  
  5. Run the script once at the command line (or build in Visual Studio) and the minified files would be created.

  6. Back in Visual Studio, use the "Show All Files" in the Solution Explorer to expose the newly created external files. I then imported them into the solution, and made sure the "Build Action" was set to "Content".

  7. Go to each of the source JavaScript and stylesheet files and set their "Build Action" to "None". They will stay in the project, but will not show up when the solution is built. The post-build step will regenerate the minified versions and copy the output to the output folder. Leaving the output to contain minified files but not the originals.

  8. From there, the last step is to go through the project (really just my MasterPage) and ensure the references to the originals are replaced with references to the minified version.

The inspiration for getting this all running was Scott Hanselman's write up The Importance (and Ease) of Minifying your CSS and JavaScript and Optimizing PNGs for your Blog or Website.

The end result is that my project still contains the nicely formatted scripting and stylesheets, but outputs minified versions. The minification is part of the build and doesn't interupt me working. I haven't added any steps to publishing the site either. And Visual Studio's built-in sanity check for files that exist continue to function.


Google Reader & The Importance of Data Portability

[Google Reader]

The closure of Google Reader has once again highlighted the importance of data portability. Fortunately, Google Reader supports export to OPML and every alternative I've looked at supports import from OPML. As a replacement, I've set up Tiny Tiny RSS in a virtual machine. The base image was the TurnKey LAMP Application and the installation was straight-forward.

The ability to export and import my subscription list has been crucial to switching between the feed readers I've tried. It amplifies the need for standards-compliant tools and really drives home the fact that I need to support those that allow portability and create those standards-compliant tools.

[DataPortability.org]

The DataPortability Project does a better job of explaining the importance that I could. There are several good reads on why the idea of data portability is important both from a technology standpoint and a business standpoint, as well as what can people do to promote the idea.

Images used without permission, courtesy of Google and DataPortability.org.


A Fast Alternative to MethodInfo.Invoke

Not surprisingly, calls to MethodInfo.Invoke are slow for the same reasons outlined that were outlined in A Fast Alternative to Activator.CreateObject.

The same approach can be used to generate methods that will invoke the MethodInfo instance, but provide an amazing performance boost by handling validation up front once rather than upon every invocation.

            public static Func<TTarget, TResult> GenerateFastInvoker<TTarget, TResult>(MethodInfo method)
            {
                #if DEBUG
                    ParameterExpression targetExpression = Expression.Parameter(typeof(TTarget), "target");
                #else
                    ParameterExpression targetExpression = Expression.Parameter(typeof(TTarget));
                #endif

                Expression<Func<TTarget, TResult>> expression = Expression.Lambda<Func<TTarget, TResult>>(
                    Expression.Call(
                        targetExpression,
                        method),
                    targetExpression);
                Func<TTarget, TResult> functor = expression.Compile();
                return functor;
            }
          

The same caveats apply as before, but the performance increase is well worth it. Further, the same alterations used to create weakly-typed fast activators can be applied to generate weakly-typed fast invokers.


Project Euler - Problem 006

Problem 6 is really easy to solve without needing any new extension methods nor complicated LINQ.

              private static BigInteger GetAnswerToProblem6()
              {
                  // Get the range of numbers to process for the problem.
                  IEnumerable<BigInteger> range = BigInteger.One.To(100);
                  // Determine the square of the sum of the values within the problem space.
                  BigInteger squareOfSum = BigInteger.Pow(range.Sum(), 2);
                  // Determine the sum of the squares of the values within the problem space.
                  BigInteger sumOfSquares = range.Select(bi => BigInteger.Pow(bi, 2)).Sum();
                  // Return the different.
                  return squareOfSum - sumOfSquares;
              }
          

Articles

My name is Doug Jenkinson. I write and ramble on about whatever comes to mind at .

Professionally

I'm currently employed as a Development Team Lead and Architect with Hyland Software in Cleveland, Ohio.

Contact

You can email me at webmaster@dougjenkinson.net.

Looking for an experienced and talented software engineer in the Akron or Cleveland area? Be sure to look at my résumé.