Course Test · University: first two years
Discrete mathematics: RatMath answers
The same twenty exercises, answered with RatMath Core and the Shelf libraries only. The code below was run when this page was built; its output is shown as printed, and compared with the independent answer.
Agrees 20
Libraries used here: core, symbolic, discrete, numtheory, logic. In this code each library is in scope under its own name (linalg.det, stats.tTest), and Core's names (R, Rational, …) are in scope directly.
- 1.TautologyAgreesIs a tautology?Independent answerYes (hypothetical syllogism): the only way to make false is true, false, and then one of the hypotheses is false.RatMath code
logic.classify("((p -> q) & (q -> r)) -> (p -> r)").kindOutputtautology
- 2.De MorganAgreesShow that .Independent answerBoth are false exactly when and are both true.RatMath code
logic.equivalent("!(p & q)", "!p | !q").statusOutputequivalent
- 3.Inclusion–exclusionAgreesHow many integers from 1 to 1000 are divisible by 3, 5 or 7?Independent answer.RatMath code
const upTo = (d) => range(d, 1000, d); discrete.inclusionExclusion([upTo(3), upTo(5), upTo(7)].map((l) => l.map(String))).unionOutput543n
- 4.Counting handsAgreesHow many five-card poker hands contain exactly two pairs?Independent answer.RatMath code
discrete.binomial(13, 2) * discrete.binomial(4, 2) ** 2n * 11n * 4nOutput123552n
- 5.Arrangements with repetitionAgreesHow many arrangements of the letters of MISSISSIPPI are there?Independent answer.RatMath code
discrete.multinomial(1, 4, 4, 2)Output34650n
- 6.Stars and barsAgreesHow many solutions in non-negative integers has ?Independent answer.RatMath code
discrete.multichoose(4, 10)Output286n
- 7.DerangementsAgreesIn how many ways can 6 letters go into 6 envelopes with none in its own envelope?Independent answer.RatMath code
discrete.derangementCount(6)Output265n
- 8.Linear recurrenceAgreesSolve , , .Independent answerRoots 2 and 3: .RatMath code
discrete.recurrence({ coefficients: [5, -6], initial: [1, 4] }).closedForm().toString()Outputa(n) = −2^n + 2·3^n
- 9.A sum proved by inductionAgreesFind a closed form for .Independent answer.RatMath code
symbolic.factor(symbolic.sum("k^3", "k", 1, "n").value)Outputn^2*(n + 1)^2/4
- 10.Modular exponentAgreesFind .Independent answer(Fermat); : .RatMath code
numtheory.modPow(7n, 222n, 11n)Output5n
- 11.Euclid and BézoutAgreesFind and integers with .Independent answer.RatMath code
numtheory.egcd(252n, 198n)Output{ g: 18n, x: 4n, y: -5n } - 12.Modular inverseAgreesFind the inverse of 17 modulo 43.Independent answer: 38.RatMath code
numtheory.modInverse(17n, 43n)Output38n
- 13.Chinese remainder theoremAgreesSolve , , .Independent answer.RatMath code
numtheory.crt([2, 3, 2], [3, 5, 7])Output{ x: 23n, modulus: 105n } - 14.Euler circuitsAgreesDoes have an Euler circuit? Does ?Independent answer: every degree is 4 (even) and it is connected, so yes. : degrees are 3, so no.RatMath code
[discrete.eulerTour(discrete.complete(5)).exists, discrete.eulerTour(discrete.complete(4)).exists]Output[true, false]
- 15.Shortest pathAgreesEdges with weights: AB 4, AC 2, BC 1, BD 5, CD 8, CE 10, DE 2. Find the shortest path from A to E.Independent answerA–C–B–D–E, length .RatMath code
const g = discrete.graph([["A", "B", 4], ["A", "C", 2], ["B", "C", 1], ["B", "D", 5], ["C", "D", 8], ["C", "E", 10], ["D", "E", 2]]); const p = discrete.shortestPath(g, "A", "E"); [p.path, p.distance]Output[["A", "C", "B", "D", "E"], 10]
- 16.Minimum spanning treeAgreesFind the weight of a minimum spanning tree of the same graph.Independent answerKruskal: BC 1, AC 2, DE 2, (AB 4 makes a cycle), BD 5: weight 10.RatMath code
const g = discrete.graph([["A", "B", 4], ["A", "C", 2], ["B", "C", 1], ["B", "D", 5], ["C", "D", 8], ["C", "E", 10], ["D", "E", 2]]); discrete.minimumSpanningTree(g).weightOutput10
- 17.Counting treesAgreesHow many labelled trees are there on 5 vertices?Independent answerCayley: .RatMath code
discrete.spanningTreeCount(discrete.complete(5))Output125n
- 18.Equivalence relationsAgreesIs an equivalence relation on ? If so, give its classes.Independent answerYes (reflexive, symmetric, transitive): classes and .RatMath code
discrete.relation([1, 2, 3], [[1, 1], [2, 2], [3, 3], [1, 2], [2, 1]]).classes()Output[[1, 2], [3]]
- 19.Number basesAgreesWrite 2026 in binary.Independent answer.RatMath code
R`2026`.toBase(BaseSystem.fromBase(2))Output11111101010
toBasewants aBaseSystem, not the number 2 (R`2026`.toBase(2)throws "Argument must be a BaseSystem"). - 20.Pigeonhole principleAgreesHow many people must be in a room to be sure that three share a birth month?Independent answer.RatMath code
discrete.pigeonhole(25, 12)Output{ atLeast: 3n, atMost: 2n, forced: true, statement: "25 items in 12 boxes: some box holds at least 3" }