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. 1.TautologyAgrees
    Is ((p→q)∧(q→r))→(p→r)((p \to q) \land (q \to r)) \to (p \to r) a tautology?
    Independent answer
    Yes (hypothetical syllogism): the only way to make p→rp \to r false is pp true, rr false, and then one of the hypotheses is false.
    RatMath code
    logic.classify("((p -> q) & (q -> r)) -> (p -> r)").kind
    Output
    tautology
  2. 2.De MorganAgrees
    Show that ¬(p∧q)≡¬p∨¬q\lnot(p \land q) \equiv \lnot p \lor \lnot q.
    Independent answer
    Both are false exactly when pp and qq are both true.
    RatMath code
    logic.equivalent("!(p & q)", "!p | !q").status
    Output
    equivalent
  3. 3.Inclusion–exclusionAgrees
    How many integers from 1 to 1000 are divisible by 3, 5 or 7?
    Independent answer
    333+200+142−66−47−28+9=543333 + 200 + 142 - 66 - 47 - 28 + 9 = 543.
    RatMath code
    const upTo = (d) => range(d, 1000, d);
    discrete.inclusionExclusion([upTo(3), upTo(5), upTo(7)].map((l) => l.map(String))).union
    Output
    543n
  4. 4.Counting handsAgrees
    How many five-card poker hands contain exactly two pairs?
    Independent answer
    (132)(42)2⋅11⋅4=78⋅36⋅44=123,552\binom{13}{2}\binom42^2\cdot 11\cdot 4 = 78 \cdot 36 \cdot 44 = 123{,}552.
    RatMath code
    discrete.binomial(13, 2) * discrete.binomial(4, 2) ** 2n * 11n * 4n
    Output
    123552n
  5. 5.Arrangements with repetitionAgrees
    How many arrangements of the letters of MISSISSIPPI are there?
    Independent answer
    11!4! 4! 2!=34,650\dfrac{11!}{4!\,4!\,2!} = 34{,}650.
    RatMath code
    discrete.multinomial(1, 4, 4, 2)
    Output
    34650n
  6. 6.Stars and barsAgrees
    How many solutions in non-negative integers has x1+x2+x3+x4=10x_1 + x_2 + x_3 + x_4 = 10?
    Independent answer
    (133)=286\binom{13}{3} = 286.
    RatMath code
    discrete.multichoose(4, 10)
    Output
    286n
  7. 7.DerangementsAgrees
    In how many ways can 6 letters go into 6 envelopes with none in its own envelope?
    Independent answer
    D6=6!(1−1+12−16+124−1120+1720)=265D_6 = 6!\left(1 - 1 + \tfrac12 - \tfrac16 + \tfrac1{24} - \tfrac1{120} + \tfrac1{720}\right) = 265.
    RatMath code
    discrete.derangementCount(6)
    Output
    265n
  8. 8.Linear recurrenceAgrees
    Solve an=5an−1−6an−2a_n = 5a_{n-1} - 6a_{n-2}, a0=1a_0 = 1, a1=4a_1 = 4.
    Independent answer
    Roots 2 and 3: an=2⋅3n−2na_n = 2\cdot 3^n - 2^n.
    RatMath code
    discrete.recurrence({ coefficients: [5, -6], initial: [1, 4] }).closedForm().toString()
    Output
    a(n) = −2^n + 2·3^n
  9. 9.A sum proved by inductionAgrees
    Find a closed form for ∑k=1nk3\sum_{k=1}^n k^3.
    Independent answer
    (n(n+1)2)2\left(\dfrac{n(n + 1)}{2}\right)^2.
    RatMath code
    symbolic.factor(symbolic.sum("k^3", "k", 1, "n").value)
    Output
    n^2*(n + 1)^2/4
  10. 10.Modular exponentAgrees
    Find 7222 mod 117^{222} \bmod 11.
    Independent answer
    710≡17^{10} \equiv 1 (Fermat); 222=22⋅10+2222 = 22\cdot10 + 2: 72=49≡57^2 = 49 \equiv 5.
    RatMath code
    numtheory.modPow(7n, 222n, 11n)
    Output
    5n
  11. 11.Euclid and BézoutAgrees
    Find gcd⁡(252,198)\gcd(252, 198) and integers with 252x+198y=gcd⁡252x + 198y = \gcd.
    Independent answer
    gcd⁡=18=4⋅252−5⋅198\gcd = 18 = 4\cdot252 - 5\cdot198.
    RatMath code
    numtheory.egcd(252n, 198n)
    Output
    { g: 18n, x: 4n, y: -5n }
  12. 12.Modular inverseAgrees
    Find the inverse of 17 modulo 43.
    Independent answer
    17⋅38=646=15⋅43+117 \cdot 38 = 646 = 15\cdot43 + 1: 38.
    RatMath code
    numtheory.modInverse(17n, 43n)
    Output
    38n
  13. 13.Chinese remainder theoremAgrees
    Solve x≡2(mod3)x \equiv 2 \pmod 3, x≡3(mod5)x \equiv 3 \pmod 5, x≡2(mod7)x \equiv 2 \pmod 7.
    Independent answer
    x≡23(mod105)x \equiv 23 \pmod{105}.
    RatMath code
    numtheory.crt([2, 3, 2], [3, 5, 7])
    Output
    { x: 23n, modulus: 105n }
  14. 14.Euler circuitsAgrees
    Does K5K_5 have an Euler circuit? Does K4K_4?
    Independent answer
    K5K_5: every degree is 4 (even) and it is connected, so yes. K4K_4: degrees are 3, so no.
    RatMath code
    [discrete.eulerTour(discrete.complete(5)).exists, discrete.eulerTour(discrete.complete(4)).exists]
    Output
    [true, false]
  15. 15.Shortest pathAgrees
    Edges 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 answer
    A–C–B–D–E, length 2+1+5+2=102 + 1 + 5 + 2 = 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]]);
    const p = discrete.shortestPath(g, "A", "E");
    [p.path, p.distance]
    Output
    [["A", "C", "B", "D", "E"], 10]
  16. 16.Minimum spanning treeAgrees
    Find the weight of a minimum spanning tree of the same graph.
    Independent answer
    Kruskal: 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).weight
    Output
    10
  17. 17.Counting treesAgrees
    How many labelled trees are there on 5 vertices?
    Independent answer
    Cayley: 53=1255^{3} = 125.
    RatMath code
    discrete.spanningTreeCount(discrete.complete(5))
    Output
    125n
  18. 18.Equivalence relationsAgrees
    Is R={(1,1),(2,2),(3,3),(1,2),(2,1)}R = \{(1,1), (2,2), (3,3), (1,2), (2,1)\} an equivalence relation on {1,2,3}\{1, 2, 3\}? If so, give its classes.
    Independent answer
    Yes (reflexive, symmetric, transitive): classes {1,2}\{1, 2\} and {3}\{3\}.
    RatMath code
    discrete.relation([1, 2, 3], [[1, 1], [2, 2], [3, 3], [1, 2], [2, 1]]).classes()
    Output
    [[1, 2], [3]]
  19. 19.Number basesAgrees
    Write 2026 in binary.
    Independent answer
    2026=1024+512+256+128+64+32+8+2=1111110101022026 = 1024 + 512 + 256 + 128 + 64 + 32 + 8 + 2 = 11111101010_2.
    RatMath code
    R`2026`.toBase(BaseSystem.fromBase(2))
    Output
    11111101010
    toBase wants a BaseSystem, not the number 2 (R`2026`.toBase(2) throws "Argument must be a BaseSystem").
  20. 20.Pigeonhole principleAgrees
    How many people must be in a room to be sure that three share a birth month?
    Independent answer
    2⋅12+1=252\cdot 12 + 1 = 25.
    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" }