
















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

















































David Galles
Department of Computer Science
University of San Francisco
03-0:
for (i=1; i<=n*n; i++)
for (j=0; j<i; j++)
sum++;
03-2:
for (i=1; i<=n*n; i++)
for (j=0; j<i; j++)
sum++;
Exact # of times
sum++
is executed:
(^2) n ∑ i=
i^
n
n
2
n
4
n
2 2
n
03-3:
long power(long x, long n) {
if (n == 0)
return 1; else
return x * power(x, n-1);
03-5:
long power(long x, long n) {
if (n == 0)
return 1; else
return x * power(x, n-1);
c
1
for some constant
c
1
n
c
2
n
for some constant
c
2
03-6:
c
1
n
n
c
2
If we knew
n
, we could solve
n
n
=
n
c
2
03-8:
c
1
n
n
c
2
If we knew
n
, we could solve
n
n
=
n
c
2
n
n
c
2
=
n
c
2
c
2
=
n
c^2
n
n
c
2
=
n
c
2
c^2
=
n
c^2
03-9:
c
1
n
n
c
2
If we knew
n
, we could solve
n
n
=
n
c
2
n
n
c
2
=
n
c
2
c
2
=
n
c^2
n
n
c
2
=
n
c
2
c^2
=
n
c^2
n
n
c
2
=
n
c^2
03-11:
c
1
n
n
k
k
c
2
for all
k
If we set
k
n
, we have:
n
=
n
n
nc
2
=
nc
2
=
c
1
nc
2
n
03-12:
long power(long x, long n) {
if (n==0) return 1;if (n==1) return x;if ((n % 2) == 0)
return power(x*x, n/2); else
return power(x*x, n/2) * x;
03-14:
n
=
n/
c
3
03-15:
n
=
n/
c
3
n/
n/
c
3
=
n/
c
3
c
3
=
n/
c^3
03-17:
n
=
n/
c
3
n/
n/
c
3
=
n/
c
3
c
3
=
n/
c^3
n/
n/
c
3
=
n/
c
3
c^3
=
n/
c^3
n/
n/
c
3
=
n/
c
3
c^3
=
n/
c^3
03-18:
n
=
n/
c
3
n/
n/
c
3
=
n/
c
3
c
3
=
n/
c^3
n/
n/
c
3
=
n/
c
3
c^3
=
n/
c^3
n/
n/
c
3
=
n/
c
3
c^3
=
n/
c^3
n/
n/
c
3
=
n/
c
3
c^3
=
n/
c^3