0

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;

Edited by Daniel_34

2
Contributors
1
Reply
17
Views
2 Years
Discussion Span
Last Post by glenn_3
0

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.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.