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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:19:46+00:00 2026-05-26T02:19:46+00:00

This would be part # 2 of my question about analysis of for loop

  • 0

This would be part # 2 of my question about analysis of for loop running time

http://faculty.simpson.edu/lydia.sinapova/www/cmsc250/LN250_Weiss/L03-BigOhSolutions.htm#PR4 contains solutions, and I have question about two particular “for” loops

Could someone explain to me how to figure out running time for both of them. Thanks !

1.

sum = 0;
for( i = 0; i < n; i++)
    for( j = 0; j < i*i; j++)
        for( k = 0; k < j; k++)
            sum++;  

2.

sum = 0;
for( i = 0; i < n; i++)
    for( j = 0; j < i*i; j++)
        if (j % i ==0)
           for( k = 0; k < j; k++)
               sum++;
  • 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-26T02:19:47+00:00Added an answer on May 26, 2026 at 2:19 am

    The first snippet is O(n^5).

    Top Loop    = 0 - O(n)   = O(n)   iterations
    Middle Loop = 0 - O(n^2) = O(n^2) iterations
    Inner Loop  = 0 - O(n^2) = O(n^2) iterations
    
    Total = O(n^5)
    

    Here’s the closed-form solution of the first snippet: (computed via Mathematica)

    sum = -(1/10)*n + (1/4)*n^2 - (1/4)*n^4 + (1/10)*n^5
    

    This is a 5th order polynomial, therefore it is: O(n^5)

    The second snippet appears to be O(n^4).

    Top Loop    = 0 - O(n)   = O(n) iterations
    Middle Loop = 0 - O(n^2) = O(n^2) iterations
    If statement enters: O(1 / n) times
    Inner Loop  = 0 - O(n^2) = O(n^2) iterations
    
    Total = O(n^4)
    

    Here’s the closed-form solution of the second snippet: (computed via Mathematica)

    sum = -(1/12)*n + (3/8)*n^2 - (5/12)*n^3 + (1/8)*n^4
    

    This is a 4th order polynomial, therefore it is: O(n^4)

    Further explanation of the effect of the if-statement:

    The middle loop iterates from 0 to i*i. The if-statement checks if j is divisible by i. But that is only possible when j is a multiple of i.

    How many times is j a multiple of i if 0 <= j < i*i? Exactly i times. Therefore only 1/i of the iterations of the middle loop will fall through to the inner-most loop.

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

Sidebar

Related Questions

This question comes as part of my doubts presented on a broader question about
This is part question, and part hope for confirmation. I have my suspicions about
I just posted this as part of a reply to a question about the
How would I do this (the part in square brackets): $name = 'Bob'; mysql_query(INSERT
I asked a question about this previously but my database structure has changed, and
This was originally going to be a question about how to implement this because
This is really a two part question, since I don't fully understand how these
Just when I was about to post this question, I came up with an
I have already posted another question about this, but no one seemed to know
This is a question about organising lots of images in a web project. Say

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.