Project Euler - Problem 005

Problem 5 can be quickly solved using LINQ's Aggregate method and BigInteger methods.

              private static BigInteger GetAnswerToProblem5()
              {
                  BigInteger result = BigInteger.One.To(20).Aggregate(SmallestMultiplier);
                  return result;
              }
              private static BigInteger SmallestMultiplier(BigInteger current, BigInteger next)
              {
                  return current * (next / BigInteger.GreatestCommonDivisor(current, next));
              }
          

This works because at each step of the aggregation, we compute the smallest value divisible by all items in the sequence to that point. The current parameter is the smallest value for all of the previous values in the sequence, and the next is the next value in the sequence.



Tags

  • .NET
  • Project Euler

Revisions

  • 9/19/2012 - Article published.