Binary array sum by recursion 2026-10-18
Keep an array unchanged, and sum entries in the half-open integer interval of array indices . The precondition requires integers and exact, terminating integer addition. An empty range returns the empty sum zero; a one-entry range returns that entry. Every longer range splits at its middle array index:For the original array , call . The left half returns , the right half returns , and the original invocation returns . The right half's left endpoint increases from zero to two: what decreases is range length , not every argument. Negative entries affect the answer but do not affect algorithm termination.
S(A, l, r):
require integer indices with 0 <= l <= r <= length(A)
size = r - l
if size == 0:
return 0
if size == 1:
return A[l]
middle = l + floor(size / 2)
left_sum = S(A, l, middle)
right_sum = S(A, middle, r)
return left_sum + right_sumSeven calls compute an array sum with only three frames active at once
. Original binary tree of invocations for , with integer intervals labeling ranges and upward arrows labeling returned sums. Small numbers give the order in which invocations begin when the left child runs first. The highlighted graph path contains the three active stack frames while the entry is read; the result from the completed left sibling is retained by its parent. Details.For range length , the children have lengths and , both positive and strictly less than . The floor function chooses the split; the ceiling function gives the other length. Algorithm termination follows by this natural number measure. For algorithm correctness, apply mathematical induction on : the two base cases return the specified sum; for a larger range, the two disjoint child ranges cover every original array index exactly once, so addition of their correct sums gives the required sum.
For , the invocation tree is a full binary tree with leaves, one per array entry. If is its number of nonleaf vertices, counting edges gives , so . Thus the total invocation count is , and there are combining additions. Here gives seven calls and three combining additions. For , there is one call and no combining addition; for , there is also one call and no combining addition, but no entry leaf, so the formula does not apply.
The call stack holds the active branch, rather than the entire invocation tree. Execute the left child completely before the right child, retaining the left result in the parent stack frame. The maximum number of active stack frames is for , and one for . Balanced splitting therefore needs frames for , compared with the frames of the factorial recursive process. Assuming constant-cost array access, index operations and addition, its time complexity is and its auxiliary space complexity is machine words for , excluding the stored input. Pass the same array with different endpoints, rather than copying slices. These bounds require all stored indices and partial sums to fit in a constant number of machine words; exact unbounded integer addition and storage require a separate bit cost analysis.
Brauer progression theorem Created 2026-10-05 Updated 2026-10-14
For positive integers , some ensures that every -color finite coloring of contains a monochromatic setIt follows from the Van der Waerden theorem by mathematical induction on the number of colors. If is a bound for colors, take a long one-color arithmetic progression of length and step . Either one of has its color, giving the result immediately, or these multiples use only colors. Pull back their finite coloring to and use mathematical induction, then multiply the resulting configuration by . Enlarge the ambient finite integer interval to include all these multiples. The cases and are immediate. The parameter makes this useful for positive monochromatic solutions of a partition regular equation with arbitrary integer coefficients.
Compactness bound for partition regularity 2026-10-05
If a rational matrix is a partition regular matrix, then for each fixed number of colors some finite integer interval already forces a monochromatic positive solution. Otherwise the solution-free finite colorings of successive intervals form a finitely branching tree with every level nonempty. The König infinity lemma gives an infinite branch, contradicting partition regularity. More generally, this argument applies to any family of configurations each using finitely many positive integers.
Counting sort prefix ranges 2026-10-18
For sorting records with integer sort keys in , where , let count the input occurrences of sort key . Define two prefix sums:The empty sum gives . Here are counts of sorting records. Exactly sorting records have smaller sort keys, so the output positions for sort key form the half-open integer intervalIts right endpoint is excluded: the last occupied array index is when .[ref]
11.2 Counting Sort and Radix Sort. Open Data Structures: An Introduction by Pat Morin; §11.2.1 Counting Sort, paragraphs after Figure 11.7: running sums count keys at most k and determine the output block
For the seven-sorting record example, the frequency array, inclusive cumulative count array, and start array areThus sort keys zero, one, two and three occupy the integer intervals , , and , respectively. In particular, the three sorting records with sort key two belong at array indices three, four and five. The inclusive cumulative count counts all six sorting records with sort keys at most two; six is the exclusive endpoint, not the final position of a sorting record with sort key two.
These integer intervals fit together because for , and . Their lengths add to , so they partition all output positions without overlaps or gaps. An absent sort key contributes an empty integer interval: with frequencies , the endpoints are and sort key two has range . For empty input, every count and endpoint is zero; every reserved integer interval is empty. This makes the same formulas cover missing sort keys and the empty case.
Integer count in a closed interval 2026-10-14
For real numbers , the integers in the closed interval begin at and end at , if there are any. Their number is thereforeThe extra one includes both ends of the resulting integer interval. The maximum with zero handles an empty intersection: contains no integer, even though it has positive length.
For , the ceiling function selects and the floor function selects , giving . The four points are . For , the count is five, not four: the real length and the number of integer points are different quantities.
Integer interval Created 2026-10-05 Updated 2026-10-06
A finite integer interval is a set with integers . Its length is . Infinite integer intervals can also be defined by allowing one or both endpoints to be infinite; a bounded-gaps condition on words always quantifies over finite integer intervals.
Matrix-chain dynamic programming 2026-10-18
Let have matrix dimensions , where are positive integers. For , define as the minimum scalar multiplication cost of the contiguous matrix product . The endpoints of an integer interval identify a dynamic programming state. A single matrix needs no matrix multiplication, giving the base case . At the final matrix multiplication, split after :The two subproducts have matrix dimensions and , regardless of their internal grouping. The recurrence relation adds their optimal costs and the final scalar multiplication cost.[ref]
Lecture 21: Dynamic Programming III. 6.006 Introduction to Algorithms, Fall 2011 by Massachusetts Institute of Technology, 6.006 course staff; Lecture 21: Dynamic Programming III, printed p. 2, Parenthesization: final split, recurrence and zero one-matrix base case; notes use half-open indices
Why is this optimal substructure? For a chosen final split , replacing either subproduct's grouping by a cheaper one leaves its matrix dimensions and exact matrix product unchanged, so it decreases the whole cost by the same amount. Therefore an optimal grouping can use optimal groupings on both sides. Conversely, each candidate in the recurrence relation joins two achievable groupings. Mathematical induction on , starting with the single-matrix base case, proves that the minimum is achievable and no grouping is cheaper.
There are also overlapping subproblems: while evaluating the different final splits of , the cases and both request the cost of . Recursion without reuse recomputes this answer; memoization or a dynamic programming table stores it once. Fill the table by increasing length , so both dependencies are ready. Record an attaining split alongside each cost.[ref]
Lecture 21: Dynamic Programming III. 6.006 Introduction to Algorithms, Fall 2011 by Massachusetts Institute of Technology, 6.006 course staff; Lecture 21: Dynamic Programming III, printed p. 2, Parenthesization: increasing substring size, cubic time and parent pointers
The dynamic programming table has cost entries. For length , there are entries and possible splits, so the exact number of candidate evaluations isUnder the unit-cost assumption that arithmetic operations, comparisons and access to stored costs each take constant time, this gives time complexity and space complexity , using Big O notation. These bounds concern choosing a grouping from the matrix dimensions; they exclude the subsequent matrix multiplication work. If the dimensions or costs require arbitrarily long integers, their arithmetic and storage costs must also be counted.
Matrix-chain split reconstruction 2026-10-18
Extend the original example by a matrix with matrix dimensions , so . The Catalan number counts all possible groupings. Their scalar multiplication costs areThe optimum is , with cost . Enumeration makes a useful check here, but dynamic programming reaches the same answer by solving ten integer intervals, including the four single-matrix base cases:For example, . The three final-split candidates for the full matrix product supply a compact certificate:The first candidate already uses the better grouping of ; the worse need not be reconsidered at the top level. This is how optimal substructure lets the table represent all five possibilities with only three final candidates.
Read , which separates from . Then separates from , and separates from . This recursion stops at every , returning the corresponding matrix name. Combining the returned expressions recovers , rather than only its cost. Every recursive call uses a shorter integer interval, establishing algorithm termination; a tied minimum may choose any attaining split. The recovered binary tree has leaves and internal nodes, so reconstruction takes steps. Its attraction is the separation between an enormous family of groupings and a small set of reusable dynamic programming states.
Suppose a candidate integer interval contains possible values and an active array segment contains exactly all occurrences from that interval, with entries. The pigeonhole principle guarantees a missing value. Split the value interval into widths and use array partitioning to obtain occurrence counts . If , retain the left side. Otherwise , so retain the right side. This works with duplicate entries because it needs only the inequality between occurrences and possible values.
Balancing the value widths gives , while the active scan length remains less than . The total work is bounded by a geometric series and logarithmic loop overhead, hence time complexity. On reaching width one, the retained occurrence count is zero, proving that the sole candidate is absent globally. This is a binary search over deficient value intervals, with local in-place array partitioning, rather than repeated full-array scans.
Natural density Created 2026-10-05 Updated 2026-10-13
For a set of nonnegative integers, its natural density is the limiting proportion of the first nonnegative integers that belong to :provided this limit exists. Write for the counting function. The numerator is a finite cardinality; the denominator is exactly , including zero. Every existing natural density lies between zero and one.
For the even nonnegative integers , there are elements below and below . Thus using the ceiling function, and . For the square numbers , the exact count for isThe floor function counts the allowed indices , and the extra one counts zero. At , the square numbers below ten are , so the proportion is ; at , it is . The count grows like the square root of , while the denominator grows like .
The prime numbers also have natural density zero. If is their set, the Prime number theorem gives , hence . The Prime number theorem is a substantial result about the count, rather than a consequence of the fact that there are infinitely many prime numbers. [ref]
https://math.mit.edu/classes/18.785/2025/LectureNotes16.pdf. Andrew V. Sutherland, MIT 18.785 lecture 16, Riemann's zeta function and the prime number theorem, 2025
Why use natural density when cardinality already measures size? The even nonnegative integers, the square numbers, and the prime numbers all have the same countably infinite cardinality, yet their proportions among large initial integer intervals differ. Natural density distinguishes frequent membership from sparse membership. Equivalently, under a uniform distribution on a finite set of possible nonnegative integers, is the probability of landing in . A set without natural density shows why this limiting proportion need not exist.
Past exam of the mathematics course of the University of Cambridge 2017 iii Paper 130 2 ii Solution Created 2026-10-03 Updated 2026-10-05
Let the required arithmetic progression have length , let be the number of colors, and take from the Hales-Jewett theorem. Given any finite coloring of , color a word over an alphabet by . For a monochromatic combinatorial line, let be its active coordinate set and let be the sum of its fixed coordinates. The sums of its words areThey all lie in , have one color, and form an arithmetic progression with strictly positive common difference . Thus, writing for the finite Van der Waerden theorem bound,The length-one case is immediate. Restricting a finite coloring of the positive integers to this finite integer interval gives the infinite-domain formulation of the Van der Waerden theorem as well.
Past exam of the mathematics course of the University of Cambridge 2017 iii Paper 130 3 i Solution Created 2026-10-03 Updated 2026-10-05
Use the standard definition of a partition regular matrix: for every finite coloring of the positive integers, there is a positive solution whose coordinates all have one color. Coordinates may repeat. Clearing denominators lets us assume ; multiplication of the row by a nonzero rational number changes neither its zero-sum subsets nor its solutions. We prove both directions of the Rado theorem for one equation directly, without invoking any form of Rado's theorem.
For necessity, choose a prime number and use the last nonzero digit coloring: write , with , and assign color . Given a monochromatic solution, let , let , and let be the common color. Divide by and reduce modulo . Terms outside vanish; the others giveThe nonzero residue is invertible modulo the prime number , so divides . This sum has absolute value less than , forcing it to be zero. The set is nonempty by its definition.
For sufficiency we first derive the needed Brauer progression theorem from the permitted Van der Waerden theorem. Fix positive integers . We claim that for every number of colors , a finite integer interval forces a monochromatic setThe case is immediate by taking and . For and , take in an interval of length at least . Suppose the claim holds for and write . By the Van der Waerden theorem, some forces an arithmetic progression of length in with one color . Work in so that all needed multiples also lie in the coloring's domain. If a subprogression of length and step has of color , we are done. Otherwise, for every , the initial long arithmetic progression contains such a subprogression, and avoids color . The induced finite coloring of therefore uses at most colors. The induction hypothesis gives a monochromatic set for this induced finite coloring. Multiplying by gives the desired configuration in the original coloring, with initial term and step . This proves the claim for ; the only use below has .
Now suppose for a nonempty . Choose , put , and put . If , then and any constant positive vector is already a monochromatic solution. Otherwise take and apply the proved Brauer progression theorem with . All of and for have one color. DefineEvery coordinate is a positive integer in that monochromatic set, since is either or . Using the zero sum on givesThus both directions are proved:For the right side is impossible, agreeing with the absence of positive solutions for a single nonzero coefficient.
Past exam of the mathematics course of the University of Cambridge 2017 iii Paper 130 4 i Solution Created 2026-10-03 Updated 2026-10-05
Use the product topology on , with discrete, and the left shift . A basic cylinder set specifies finitely many coordinates. This is a compact metric space, and is a homeomorphism. Write for the forward orbit closure. A minimal point means that is a minimal dynamical system, equivalently that every point of has a dense forward orbit in ; it need not be a fixed point.
There is a convention needed in the source: the bounded-gaps property concerns every finite integer interval , hence every finite word over an alphabet. Literally allowing would make the displayed condition impossible for finite , although a constant coloring is a minimal point. Under the standard finite-word interpretation, the property is precisely uniform recurrence.
Suppose first that is a minimal dynamical system. Its nonempty compact forward-invariant subset must equal . As the ambient left shift is invertible, all integer translates of belong to . Let have length , and let . This is a nonempty clopen set. Each forward orbit in meets , so covers . By compactness, finitely many suffice; let be the largest index in this finite cover. For any integer , the point lies in , so some satisfies . The prescribed word therefore occurs at positions , entirely inside . Taking proves the bounded-gaps property for every interval of length at least .
Conversely suppose is uniformly recurrent. Every finite word from any integer translate of occurs arbitrarily far to the right, so every such translate belongs to . Moreover, the property that every length- block contains a specified word of passes to every : a finite block of is a limit of blocks of forward translates of , and the finite discrete alphabet forces eventual exact agreement on that block. Given , look for its word inside . An occurrence starts at for some , so agrees with on . Taking for arbitrarily large proves that belongs to the forward orbit closure of every . That closure is a closed forward-invariant subset of , so it also contains every forward translate of and hence all of . Every forward orbit is therefore dense in , provingThe same conclusion holds if orbit closure is defined using all integer iterates: the bounded-gaps condition makes the forward and two-sided closures equal.
Radix sort key encoding 2026-10-18
An encoding for radix sort must make the intended total order of sort keys agree with the lexicographic order of its fixed-width numerical digit tuples. The positional notation for bounded nonnegative integers has this property. At the most significant differing position , a one-unit numerical digit difference contributes , while all lower positions together contribute at mostThis geometric series shows why lower numerical digits cannot reverse that comparison. Leading zero padding works here because it preserves place values and the represented integer.
For signed integers in a known integer interval , encode and choose with . This order-preserving function is also injective: exactly when . For , the four-bit encodings of are after adding eight. Restore the original signed sort keys when displaying the complete sorting records; the encoding need not replace their data. Sorting the unsigned values of raw two's complement bit patterns would put nonnegative integers before negative ones, so their representation alone is not this ordered encoding.
For variable-length strings, first specify the alphabet order and whether a proper prefix precedes its extensions. As an original construction, use , let every string have length at most three, encode as one and as two, and append zeros to width three. Zero is reserved for an end marker strictly below every real symbol. The strings encode as in radix three, which preserves their lexicographic order. A differing real symbol decides the comparison; at a prefix boundary, the shorter string's zero comes before the extension's one or two. Left padding would instead encode as and as , reversing . Padding raw bytes with zero also fails if zero is an allowed data symbol: distinct strings can then receive the same encoding. A reserved end marker and an explicit symbol ordering are essential; byte order alone does not establish a desired human text order.
Stable ties preserve lower digits; suitable encodings preserve the intended order
. Original counterexample above: reversing a tens tie destroys the units order and a later stable hundreds pass cannot repair it. Below: signed integer offsets and a reserved end marker for bounded strings produce increasing fixed-width encodings. All entries are exact constructed examples. Details. Set without natural density 2026-10-14
Include the positive integers in , and exclude the intervening integer intervals and zero. Equivalently, include for . Each included block is large enough to raise the running proportion; the following excluded block lowers it again.
With , the exact boundary counts follow from a geometric series:The first sum is empty at . Dividing by the corresponding boundary, rather than using one denominator for both, givesFor example, , whereas , yielding proportions and . For , the exact count is ; for it stays . Hence the proportion increases through each included block and decreases through each excluded block. These boundary subsequences give the actual limit inferior and limit superior:Since these differ, has no natural density. Its upper asymptotic density still records its largest limiting proportion.
Stable record placement in counting sort 2026-10-18
Initialize a mutable endpoint array to the inclusive cumulative counts . Scan input array indices . For each complete sorting record with sort key , first decrement , then write at output array index . This is equivalent to writing at the old endpoint minus one and then decrementing the endpoint.[ref]
11.2 Counting Sort and Radix Sort. Open Data Structures: An Introduction by Pat Morin; §11.2.1 Counting Sort, final scan paragraph and stability paragraph after Theorem 11.7
In the worked example, the first input sorting record read is at array index six. Its endpoint changes from to , so the complete sorting record goes to . Next, changes from seven to six and goes to . Writing before the decrement at the old endpoint would put outside its reserved integer interval and try to put beyond the output array. All seven writes are:Each endpoint changes only when a sorting record with its own sort key is placed. Consequently, stays at five through the placements of before dropping to four for .
Here is a complete implementation under the stated input contract; each sorting record is a pair whose first component is its integer sort key:
def counting_sort_records(records, K):
end = [0] * K
output = [None] * len(records)
for key, data in records:
end[key] += 1
for key in range(1, K):
end[key] += end[key - 1]
for i in range(len(records) - 1, -1, -1):
key = records[i][0]
end[key] -= 1
output[end[key]] = records[i]
return outputThe same array
end successively stores frequencies, inclusive cumulative counts, and moving exclusive endpoints; the formulas describe their mathematical values without requiring three stored arrays. The write copies the complete sorting record, including its associated data. In particular, two occurrences of an identical pair still cause two separate writes.For a short algorithm correctness proof, let be the number of already placed sorting records with sort key . A loop invariant isAll integer intervals here mean output array indices. Initially , so each whole reserved integer interval is unfilled. When the next sorting record has sort key , fewer than such occurrences have been placed, giving . Decrementing selects a fresh slot inside its reserved integer interval. At termination, and : every output slot has been filled exactly once with a complete input occurrence. The disjoint, increasing sort key ranges establish sortedness, while the fresh-slot property establishes sorting permutation preservation.
For stable sorting, take two input occurrences with equal sort keys at array indices . The backward scan visits first and puts it farther right in their shared integer interval. When is visited, the endpoint has decreased at least once, so its output position is smaller. Thus their original order survives. A forward scan with this same decrement rule reverses every equal-sort key group: it would put at positions five, four, three. A forward scan can instead be stable if it starts from and fills each integer interval left to right, incrementing after each write. The scan direction and endpoint convention must agree.
Count initialization takes writes, output initialization takes writes, counting takes updates, the prefix sum pass takes updates, and placement takes writes. Hence time complexity and auxiliary storage are in Big O notation, assuming constant-cost sort key access, integer arithmetic, array indexing and complete sorting record movement. Large records can instead be represented by constant-size references.[ref]
11.2 Counting Sort and Radix Sort. Open Data Structures: An Introduction by Pat Morin; §11.2.1 Counting Sort, auxiliary counter and output arrays, loop-cost discussion and Theorem 11.7
For example, three sorting records with sort keys zero, and zero require one million counters if ; unused sort keys still incur initialization and prefix sum work. The bound is therefore linear in the input length only when . Empty input skips the record scans, but this implementation still initializes counters and accumulates their prefix sums. For nonempty input, counting all records and filling their ranges gives a concrete termination argument.
Direct sort key indexing is a different operation from a binary comparison. The comparison sorting lower bound concerns comparison sorts; counting sort uses its restricted integer sort keys as array indices. Its small- speed is particularly useful for categorical numerical labels and for each stable sorting pass of radix sort: the reserved integer intervals turn group sizes into exact destinations.
Uniform recurrence Created 2026-10-05 Updated 2026-10-14
A two-sided sequence over a finite alphabet is uniformly recurrent if every finite word over an alphabet occurring in it occurs with bounded gaps. More precisely, for each such word some ensures that every length- interval contains a complete occurrence. This is equivalent to being a minimal point of the full shift: a finite cover of the orbit closure by preimages of a word's cylinder set bounds its return gaps; conversely, bounded gaps pass to all points of the orbit closure and make every forward orbit dense there. The property concerns finite words, not infinite integer intervals.
Upper asymptotic density Created 2026-10-06 Updated 2026-10-14
For a subset of the positive integers, its upper asymptotic density isThe limit superior keeps the largest limiting proportion even when the running proportions oscillate. Because they lie in , this limit superior always exists in . If natural density exists, upper asymptotic density equals it.
The convention here uses , while natural density above uses . Replacing one interval by the other changes a count by at most one, so the proportions differ by at most and have the same limit superior. For the set without natural density above, the count in is six rather than five, because is included and zero is absent. Nevertheless its upper asymptotic density is exactly . The even positive integers have upper asymptotic density and the prime numbers have upper asymptotic density zero.
The useful feature is that a large proportion along arbitrarily large initial integer intervals already supplies structure. The Szemerédi theorem says that positive upper asymptotic density forces arithmetic progressions of every finite length, with positive common difference, even without an existing natural density. Szemerédi's 1975 paper resolved this general finite-length problem; it makes density a bridge from counting to additive structure. [ref]
https://doi.org/10.4064/aa-27-1-199-245. Endre Szemerédi, On sets of integers containing k elements in arithmetic progression, Acta Arithmetica 27, 199–245, 1975





