Binary fraction 2026-10-25
A base-two positional fraction with a binary point separating nonnegative and negative powers of two. For example, . A finite expansion is a dyadic rational: with fractional places, its value is an integer divided by . Conversely, any nonnegative reduced fraction whose denominator is a power of two has a finite binary expansion. Leading integer zeroes and trailing fractional zeroes do not change its value.
Character large sieve Created 2026-10-06 Updated 2026-10-07
For coefficients supported on consecutive integers,
The star restricts to primitive Dirichlet characters. Their finite Fourier transform of a primitive Dirichlet character has normalization of absolute value . Orthogonality of Dirichlet characters therefore bounds the weighted contribution for one modulus by . Sum over and use the exponential-sum large sieve on the distinct reduced fractions of denominators at most .
Coprime integers Created 2026-09-24 Updated 2026-10-14
Two integers are coprime when their greatest common divisor is one:
For positive integers, this says that the only positive common divisor is one. For signed integers, use , with absolute values; changing either sign leaves coprimality unchanged. With the convention , zero is coprime precisely to , because . Both are coprime to every integer. [ref]
The word concerns shared prime factors, not whether either input is a prime number. For instance and are coprime composite numbers, even though neither is a square-free integer. Conversely, and are square-free integers but are not coprime. Consecutive integers are always coprime: any common divisor of divides their difference one. Thus provide another pair of coprime composite numbers.
For positive integers, let be the prime support of an integer. The equivalent test is
The set intersection of the two prime supports of an integer must be the empty set. Indeed, any positive common divisor greater than one has a prime factor, and any shared prime factor is a common divisor greater than one. Hence coprimality depends on which prime factors occur, rather than on their prime valuations; replacing either input by its radical of an integer preserves coprimality.
For a list of at least two integers, distinguish pairwise coprimality, requiring every pair to be coprime, from joint coprimality, requiring only the greatest common divisor of the entire list to be one. For example,
No prime factor belongs to all three prime supports of an integer, yet each pair shares one. By contrast, have pairwise coprimality. The distinction is presented by Poritz under the names pairwise and mutually relatively prime. [ref]
Figure 1. . Filled cells mark membership in the prime support of an integer; empty cells mean the prime number does not divide the row label. The first panel has two coprime integers; the middle has joint coprimality but fails pairwise coprimality; the last has pairwise coprimality. The columns exhaust all prime factors of these eight row entries. This original finite arithmetic diagram shows exact divisibility, not sampled probabilities. Details.
One use is simplifying fractions. For a fraction with integer entries and , divide its numerator and denominator by to obtain a reduced fraction with coprime entries. For example, since , and uses the same . A further common divisor would make too small, so the reduction is complete. In particular, the reduced fraction for zero is .
Another use is reversible modular arithmetic: for an integer modulus , an integer has a modular inverse precisely when are coprime. The proof outline uses Bézout's identity: makes a modular inverse, and any inverse relation forces every common divisor to divide one. For example, gives , whereas has no modular inverse modulo . Thus coprimality is the exact condition allowing division by a residue in modular arithmetic. [ref]
For a reduced fraction with , , and coprime integers, its decimal representation repeats immediately after the decimal point. The shortest repeating block has length
Here denotes multiplicative order, and denotes Euler's totient function. Changing the numerator preserves the decimal period as long as the fraction remains reduced.[ref]
For example, long division of gives the remainders , with emitted digits . Consequently
Each remainder determines both the next digit and the next remainder; returning to one restarts the same six steps.
Figure 1.
Long-division remainders explain the six-digit repeat of one seventh and the two-digit prefix of one twenty-eighth
. Details.
More generally, initialize long division with . At each step, division with remainder gives
so . Since are coprime integers, the modular inverse of shows that exactly when . To see why the digit block cannot repeat sooner than the remainders, a purely repeating decimal with a block of digits represents for an integer . A reduced fraction with that value forces . Hence the shortest digit repeat and the multiplicative order coincide.
Knowing the decimal period predicts how many digits must be computed and stored: one block determines the whole repeating decimal. It also constructs the block directly. With , the integer
is the block, padded on the left with zeros to digits. For , and , so the block is and . Reduction matters: has decimal period one, whereas .
If the reduced denominator contains prime factors two or five, ten has no multiplicative order modulo that whole denominator. Write
The decimal preperiod has exactly digits. If , the remaining repeating decimal has decimal period . For instance, gives : two prefix digits, then six repeating digits. If , the decimal representation is a terminating decimal with exactly fractional places. We use the representation ending in zeros, rather than its alternative ending in nines; for example terminates after three places.
A short derivation explains both cases. After digits, multiplying the reduced fraction by and discarding its integer part leaves a reduced fraction whose denominator is . This denominator first becomes coprime to ten at . It then equals , giving an immediate repeat when , or an integer when . The same long division argument works for positional notation in any base : for a reduced denominator coprime to , the shortest repeating block has length .
Decimal preperiod 2026-10-14
The shortest prefix before the repeating tail of a decimal representation. For a reduced fraction with denominator , where is coprime to ten, its length is ; the tail length is given by decimal period from multiplicative order.
Fraction Created 2026-10-14 Updated 2026-10-20
A fraction represents the quotient of its numerator by its nonzero denominator . For integers this is a rational number. Different fractions can represent the same number: . Dividing both entries by their greatest common divisor gives a reduced fraction, with the sign convention that the denominator is positive.
For a fixed prime , the grid has circular spacing . Consequently an arc of length contains at most three points of this grid, including endpoints. The standard Chebyshev estimate for the prime-counting function gives
Since , the local-multiplicity large sieve yields the prime-denominator large sieve:
By contrast, distinct reduced fractions with denominators at most have circular distance at least : their difference, even after subtraction of an integer, has a nonzero integer numerator over denominator . Applying part (a) alone gives only . The local-multiplicity argument saves a factor of .
For a positive remaining fraction , the condition is equivalent to . Since decreases as the positive integer increases, choose . The zero fraction needs no term. This is the greedy Egyptian fraction expansion.
The remaining fraction is
The defining inequalities give . Therefore the unreduced new numerator is a nonnegative integer strictly smaller than the old positive numerator. Passing to a reduced fraction can only reduce it further. That numerator is a loop variant, so the process terminates. The increasing denominators are not the termination argument; their size need not be bounded in advance.
An Egyptian fraction need not be proper: several positive unit fractions may sum to at least one. Accordingly, accumulate a general nonnegative reduced fraction, rather than calling the proper-input-only Reduce on every intermediate value. First define an exact helper:
def toRatExact(e: Array[Int]): (BigInt, BigInt) = {
  var n = BigInt(0)
  var d = BigInt(1)
  var i = 0
  while (i < e.length) {
    require(e(i) > 0)
    val x = BigInt(e(i))
    val g = d.gcd(x)
    val nextN = n * (x / g) + d / g
    val nextD = d * (x / g)
    val h = nextN.gcd(nextD)
    n = nextN / h
    d = nextD / h
    i += 1
  }
  (n, d)
}

