Reusing computations
Jordi Cortadella
Department of Computer Science
Reusing computations
β’ There are certain algorithms that require a
repeated sequence of similar computations.
For example:
π
π π₯ =
π‘π (π₯)
π=0
β’ Strategy: try to reuse the work done from
previous computations as much as possible.
Introduction to Programming
© Dept. CS, UPC
2
Calculating ο°
ο° can be calculated using the following series:
π
=
2
β
π=0
π!
2π + 1 βΌ
where πβΌ = 1 × 3 × 5 × 7 × β― × π (π odd)
Introduction to Programming
© Dept. CS, UPC
3
Calculating ο°
π 0! 1! 2! 3!
=
+
+
+
+β―
2 1βΌ 3βΌ 5βΌ 7βΌ
π
1
1β2
1β2β3
=1+
+
+
+β―
2
1β3 1β3β5 1β3β5β7
Since an infinite sum cannot be computed, it may
often be sufficient to compute an approximation
with a finite number of terms.
Introduction to Programming
© Dept. CS, UPC
4
Calculating ο°: naïve approach
// Pre: nterms > 0
// Returns an estimation of ο° using nterms terms
// of the series.
double Pi(int nterms) {
double sum = 1;
// Approximation of ο°/2
// Inv: sum is an approximation of ο°/2 with k terms,
//
term is the k-th term of the series.
for (int k = 1; k < nterms; ++k) {
term = factorial(k)/double_factorial(2οͺk + 1);
sum = sum + term;
}
return 2οͺsum;
}
It should be a
real division
Introduction to Programming
© Dept. CS, UPC
5
Calculating ο°: reusing computations
π
1
1β2
1β2β3
=1+
+
+
+β―
2
1β3 1β3β5 1β3β5β7
π
= π‘0 + π‘1 + π‘2 + π‘3 + β―
2
π!
πβ1 !βπ
π
π‘π =
=
= π‘πβ1
2π + 1 βΌ
2π β 1 βΌ β (2π + 1)
2π + 1
Introduction to Programming
© Dept. CS, UPC
6
Calculating ο°
// Pre: nterms > 0
// Returns an estimation of ο° using nterms terms
// of the series.
double Pi(int nterms) {
double sum = 1;
double term = 1;
// Approximation of ο°/2
// Current term of the sum
// Inv: sum is an approximation of ο°/2 with k terms,
//
term is the k-th term of the series.
for (int k = 1; k < nterms; ++k) {
term = termοͺk/(2.0οͺk + 1.0);
sum = sum + term;
}
return 2οͺsum;
}
Introduction to Programming
© Dept. CS, UPC
7
Calculating ο°
β’ ο° = 3.14159265358979323846264338327950288...
β’ The series approximation:
Introduction to Programming
nterms
1
5
Pi(nterms)
2.000000
3.098413
10
15
20
25
3.140578
3.141566
3.141592
3.141593
© Dept. CS, UPC
8
Taylor and McLaurin series
β’ Many functions can be approximated by using
Taylor or McLaurin series, e.g.:
β’ Example: sin x
Introduction to Programming
© Dept. CS, UPC
9
Calculating sin x
β’ McLaurin series:
β’ It is a periodic function (period is 2ο°)
β’ Convergence improves as x gets closer to zero
Introduction to Programming
© Dept. CS, UPC
10
Calculating sin x
β’ Reducing the computation to the (-2ο°,2ο°)
interval:
β’ Incremental computation of terms:
Introduction to Programming
© Dept. CS, UPC
11
Calculating sin x
#include <cmath>
// Returns an approximation of sin x.
double sin_approx(double x) {
int k = int(x/(2οͺM_PI));
x = x β 2οͺkοͺM_PI; // reduce to the (-2ο°,2ο°) interval
double term = x;
double x2 = xοͺx;
int d = 1;
double sum = term;
while (abs(term) >= 1e-8) {
term = -termοͺx2/((d+1)οͺ(d+2));
sum = sum + term;
d = d + 2;
}
}
return sum;
Introduction to Programming
© Dept. CS, UPC
12
Combinations
Calculating
π
=
π!
π
π!
πβπ !
=
π(πβ1)β―(πβπ+1)
π(πβ1)β―1
β Naïve method: 2π multiplications and 1 division
(potential overflow with π!)
β Recursion:
π
π
=
=1
0
π
π πβ1
πβπ+1
π
π
=
=
π
πβ1
π πβ1
π
π
πβ1
πβ1
πβ1
=
=
+
π
πβ1
π
πβπ
Introduction to Programming
© Dept. CS, UPC
13
Combinations
// Pre: n ο³ k ο³ 0
// Returns combinations(n k)
int combinations(int n, int k) {
int c = 1;
for (; k > 0; --k) {
c = cοͺn/k;
--n;
}
return c;
}
Problem: integer division
cοͺn/k may not be integer
Introduction to Programming
© Dept. CS, UPC
14
Combinations: recursive solution
// Pre: n ο³ k ο³ 0
// Returns combinations(n k)
int combinations(int n, int k) {
if (k == 0) return 1;
return nοͺcombinations(n β 1, k β 1)/k;
}
Always an
integer division!
Introduction to Programming
© Dept. CS, UPC
15
Conclusions
β’ Try to avoid the brute force to perform
complex computations.
β’ Reuse previous computations whenever
possible.
β’ Use your knowledge about the domain of the
problem to minimize the number of
operations.
Introduction to Programming
© Dept. CS, UPC
16
© Copyright 2026 Paperzz