Division algorithm


A division algorithm is an algorithm that computes the quotient and remainder of two integers. Its success is guaranteed by the following theorem:

For any two integers \(n\) and \(d\), where \(d > 0\), there exist unique integers \(q\) and \(r\), with \(0 \leq r \leq d - 1\), such that: $$n = qd + r$$ This formalizes integer division. You can write \(q\) and \(r\) in terms of \(n\) and \(d\) with the \(\text{div}\) and \(\bmod\) operations: $$n = (n \text{ div } d) \times d + (n \bmod d)$$

In other words, any integer \(n\) can be uniquely expressed as a linear combination of some quotient \(q\) and remainder \(r\), for every given positive divisor \(d.\) Likewise, if you isolate the remainder \(r\), it becomes a linear combination of \(n\) and \(d.\) The Euclidean algorithm is based on this fact.

Anyway, let's look at a specific division algorithm that will describe exactly how \(q = n \text{ div } d\) and \(r = n \bmod d\) are computed. It takes the form of repeated subtraction and depends on the sign of the dividend:

algorithm division is
input: two integers, n and d, where d > 0
output: two integers, q = n div d and r = n mod d

q ← 0
r ← n
if n ≥ 0 (the non-negative case)
while r ≥ d
q ← q + 1
r ← r - d
else (the negative case)
while r < 0
q ← q - 1
r ← r + d

So, if you ever find yourself confused on how to compute \(\bmod\), no worries. You can trust that this algorithm will always be correct.

⚠ Notice that this algorithm is consistent with the theorem above, ensuring that \(r\), the result of \(n \bmod d\), will always be a non-negative integer less than \(d.\) Perfect!
⚠ Here's a fact that might come in handy someday. For every positive integer \(n\), if \(d\) divides \(n\), then there are exactly \(\frac{n}{d}\) positive integers less than or equal to \(n\) that are also divisible by \(d.\)
Logic & Proofs
IntegerRational numberInequalityReal numberTheoremProofStatementProof by exhaustionUniversal generalizationCounterexampleExistence proofExistential instantiationAxiomLogicTruthPropositionCompound propositionLogical operationLogical equivalenceTautologyContradictionLogic lawPredicateDomainQuantifierArgumentRule of inferenceLogical proofDirect proofProof by contrapositiveIrrational numberProof by contradictionProof by casesSummationDisjunctive normal form
Set Theory
SetElementEmpty setUniversal setSubsetPower setCartesian productStringBinary stringEmpty stringSet operationSet identitySet proof
Functions
FunctionFloor functionCeiling functionInverse function
Algorithms
AlgorithmPseudocodeCommandAsymptotic notationTime complexityAtomic operationBrute-force algorithm
Relations
RelationReflexive relationSymmetric relationTransitive relationRelation compositionEquivalence relationEquivalence class
Number Theory
Integer divisionLinear combinationDivision algorithmModular arithmeticPrime factorizationGreatest common divisorLeast common multiplePrimality testFactoring algorithmEuclid's theoremPrime number theoremEuclidean algorithm
Induction
Proof by inductionFibonacci sequenceProof by strong inductionWell-ordering principleSequenceFactorialRecursive definition
Combinatorics
Rule of productRule of sumBijection rulePermutationCombinationComplement ruleExperimentOutcomeSample spaceEventProbabilityProbability distributionUniform distributionMultisetSixfold wayInclusion-exclusion principlePigeonhole principle
Graph Theory
GraphWalkSubgraphRegular graphComplete graphEmpty graphCycle graphHypercube graphBipartite graphComponentEulerian circuitEulerian trailHamiltonian cycleHamiltonian pathTreeHuffman treeSubstringForestPath graphStarSpanning treeWeighted graphMinimum spanning treeGreedy algorithmPrim's algorithm
Recursion
RecursionRecursive algorithmCorrectness proofDivide-and-conquer algorithmSorting algorithmMerge sort