def toRat(e: Array[Int]): (Int, Int) = {
  val result = toRatExact(e)
  require(result._1.isValidInt && result._2.isValidInt,
          "Reduced result does not fit (Int, Int)")
  (result._1.toInt, result._2.toInt)
}
The loop invariant is , , , , and
Writing , the fraction for the extended prefix has numerator and denominator . These are the nextN and nextD in the code. Reducing them preserves the value and restores coprimality. The loop variant is . At exit the helper returns the exact sum in lowest terms, including for the empty array.
There are three distinct ways to control integer overflow: cancel the common denominator factor before multiplication, reduce after each addition, and use arbitrary-precision arithmetic when the remaining values still exceed fixed-width bounds. Cancellation reduces intermediate size but cannot guarantee that an exact answer fits (Int, Int). For example, the sum is in lowest terms, whose denominator exceeds Int.MaxValue. No implementation returning that fixed type can represent every permitted input exactly. The wrapper therefore checks this unavoidable result-type limitation instead of silently truncating; toRatExact supplies the unrestricted mathematical result.[ref]
When ,
An arc of length contains at most three points of each grid of denominator . The Chebyshev estimate gives such primes. Apply the local-multiplicity large sieve. Using only the separation of distinct reduced fractions gives the weaker .
Stern-Brocot tree 2026-10-20
A binary tree enumerating every positive reduced fraction exactly once. Begin with boundary fractions and ; between neighboring fractions , insert their mediant and recurse into the two new intervals. The determinant relation is preserved, and the new denominator is .
Terminating decimal 2026-10-14
A decimal representation with only finitely many nonzero fractional digits. A reduced fraction has this property exactly when its denominator has no prime factors other than two and five. The zero tail is distinguished from the genuine repeating block measured by decimal period from multiplicative order.
Consider an original abstract data type whose values are rational numbers. Store an ordered pair of integers, interpreted as a numerator and a denominator. Use exact, unbounded integer arithmetic in this model. The storage space, representation invariant and abstraction function are
The representation invariant does not require a positive denominator or a reduced fraction. Consequently , and are three different concrete states with the same abstract rational number . Similarly, and both represent zero. The abstraction function is a surjection: every rational number has an integer numerator and a nonzero integer denominator, so every abstract value has a representation.
Figure 1.
Several valid integer pairs represent one rational value; zero denominators have no abstract value
. Details.
Both and violate the representation invariant; neither has a value under this abstraction function. By contrast, is completely valid. Simplifying it to the reduced fraction may be a useful implementation choice, but is not a requirement of this representation. Strengthening the representation invariant to require a positive denominator and a greatest common divisor of one would choose a unique stored representative for each rational number; every constructor and operation would then need to establish that stronger condition.
Equality of abstract values is therefore different from equality of stored ordered pairs. For valid and ,
This defines an equivalence relation on : each equivalence class contains all representations of one rational number. An API equality operation can compare these cross-products using exact integer arithmetic. Comparing the two fields individually would incorrectly declare and unequal as rational numbers. The general reason is that an abstraction function may map several concrete values to one abstract value.[ref]