Chapter 3
Chapter 3
Chapter 3.
Integer Functions
Discussion on CMath: A Foundation for CS
AUGPath
November 2, 2023
Basic concepts Solving Recurrences Mod: The binary Op
Section 1
Basic concepts
Basic concepts Solving Recurrences Mod: The binary Op
5
⌊x⌋
4
⌈x⌉
y3
2
1
−5 −4 −3 −2 −1 1 2 x3 4 5
−1
−2
−3
−4
−5
Basic Properties
Basic Properties
Example. An identity
Example
p √
Prove that ⌊ ⌊x⌋⌋ = ⌊ x⌋.
Basic concepts Solving Recurrences Mod: The binary Op
Example. An identity
Example
p √
Prove that ⌊ ⌊x⌋⌋ = ⌊ x⌋.
p
• Let m = ⌊ ⌊x⌋⌋. What is the range of m?
p
• m ≤ ⌊x⌋ < m + 1.
• Squaring to get the answer.
Basic concepts Solving Recurrences Mod: The binary Op
Example
Let f (x) be any cont. mono. increasing func. with prop. that
f (x) = integer =⇒ x = integer, prove that ⌊f (x)⌋ = f (⌊x⌋),
same as the ceiling.
Basic concepts Solving Recurrences Mod: The binary Op
Example
Let f (x) be any cont. mono. increasing func. with prop. that
f (x) = integer =⇒ x = integer, prove that ⌊f (x)⌋ = f (⌊x⌋),
same as the ceiling.
• If x = ⌊x⌋, trivial.
• Otherwise x > ⌊x⌋ =⇒ f (x) > f (⌊x⌋). What about f (⌊x⌋)
and ⌊f (x)⌋?
• Assume this is true: f (⌊x⌋) < ⌊f (x)⌋, continuous,
• must be a number y s.t. x ≤ y < ⌈x⌉ and f (y) = ⌈f (x)⌉. By
the special property of x
• y integer, no number between x ≤ y < ⌈x⌉, hence they are
equal.
Same problem: cont. mono. decreasing, what’s that?
Basic concepts Solving Recurrences Mod: The binary Op
Example
Compute
X
1000
√
W = [ 3
n |n]
i=1
Basic concepts Solving Recurrences Mod: The binary Op
Example
Compute
X
1000
√
W = [ 3
n |n]
i=1
√
• Make a new one name for k = 3 n, getting
k | n, 1 ≤ n ≤ 1000.
√
• The range for k is k ≤ 3 n < k + 1
• k|n means that there is a m so that n = km.
P
• then becomes 1 + k,m [k 3 ≤ km ≤ (k + 1)3 ][1 ≤ k < 10].
Basic concepts Solving Recurrences Mod: The binary Op
Example
Compute
X
K
√
W = [ 3 n |n], K ∈ Z.
i=1
Basic concepts Solving Recurrences Mod: The binary Op
Example
Compute
X
K
√
W = [ 3 n |n], K ∈ Z.
i=1
P
• We should care about m [k 3 ≤ Km ≤ N ].
P
• this part become m [m ∈ [k 2 ..N /K]].
• the estimation will be 3/2N 2/3 + O(N 1/3 ).
Basic concepts Solving Recurrences Mod: The binary Op
Section 2
Solving Recurrences
Basic concepts Solving Recurrences Mod: The binary Op
K0 = 1;
Kn+1 = 1 + min(2K⌊n/2⌋ , 3K⌊n/3⌋ ).
K0 = 1;
Kn+1 = 1 + min(2K⌊n/2⌋ , 3K⌊n/3⌋ ).
JPG: Findings
1 2 31 4 5 62 7 8 93 10
11 124 13 14 155 16 17
186 19 20 217 22
23 24 8 25
26 279
28
29
3010
What will the id become?
• 1, 2 become ;
• 3 executed;
• 4, 5 become ;
• 6 is executed;
• 3k + 1, 3k + 2 will become ;
• 3k + 3 is executed.
Basic concepts Solving Recurrences Mod: The binary Op
JPG: Findings
1 2 31 4 5 62 7 8 93 10
11 124 13 14 155 16 17
186 19 20 217 22
23 24 8 25
26 279
28
29
3010
What will the id become?
• 1, 2 become n + 1, n + 2;
• 3 executed;
• 4, 5 become n + 3, n + 4;
• 6 is executed;
• 3k + 1, 3k + 2 will become n + 2k + 1, n + 2k + 2;
• 3k + 3 is executed.
Basic concepts Solving Recurrences Mod: The binary Op
JPG: Findings
1 2 31 4 5 62 7 8 93 10
11 124 13 14 155 16 17
186 19 20 217 22
23 24 8 25
26 279
28
29
3010
• Counting is consistent, no jumping over someone.
• The k-th person eliminated ends up with number 3k.
• To find the survivor = figure out the original number 3N .
Basic concepts Solving Recurrences Mod: The binary Op
JPG: Findings
1 2 31 4 5 62 7 8 93 10
11 124 13 14 155 16 17
186 19 20 217 22
23 24 8 25
26 279
28
29
3010
• What is 3N originally?
• N (N > n) has a form of N = n + 2k + 1 or N = n + 2k + 2,
in a single round.
• for two ks, getting k1 = (N − 1 − n)/2, k2 = (N − 2 − n)/2.
• = ⌊(N − n − 1)/2⌋.
Basic concepts Solving Recurrences Mod: The binary Op
JPG: Findings
1 2 31 4 5 62 7 8 93 10
11 12 4 13 14 5
15 16 17
186 19 20 217 22
23 24 8 25
26 279
28
29
3010
An algorithm for this:
• Let N ← 3n;
• while N > n, let N ← ⌊(N − n − 1)/2⌋ + N − n;
• Answer← N .
Basic concepts Solving Recurrences Mod: The binary Op
JPG: Findings
1 2 31 4 5 62 7 8 93 10
11 124 13 14 155 16 17
186 19 20 217 22
23 248 25
26 279
28
29
3010
Simplifying this algorithm: like treating arithmetic series.
• Assign the numbers from largest to smallest
• yielding ⌈3/2D⌉.
Basic concepts Solving Recurrences Mod: The binary Op
JPG: Findings
1 2 31 4 5 62 7 8 93 10
11 12 4 13 14 5
15 16 17
186 19 20 217 22
23 24 8 25
26 279
28
29
3010
Generalized: D = ⌈q/(q − 1)D⌉ for general qs, i.e. q-kill one.
Basic concepts Solving Recurrences Mod: The binary Op
Section 3
Mod: definition
n = m ⌊n/m⌋ + n mod m.
for y ̸= 0.
• generalize it to negative integers
• 5 mod 3 = 5 − 3 ⌊5/3⌋ = 2.
• 5 mod −3 = 5 − (−3) ⌊5/ − 3⌋ = −1.
• −5 mod 3 = −5 − (−3) ⌊−5/3⌋ = 1.
• −5 mod −3 = −5 − (−3) ⌊−5/ − 3⌋ = −2.
Basic concepts Solving Recurrences Mod: The binary Op
Mod: definition
n = m ⌊n/m⌋ + n mod m.
for y ̸= 0.
• Observation: In any case the result number is exactly in
between 2 numbers.
• Special definition: if y = 0, then x mod 0 = x.
• preserves the property that x and y always differs from x by a
multiple of y.
Basic concepts Solving Recurrences Mod: The binary Op
Properties
1 9 17 24 31
2 10 18 25 32
3 11 19 26 33
4 12 20 27 34
5 13 21 28 35
6 14 22 29 36
7 15 23 30 37
8 16
Basic concepts Solving Recurrences Mod: The binary Op
Find X j√ k
k
0≤k≤n
Find X j√ k
k
0≤k≤n
Find X j√ k
k
0≤k≤n
It looks that:
x + kn mod m kn kn mod m
+ −
m m m
Basic concepts Solving Recurrences Mod: The binary Op
jxk 0 0 mod m
+ −
m m m
x + n mod m n n mod m
+ + −
m m m
x + 2n mod m 2n 2n mod m
+ + −
m m m
.. ..
. .
x + (m − 1)n mod m (m − 1)n (m − 1)n mod m
+ + − .
m m } |
| {z m
{z }
| {z }
bn C
a⌊ x
a⌋
Basic concepts Solving Recurrences Mod: The binary Op
X nk + x X mk + x
= , integers m, n > 0.
m n
0⩽k<m 0⩽k<n
Thanks