Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6838133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:33:36+00:00 2026-05-26T23:33:36+00:00

I am working on optimizing for speed a method to calculate a triangle number

  • 0

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! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:33:36+00:00Added an answer on May 26, 2026 at 11:33 pm

    I have no idea whether or not it’s appropriate for the Euler problem, but in general if I want to do more work than a single simple expression for initialization, I do it like this:

    private static readonly ulong[] numbersArray = CreateNumbersArray();
    
    private static ulong[] CreateNumbersArray()
    {
        ulong[] ret = new ulong[5000];
        ret[0] = 1;
        return ret;
    }
    

    Or you can do it in a static constructor:

    private static readonly ulong[] numbersArray;
    
    static FooClass()
    {
        numbersArray = new ulong[5000];
        numbersArray[0] = 1;
    }
    

    Are you really sure it’s appropriate to make this a static variable, by the way?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on optimizing site load speed for a few moderately high traffic
I'm working on optimizing an application . I found that i need to optimize
I've been working on optimizing a query and have ran into a situation that's
I'm student working on optimizing GCC for multi-core processor. I tried going through the
I’m working on optimizing some of my queries and I have a query that
I'm working on my first game, and it works fine, but clearly needs some
I'm having troubles optimizing this Levenshtein Distance calculation I'm doing. I need to do
I am working on optimizing one of the SQL Job. Here I have few
I've been working on optimizing my site and databases, and I have been using
I am working on a project involving optimizing energy consumption within a system. Part

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.