Euler #6 is very straightforward. I was concerned about needing to work with numbers > 2^32, but this was not a factor. I initially set my bound to < max instead of <= max which threw my results off my a small margin (1-99 instead of 1-100). Runtime is < 1 ms, only ~2400 ticks.
using System; using System.Diagnostics; namespace Euler6 { class Program { static void Main(string[] args) { Stopwatch sw = new Stopwatch(); sw.Start(); long result = SumDifference(1, 100); sw.Stop(); Console.WriteLine("Runtime was " + sw.ElapsedMilliseconds + " ms"); Console.WriteLine("Runtime was " + sw.ElapsedTicks +" ticks"); Console.WriteLine("Sum difference is: " + result); Console.ReadLine(); } static long SumDifference(int begin, int end) { return SquareOfSums(begin, end) - SumOfSquares(begin, end); } static long SumOfSquares(int begin, int end) { long returnValue = 0; for (int i = begin; i <= end; i++) { returnValue += Convert.ToInt64(Math.Pow(i,2)); } return returnValue; } static long SquareOfSums(int begin, int end) { long returnValue = 0; for (int i = begin; i <= end; i++) { returnValue += i; } return Convert.ToInt64(Math.Pow(returnValue, 2)); } } }
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.