I'm studying for a test and appreciate your help. How to calculate the running time (T(n)) of the following segment (written in Pascal):

k := 0;
m := n;
while m <= n do
begin
  k := k + 1;
  m := k * k;
end;
for i := 1 to k do
  for j := i to k do
  begin
    s := s + m;
    m := m - 1;
  end;
linux centric ..

#

read the man pages regarding your complier and profiler
on my linux system fpc is the pascal compiler and gprof the profiler.
build it as a standalone executable then run it

fpc -pg homework.pas

time ./homework

Input n:5000

real 0m2.072s
user 0m0.000s
sys 0m0.000s

#

gprof ... profiler ..

#
gprof homework
Flat profile:

Each sample counts as 0.01 seconds.
no time accumulated

% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
0.00 0.00 0.00 1 0.00 0.00 PASCALMAIN
.....

#

list source

cat homework.pas

Program homework;   
Var       
    m, n, k, i, j, s : Integer;

Begin    
 Write('Input n:'); 
 Readln(n);

k := 0;
m := n;
s := 0;

while m <= n do
begin
  k := k + 1;
  m := k * k;
end;

for i := 1 to k do
  for j := i to k do
  begin
    s := s + m;
    m := m - 1;
  end;   
End.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.