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 8814033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:00:22+00:00 2026-06-14T04:00:22+00:00

I have multiple (N) nested loops as follows: int k = 0; for (int

  • 0

I have multiple (N) nested loops as follows:

int k = 0;
for (int i1 = 0; i1 < n; i1++)
{
  for (int i2 = 0; i2 <= i1; i2++)
  {
    for (int i3 = 0; i3 <= i2; i3++)
    {
       ...
            for (int iN = 0; iN <= i{N-1}; iN++)
            { 
              k++;
              //k = f(i1, ... , iN);
            }
    } 
  } 
}

I need a formula to get k inside the loops based on i1, … , iN.

For N=1: k=f(i1)=i1

For N=2: k=f(i1,i2)=i1*(i1+1)/2+i2

  • 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-06-14T04:00:23+00:00Added an answer on June 14, 2026 at 4:00 am

    In general, you have a nested sum :

    sum(sum(sum(1,i3=0..i2),i2=0..i1),i1=0..n-1)
    

    which you can simplify from the inside out using these basic summation formules. The end result will only depend on N as izomorphius commented, as N is the upper limit for i1, which is the upper limit for i2,…

    edit: ok, with your edit, I now see you have a different problem in mind. Now it’s a bit more complicated but still no problem.

    We will need to split some stuff up for this. I will calculate the value k= f(4,3,1).
    At that point in time, we will have performed 4 full i1 loops (i1=0,1,2,3) and are doing the fifth. The k value after 4 full loops of i1(=k_i1) (just before we start the fifth) can be calculated using this function (same kind as before).

    k_i1=sum(sum(sum(1,i3=0..i2),i2=0..l),l=0..i1-1) = f1(i1);
    

    Now we start the 5th loop and do the same for the i2 loop. At that point, we will have done 3 full loops of i2 and so we get

    k_i2=sum(sum(1,i3=0..l),l=0..i2-1)=f2(i2);
    

    This goes on for all your loops. To get the final value, you have to add each of the fi-functions. k will look like

    k=k_i1+k_i2+...
    

    Some minor (+-1) errors in my explanation are possible, but the basic idea is to use the formula to count the full loops.

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

Sidebar

Related Questions

I have multiple nested foreach loops that do some processing, how can I convert
I have multiple nested movieclip in its own different movieclip, and i would like
I have multiple ajax requests with javascript code as response, and I need to
I have multiple UpdatePanels on a webpage, one inside UserControl (Purple ColorBox as show
I have a parent folder that contains inside it multiple folders named Project-* (folders
I'm having problem with nested loop. I have multiple number of posts, and each
I currently have many linq expressions nested within foreach loops ... kind of defeating
I have a multiply nested namespace: namespace first {namespace second {namespace third { //
I have multiple projects which are to be hosted together in a Tomcat container,
I have multiple forms for lots of products on my page and want to

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.