ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Location: Location??? Where I am is top secret, if I tell you, I have to kill you.
Distribution: College, Slack
Posts: 24
Rep:
No, what are you on?
There is no way to tell simply because you give way to little info.
It's like asking someone to tell you what color a room is, when the lights are off.
n could be anything.
SO that is going to stop people from being able to figure it out. If you gave a little more info, maybe, someone could tell you, but with what you gave, there is no way, that i know of that anyone could find out.
no because in the above example...n could be any number....and it would still execute exactly n^3 time....if n was 2 then it would execute 8 times....if n was 4 then it would execute 64 times....
so no matter what n is, it will still execute n^3 times.....
What exactly are you asking for? You asked for a specific value in the original post but now you make it sound like you want a formula which isn't even right because you have ++ in 4 different places in the loops so it doesn't get called n^3 times.
Well in that case, the answer is you haven't given enough information to give an answer. We need to know what n is to give you a number. Technically speaking, x++ only gets executed in the innermost loop, not each loop, and it gets executed n times...whatever n is...that's the answer you're looking for?
I dunno if it ever does, look at the conditions and when the increments happen:
Code:
for (i=0; i < n; i++)
for (j=0; j < i; j++)
for (k = 0; k < j; k++)
x++;
So i is set to 0, we assume 0 < n is true and the first for loop is entered. The second for loop is encountered and j is set to 0 too. The next thing is to evaluate its condition, which gives us 0 < 0, false, and the loop body and j++ never gets executed. Then after an i++ we're back to the condition of the first loop. Now if n = 1 then 1 < 1 is false and we're done.
Otherwise we loop again and test that j < i, which is now true. I'm gunna stop working the timings from here but my point is that dependant on the value of n you might only get to a certain depth in the for loops and never execute the x++ even if you increment other variables. It all depends on when the conditions are evaluated.
you are right, it is dependent on the value of N.....but i'm thinking there still has to be a general formula that will evaluate x++ for any value of N......and that is what i'm trying to figure out.....i also need to figure out the big Oh complexity of each loop as well....i'm thinking it may just be O(N) since i, and j are constants and the loop just depends on the value of N...but i'm not real sure...
Honestly I think this is your homework which is why I havent given you the formula, but I've started you off on it with my previous post. The mods do not want homework questions answered, this site's for people having problems with linux and you'll understand everything better if you do it yourself. Just continue my workings, maybe write the outcome for each value of n >= 0 and try to see the pattern wrt n.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.