CSCI-UA.0480-004
Algorithmic Problem Solving
Brett Bernstein and Sean McIntyre
Class 17: Math
Math!
●
A big component in programming
contests
●
●
●
Besides common algorithms and patterns,
math problems tend to be hard to teach
The problem-setter fnds a problem that he
or she is comfortable with and creates a
problem around it
“Complete search” is usually useful
●
Except, for example, number theory problems
Math!
●
Computing logarithms, base b
●
In Java, we have Math.log(), which is base e
●
How do we compute logb(a)?
●
Math.log(a) / Math.log(b)
Math!
●
Prime Numbers
●
●
●
●
An integer p >= 2 divisible by 1 and p
First 10 primes: {2, 3, 5, 7, 11, 13, 17, 19,
23, 29}
Prime counts:
●
1-100: 25 primes
●
1-1000: 168 primes
●
1-10,000: 1229 primes
Useful for factoring!
Math!
●
Testing if a number is prime, isPrime(n)
●
Check if n is divisible by 2, …, n-1
●
●
Check if n is divisible by 2, 3, …, sqrt(n)
●
●
O(sqrt(n))
Check if n is divisible by 3, 5, 7, …, sqrt(n)
●
●
O(n)
O(sqrt(n)/2) = O(sqrt(n)) – plus check of 2
Check if n is divisible by primes <= sqrt(n)
●
O(π(sqrt(n)) = O(sqrt(n) / log(sqrt(n)))
●
π(m) = number of primes up to m
Math!
●
Generating primes from 1 to N
●
Naive algorithm:
●
for (int i = 0; i <= N; i++) {
if (isPrime(i)) print(i);
}
●
Better algorithm:
●
Sieve of Eratosthenes
Math!
Sieve of Eratosthenes
●
0
1
2
3
4
5
6
7
8
9
F
F
T
T
T
T
T
T
T
T
... 51 52 53 54 55 ... 75 76 77
T
T
T
T
T
T
T
T
Math!
Sieve of Eratosthenes
●
0
1
2
3
4
5
6
7
8
9
... 51 52 53 54 55 ... 75 76 77
F
F
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
F
F
T
T
F
T
F
T
F
T
T
F
T
F
T
T
F
T
Math!
Sieve of Eratosthenes
●
0
1
2
3
4
5
6
7
8
9
... 51 52 53 54 55 ... 75 76 77
F
F
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
F
F
T
T
F
T
F
T
F
T
T
F
T
F
T
T
F
T
F
F
T
T
F
T
F
T
F
F
F
F
T
F
T
F
F
T
Math!
Sieve of Eratosthenes
●
0
1
2
3
4
5
6
7
8
9
... 51 52 53 54 55 ... 75 76 77
F
F
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
F
F
T
T
F
T
F
T
F
T
T
F
T
F
T
T
F
T
F
F
T
T
F
T
F
T
F
F
F
F
T
F
T
F
F
T
F
F
T
T
F
T
F
T
F
F
F
F
T
F
F
F
F
T
Math!
Sieve of Eratosthenes
●
0
1
2
3
4
5
6
7
8
9
... 51 52 53 54 55 ... 75 76 77
F
F
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
F
F
T
T
F
T
F
T
F
T
T
F
T
F
T
T
F
T
F
F
T
T
F
T
F
T
F
F
F
F
T
F
T
F
F
T
F
F
T
T
F
T
F
T
F
F
F
F
T
F
F
F
F
T
F
F
T
T
F
T
F
T
F
F
F
F
T
F
F
F
F
F
Math!
●
Sieve of Eratosthenes
1)A 1D boolean array P from 0 to N
●
Initialized to true except 0 and 1
2)Pick i, the next True element in the array
1)Mark P[ j ] = False for j = i*2, i*3, …, i*k where
i*(k+1) > N
Math!
●
Finding prime factors for a number N
●
●
Check if N is divisible by the primes
generated by the sieve
●
N = P * N'
●
P is a prime factor
If N' = 1, then all primes have been
discovered
●
Otherwise repeat on N'
Math!
●
GCD, greatest common divisor of two
numbers
●
●
Euclidean algorithm
●
gcd(a, 0) = a
●
gcd(a, b) = gcd(b, a % b)
In code:
●
●
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
LCM, least common multiple of two
numbers
●
lcm(a, b) = a*b / gcd(a, b)
Math!
●
Exercises
Math!
●
BigInteger class
●
Arbitrarily large integer type
●
●
●
●
But has to ft in memory of the program
If you're asked to work with very large
numbers (larger than 2^63), use
BigInteger!
Basic operations: +, -, /, *, %, pow
Advanced operations: gcd, modulo
arithmetic, base conversion
Math!
●
BigInteger class
●
Arbitrarily large integer type
●
●
●
●
But has to ft in memory of the program
If you're asked to work with very large
numbers (larger than 2^63), use
BigInteger!
Basic operations: +, -, /, *, %, pow
Advanced operations: gcd, modulo
arithmetic, base conversion
Math!
●
Exercise:
●
●
UVa 11130 Billiard Bounces
Example where math insight helps fnd the
solution!
© Copyright 2026 Paperzz