Course Test · University: upper level
Number theory: 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, numtheory. 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.Bézout coefficientsAgreesFind and integers with .Independent answer, , , . So the gcd is 18, and back-substituting gives .RatMath code
numtheory.egcd(252, 198)Output{ g: 18n, x: 4n, y: -5n }Every other numtheory function takes plain numbers, butegcdneeds BigInts:egcd(252n, 198n)gives{ g: 18n, x: 4n, y: -5n }. - 2.Linear congruenceAgreesSolve .Independent answer, so and .RatMath code
numtheory.solveLinear(17, 3, 29).solutionsOutput[7n]
- 3.Chinese remainder theoremAgreesFind the least positive with , , .Independent answerfrom the first and third; : 23 is . So (mod 105).RatMath code
numtheory.crt([2, 3, 2], [3, 5, 7])Output{ x: 23n, modulus: 105n } - 4.Euler's totientAgreesCompute .Independent answer: .RatMath code
numtheory.totient(360)Output96n
- 5.Fermat's little theoremAgreesFind .Independent answer, , so .RatMath code
numtheory.modPow(3, 100, 7)Output4n
- 6.Divisor functionsAgreesCompute and . Is 28 perfect?Independent answerDivisors 1, 2, 4, 7, 14, 28: , , so 28 is perfect.RatMath code
[numtheory.sigma(28), numtheory.tau(28), numtheory.isPerfect(28)]Output[56n, 6n, true]
- 7.Möbius functionAgreesCompute and .Independent answerhas three distinct primes: . has a square factor: .RatMath code
[numtheory.mobius(30), numtheory.mobius(12)]Output[-1n, 0n]
- 8.Legendre symbolAgreesEvaluate .Independent answerBy reciprocity . (Indeed .)RatMath code
numtheory.legendre(5, 11)Output1
- 9.Jacobi symbolAgreesEvaluate the Jacobi symbol . Is 2 a square mod 15?Independent answer, but 2 is not a square mod 3, so it is not a square mod 15: the Jacobi symbol 1 does not imply a square.RatMath code
[numtheory.jacobi(2, 15), numtheory.sqrtsMod(2, 15)]Output[1, []]
- 10.Square roots mod pAgreesSolve .Independent answer, so , that is 6 and 7.RatMath code
numtheory.sqrtsMod(10, 13)Output[6n, 7n]
- 11.Order of an elementAgreesFind the multiplicative order of 2 modulo 11.Independent answerThe order divides 10. , , so the order is 10.RatMath code
numtheory.multiplicativeOrder(2, 11)Output10n
- 12.Primitive rootsAgreesFind the least primitive root modulo 13.Independent answer, , so : 2 has order 12 and is a primitive root.RatMath code
numtheory.primitiveRoot(13)Output2n
- 13.Fermat numbersAgreesFactor .Independent answerEuler: (both prime).RatMath code
numtheory.formatFactors(numtheory.factor(2n ** 32n + 1n))Output641 · 6700417
- 14.Carmichael numbersAgreesShow 561 is composite but for every coprime to 561.Independent answer, and all divide 560 (Korselt), so the Carmichael function divides 560.RatMath code
[numtheory.formatFactors(numtheory.factor(561)), numtheory.carmichael(561), 560 % Number(numtheory.carmichael(561))]Output["3 · 11 · 17", 80n, 0]
- 15.Sums of two squaresAgreesWrite 65 as a sum of two squares in every way ().Independent answer: .RatMath code
numtheory.twoSquaresAll(65)Output[[8n, 1n], [7n, 4n]]
- 16.Linear Diophantine equationsAgreesFind all solutions of in non-negative integers.Independent answerneeds : give .RatMath code
numtheory.nonNegativeSolutions(3, 5, 47).solutionsOutput[[4n, 7n], [9n, 4n], [14n, 1n]]
- 17.Frobenius numberAgreesChicken nuggets come in boxes of 6, 9 and 20. What is the largest number that cannot be bought exactly?Independent answer44 = 24 + 20, 45 = 5·9, 46 = 6 + 40, 47 = 27 + 20, 48 = 8·6, 49 = 9 + 40 are all possible, and any later number adds 6 to one of these. 43 is not possible (43, 23 and 3 are not sums of 6s and 9s). Answer: 43.RatMath code
numtheory.frobenius([6, 9, 20])Output43n
- 18.Continued fractions and Pell's equationAgreesFind the continued fraction of , and the smallest positive solution of .Independent answer. For : , convergent : , so .RatMath code
const cf = numtheory.periodicContinuedFraction(numtheory.fromQuadratic(0, 1, 13, 1)); const p = numtheory.pell(7); [cf.preperiod, cf.period, p.x, p.y]Output[[3n], [1n, 1n, 1n, 1n, 6n], 8n, 3n]
Bothalgebraic.sqrt(13n)and the displayed quadratic representation give initial term 3 and repeating block 1, 1, 1, 1, 6. Check the Pell solution independently by computing 8² − 7·3² = 1.Fix: ratmath/algebraic#9, the arithmetic reads plain numbers, sosqrt(48)works - 19.Legendre's formulaAgreesWhat is the largest power of 2 dividing ?Independent answer.RatMath code
numtheory.factorialValuation(100, 2)Output97
factorialValuationapplies Legendre's formula without building .Fix: ratmath/numtheory#10,factorialValuation(Legendre's formula) - 20.Prime countingAgreesHow many primes are below 1000?Independent answer.RatMath code
numtheory.primePi(1000)Output168n