I am working on optimizing for speed a method to calculate a triangle number for ProjectEuler - Problem 12.
I need to somehow be able to initialize the first element in an array:
private static ulong[] numbersArray = new ulong[5000];
One way to calculate a triangle number would be:
public static ulong GetTriangleFormula(ulong n)
{
return n * (n + 1) / 2;
}
But it has a computational complexity somewhere between O(n) and O(n^2) so I was thinking I could trade memory for run speed by computing the numbers recursively and storing the results in a Dictionary/array. Since the triangle numbers will be calculated successively in the final solution it should work.
Computing the n-th triangle number should become a simple sum of numbersArray[n - 2] and n.
Using a Dictionary the computation was much slower for successive triangle number computation from 1 to 1000:
private static Dictionary<ulong, ulong> numbers = new Dictionary<ulong, ulong>() { { 1, 1 } };
public static ulong GetTriangleDictionaryRecursive(ulong n)
{
if (!numbers.ContainsKey(n))
numbers[n] = n + GetTriangleDictionaryRecursive(n - 1);
return numbers[n];
}
I added {1, 1} to the Dictionary so that i wouldn't have to always check at the beginning of the GetTriangleDictionaryRecursive method for the base case:
if(n == 1) return 1;
But the results were that it was about 40 times slower than the formula method.
So now i am trying to write a method using an array of type ulong[] but I do not know how I can initialize only the first element with value 1 (the others being default for ulong 0).
public static ulong GetTriangleArrayRecursive(ulong n)
{
if (numbersArray[n - 1] == 0)
numbersArray[n - 1] = n + GetTriangleArrayRecursive(n - 1);
return numbersArray[n - 1];
}
Thanks for your help! :)
numbersArray[0] = 1;right after you allocate it?numbersArray[0] = 1;will intialise the first 1 in the array to 1.. I think you overcomplicated the question