Project Euler

by Kofi Sarfo 25. August 2009 00:43

"Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems." - Project Euler .Net

Maths problems aren't something we've been going out of our way to find. In fact we were looking for Code Katas when we found Project Euler. The first Code Kata, Supermarket Pricing, isn't immediately a programming problem. In fact it doesn't take a tremendous leap to flesh out concrete example code even though the real idea is to practise some modelling. So in pursuit of an immediate problem requiring an immediate solution via code:

#1 Add all the natural numbers below one thousand that are multiples of 3 or 5

No, it's not in the least bit difficult but that's not the idea. The obvious solution:

public static long AddNumbersThatAreMultiplesOf(int Min, int Max, int[] Primes) { long sum = 0; for (int i = Min; i < Max; i++) { foreach (int p in Primes) { if (i % p == 0) { sum += i; break; } } } return sum; }

It's an example of doing just enough to get the right answer. A better solution (cleverly translated from PHP) is below:

public static double GetAnswerUsingMoreOptimalSolution() { long max = 1000; double SumOfMultiplesOfThreeBelowMax = 1.5 * (int)((max-1)/3) * (int)((max+2)/3); double SumOfMultiplesOfFiveBelowMax = 2.5 * (int)((max-1)/5) * (int)((max+4)/5); double SumOfMultiplesOfFifteenBelowMax = 7.5 * (int)((max-1)/15)*(int)((max+14)/15); return SumOfMultiplesOfThreeBelowMax + SumOfMultiplesOfFiveBelowMax - SumOfMultiplesOfFifteenBelowMax; }

This more optimal solution (not mine) looks to be derived from GCSE mathematics, the formula for arithmetic progressions: A sum of terms equals (n/2)(a+l), where n is the number of terms, a is the first term, and l is the last term.

Note: The obvious refactoring in GetAnswerUsingMoreOptimalSolution() has been left intentionally.

A member of the BrainBench Appreciation Society

by Kofi Sarfo 24. August 2009 08:26
C# 2.0
 
  Score 4.28
  Percentile Scored higher than 95% of previous examinees
  Account Percentile Scored higher than 95% of 21 examinees within this account
  Proficiency Level: Advanced (Master)
Demonstrates a clear understanding of many advanced concepts within this topic. Appears capable of mentoring others on most projects in this area.
  Strong Areas
  • Object Oriented Design
  • Unmanaged Interoperability
  • Grammar and Logic
  • Assembly Loading and Reflection
  • Arrays and Collections
  Weak Areas
  • None noted

It's tempting to do a few more for a perfect score. When I did a BrainBench test online last it included questions I thought were less relevant such as those about compiler options, for example, but this one was much better. It still requires a fair amount of thought and for those questions where compiling the code or executing the example will give the correct answer they use images rather than text so cut & paste isn't an option.

It's worth pointing out that this is shared with tongue in cheek. I think few decent developers place any value in these because it's easy to score highly on these tests without actually having any programming competency.

Tags:

Resources

Interview with the Hedge Fund

by Kofi Sarfo 24. August 2009 00:03

We interviewed with a London-based Hedge Fund last Friday and, as usual, it was interesting. The first guy I saw (head of development) is a graduate of Conchango, a company that writes software of excellent quality using good developers, the latest software technology and techniques, etc. Also, they often kindly host the London Dotnet User Group meetings at their offices near London Bridge.

Naturally the interviewer asks fair questions along the lines of Value Types versus Reference Types and Garbage Collection. Inheritence and Shadowing. So far so good. Only when we get to the Messaging architecture used and extended at the previous client's site is there a little uncertainty. Without the source code, which we didn't take with us, we can't remember enough detail about the implementation from July 2008 so there's one shaky answer.

Next up is a question about our exposure to SQL Server 2005. Yup, we used it. Oh, which bits? That will be the new Try-Catch (after a little prodding about error-handling). We mention not having used CLR functions but being aware of the possibility which leads to a question about the components of the CLR... and we've a blank moment!

The JIT compiler! In fact we mentioned just JIT.

This is *all* that arrives. That's it. Nothing about Exception-Handling, Memory Management, Thread Management, Garbage Collection, Security, Managed Code, Type Safety - this is an impressive list already - Portable Executables nor IL never mind debugging and profiling services offered. Oh, there's more that wasn't mentioned. Code management (loading and execution), Application memory isolation, Access to metadata (enhanced type information) and Interop. Nice.

Then comes the first of a few quirky incidents. Hint: If ever an interviewer testing your C# knowledge offers the Base Class Library as a component of the CLR it's probably worth saying something. To prevent an already lengthy entry from growing needlessly still a summary will do here. When Interviewer #1 is replaced by a senior developer I then learn that impersonation does not work the way I've used it. Apparently, I've misundersood all along that using the identity element in the web config for an ASP.NET web application will not allow me to use credentials for a SQL Server connection. Maybe at that point I should have just looked it up on my iPhone and shown him this:


<system.web>
<identity impersonate="true" userName="domain\username" password="password" />
</system.web>
Of course that might have seemed a bit smug. Both nice guys. Both almost certainly good developers. I'd still like to work with them on the enterprise reporting solution due by the end of the year. However, during my next interview we won't assume that the interviewers have a monopoly on .NET understanding.

Kiva Loans

  • Makenzi Makau

    Makenzi Makau

    Grocery Store

    Requested loan: $200

    Amount raised: $0

    , Kenya

    To buy cabbages and oranges to sell.

    Loan Now »

  • Andrew Katushabe

    Andrew Katushabe

    Pub

    Requested loan: $875

    Amount raised: $0

    Mubende, Uganda

    to buy more drinks such as soda and beer for resale.

    Loan Now »

  • Jessica Joguilon

    Jessica Joguilon

    Pigs

    Requested loan: $350

    Amount raised: $0

    Valladolid, Negros Occidental, Philippines

    to buy feeds and vitamins for her pigs.

    Loan Now »

To see more entrepreneurs »

Make a loan to an entrepreneur across the globe for as little as $25. Kiva is the world's first online lending platform connecting online lenders to entrepreneurs across the globe.