The order of complexity of the Pascal code -- like n * log(n)....

Click For Summary

Discussion Overview

The discussion revolves around determining the order of complexity of a given Pascal code snippet. Participants explore how to analyze the complexity based on the structure of the loops and the behavior of variables involved, focusing on the relationship between the outer and inner loops.

Discussion Character

  • Homework-related
  • Mathematical reasoning
  • Exploratory

Main Points Raised

  • One participant states that the complexity of the provided code is ##n*logn##, explaining that the outer loop runs n times while the inner loop runs logn times.
  • Another participant suggests examining the first few iterations of the outer loop to understand how the variable s changes and how it relates to the total iterations of the inner loop.
  • A further contribution describes the behavior of the variables s and k, noting that s is assigned values that double with each iteration, which may influence the number of steps in the inner loop.
  • There is a suggestion that the pattern of doubling for s and k continues for n iterations, which could affect the overall complexity analysis.

Areas of Agreement / Disagreement

Participants are exploring the complexity together, but there is no consensus on the final interpretation of the complexity or the exact relationship between the variables and iterations. The discussion remains unresolved regarding the best approach to analyze the problem.

Contextual Notes

Participants have not fully clarified the assumptions regarding the initial values of variables or the specific conditions under which the loops operate. The relationship between the outer and inner loops is still being explored, and no definitive mathematical steps have been established.

doktorwho
Messages
181
Reaction score
6

Homework Statement


The kind of problems i we are currently dealing with in school are like this:
Complexity.JPG

Find the order of complexity of the function

Homework Equations


3. The Attempt at a Solution [/B]
Im suppose to guess or calculate the complexity of this based on code sample. Its in Pascal and to clarify what is meant here here is an example of the code and its complexity
Code:
for k:=1 to n do
   while x>0
      k:=k+1
      x:= x DIV 2
The complexity of this code is ##n*logn##. The k-part is repeated n times while the inner while repeats logn times. How to solve these kinds of problems? What would be the best way?
As i see this, the i<n part is repeated n times, but the s-part confuses me. I can't see ##2^n##
 
Physics news on Phys.org
It will help if you look at the first 3 runs through of the outer loop manually. What happens to s in each step? How is that linked to the total iterations in the inner loop?
 
mfb said:
It will help if you look at the first 3 runs through of the outer loop manually. What happens to s in each step? How is that linked to the total iterations in the inner loop?

In the first run s is 1 and it is assigned to k. Then the inner code runs once, increases k by one and assigns it to s. Then the outer again assigns so to k but now the inner runs twice and increases k by 2. In the next run s is 8. Is that the pattern, then it will be 16 and it runs n times since the outer checks for i<n and it is increased by 1 every time?
 
The values used for s and k double every time, and they will do that n times, right.

That should lead to the number of steps done in the inner loop.
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
3
Views
3K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 14 ·
Replies
14
Views
2K
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K