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

  • Home
  • SEARCH
  • 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 6588813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:05:41+00:00 2026-05-25T17:05:41+00:00

I am trying to understand how many times the statement x = x +

  • 0

I am trying to understand how many times the statement “x = x + 1” is executed in the code below, as a function of “n”:

for (i=1; i<=n; i++)
  for (j=1; j<=i; j++)
    for (k=1; k<=j; k++)
       x = x + 1 ;

If I am not wrong the first loop is executed n times, and the second one n(n+1)/2 times, but on the third loop I get lost. That is, I can count to see how many times it will be executed, but I can’t seem to find the formula or explain it in mathematical terms.

Can you?

By the way this is not homework or anything. I just found on a book and thought it was an interesting concept to explore.

  • 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-25T17:05:42+00:00Added an answer on May 25, 2026 at 5:05 pm

    Consider the loop for (i=1; i <= n; i++). It’s trivial to see that this loops n times. We can draw this as:

    * * * * *
    

    Now, when you have two nested loops like that, your inner loop will loop n(n+1)/2 times. Notice how this forms a triangle, and in fact, numbers of this form are known as triangular numbers.

    * * * * *
    * * * *
    * * *
    * *
    *
    

    So if we extend this by another dimension, it would form a tetrahedron. Since I can’t do 3D here, imagine each of these layered on top of each other.

    * * * * *     * * * *     * * *     * *     *
    * * * *       * * *       * *       *
    * * *         * *         *
    * *           *
    *
    

    These are known as the tetrahedral numbers, which are produced by this formula:

    n(n+1)(n+2)
    -----------
         6
    

    You should be able to confirm that this is indeed the case with a small test program.

    If we notice that 6 = 3!, it’s not too hard to see how this pattern generalizes to higher dimensions:

    n(n+1)(n+2)...(n+r-1)
    ---------------------
             r!
    

    Here, r is the number of nested loops.

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

Sidebar

Related Questions

I'm trying to get a one-to-many relationship working with grails/gorm. I don't understand how
After trying to understand why client code is not rendered in a page (injected
I'm a C programmer trying to understand C++. Many tutorials demonstrate object instantiation using
Trying to understand Ruby a bit better, I ran into this code surfing the
I'm trying to understand some code I've been given. It works in a similar
I'm trying to figure out how many times the maximum value of an array
I am trying to learn more about regular expressions I have one below that
I am trying to understand the code in this page: https://github.com/corroded/git-achievements/blob/gh-pages/git-achievements and I'm kinda
I am not JS developer but I am trying to understand the way the
I know this has been asked many times, but I don't understand what is

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.