math-exercises 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/package.json +2 -2
  2. package/src/exercises/calcul/addAndSub.js +19 -13
  3. package/src/exercises/calcul/addAndSub.ts +31 -23
  4. package/src/exercises/calcul/fractions/fractionAndIntegerDivision.js +35 -0
  5. package/src/exercises/calcul/fractions/fractionAndIntegerDivision.ts +38 -0
  6. package/src/exercises/calcul/fractions/fractionAndIntegerProduct.js +31 -0
  7. package/src/exercises/calcul/fractions/fractionAndIntegerProduct.ts +32 -0
  8. package/src/exercises/calcul/fractions/fractionAndIntegerSum.js +32 -0
  9. package/src/exercises/calcul/fractions/fractionAndIntegerSum.ts +31 -0
  10. package/src/exercises/calcul/fractions/fractionsDivision.js +29 -0
  11. package/src/exercises/calcul/fractions/fractionsDivision.ts +30 -0
  12. package/src/exercises/calcul/fractions/fractionsProduct.js +29 -0
  13. package/src/exercises/calcul/fractions/fractionsProduct.ts +29 -0
  14. package/src/exercises/calcul/fractions/fractionsSum.js +29 -0
  15. package/src/exercises/calcul/fractions/fractionsSum.ts +28 -0
  16. package/src/exercises/calcul/fractions/simplifyFraction.js +25 -0
  17. package/src/exercises/calcul/fractions/simplifyFraction.ts +24 -0
  18. package/src/exercises/calcul/operationsPriorities.js +89 -0
  19. package/src/exercises/calcul/operationsPriorities.ts +118 -0
  20. package/src/exercises/calcul/rounding/roundToUnit.js +70 -0
  21. package/src/exercises/calcul/rounding/roundToUnit.ts +68 -0
  22. package/src/exercises/calculLitteral/distributivity/allIdentities.js +2 -0
  23. package/src/exercises/calculLitteral/distributivity/allIdentities.ts +2 -0
  24. package/src/exercises/calculLitteral/distributivity/doubleDistributivity.js +3 -3
  25. package/src/exercises/calculLitteral/distributivity/doubleDistributivity.ts +2 -0
  26. package/src/exercises/calculLitteral/distributivity/firstIdentity.js +2 -0
  27. package/src/exercises/calculLitteral/distributivity/firstIdentity.ts +2 -0
  28. package/src/exercises/calculLitteral/distributivity/secondIdentity.js +3 -3
  29. package/src/exercises/calculLitteral/distributivity/secondIdentity.ts +2 -0
  30. package/src/exercises/calculLitteral/distributivity/simpleDistributivity.js +3 -3
  31. package/src/exercises/calculLitteral/distributivity/simpleDistributivity.ts +2 -0
  32. package/src/exercises/calculLitteral/distributivity/thirdIdentity.js +4 -2
  33. package/src/exercises/calculLitteral/distributivity/thirdIdentity.ts +2 -0
  34. package/src/exercises/calculLitteral/equation/equationType1Exercise.js +3 -3
  35. package/src/exercises/calculLitteral/equation/equationType1Exercise.ts +4 -5
  36. package/src/exercises/calculLitteral/equation/equationType2Exercise.js +3 -3
  37. package/src/exercises/calculLitteral/equation/equationType2Exercise.ts +5 -5
  38. package/src/exercises/calculLitteral/equation/equationType3Exercise.js +3 -3
  39. package/src/exercises/calculLitteral/equation/equationType3Exercise.ts +5 -5
  40. package/src/exercises/calculLitteral/equation/equationType4Exercise.js +3 -3
  41. package/src/exercises/calculLitteral/equation/equationType4Exercise.ts +7 -12
  42. package/src/exercises/calculLitteral/factorisation/factoType1Exercise.js +4 -9
  43. package/src/exercises/calculLitteral/factorisation/factoType1Exercise.ts +3 -6
  44. package/src/exercises/calculLitteral/reduction.ts +27 -0
  45. package/src/exercises/exercise.ts +2 -0
  46. package/src/exercises/exercises.js +83 -0
  47. package/src/exercises/exercises.ts +58 -0
  48. package/src/exercises/powers/powersDivision.js +46 -0
  49. package/src/exercises/powers/powersDivision.ts +51 -0
  50. package/src/exercises/powers/powersOfTenToDecimal.js +33 -0
  51. package/src/exercises/powers/powersOfTenToDecimal.ts +37 -0
  52. package/src/exercises/powers/powersPower.js +45 -0
  53. package/src/exercises/powers/powersPower.ts +55 -0
  54. package/src/exercises/powers/powersProduct.js +46 -0
  55. package/src/exercises/powers/powersProduct.ts +51 -0
  56. package/src/exercises/powers/scientificToDecimal.js +38 -0
  57. package/src/exercises/powers/scientificToDecimal.ts +44 -0
  58. package/src/exercises/squareRoots/simpifySquareRoot.js +2 -3
  59. package/src/exercises/squareRoots/simpifySquareRoot.ts +2 -0
  60. package/src/index.js +8 -8
  61. package/src/index.ts +5 -16
  62. package/src/mathutils/arithmetic/coprimesOf.js +13 -0
  63. package/src/mathutils/arithmetic/coprimesOf.ts +9 -0
  64. package/src/mathutils/arithmetic/dividersOf.ts +7 -0
  65. package/src/mathutils/arithmetic/lcd.js +12 -0
  66. package/src/mathutils/arithmetic/lcd.ts +7 -0
  67. package/src/mathutils/arithmetic/nonCoprimesOf.ts +9 -0
  68. package/src/mathutils/arithmetic/nonDividersOf.ts +12 -0
  69. package/src/mathutils/decimals/decimalPartLengthOf.ts +10 -0
  70. package/src/mathutils/random/randint.js +11 -2
  71. package/src/mathutils/random/randint.ts +10 -2
  72. package/src/numbers/decimals/decimal.js +144 -0
  73. package/src/numbers/decimals/decimal.ts +140 -0
  74. package/src/numbers/integer/integer.js +61 -4
  75. package/src/numbers/integer/integer.ts +58 -7
  76. package/src/numbers/integer/power.js +53 -0
  77. package/src/numbers/integer/power.ts +49 -0
  78. package/src/numbers/nombre.js +3 -2
  79. package/src/numbers/nombre.ts +2 -0
  80. package/src/numbers/rationals/rational.js +96 -10
  81. package/src/numbers/rationals/rational.ts +81 -15
  82. package/src/numbers/reals/real.js +2 -0
  83. package/src/numbers/reals/real.ts +3 -1
  84. package/src/numbers/reals/squareRoot.ts +3 -10
  85. package/src/polynomials/affine.ts +2 -6
  86. package/src/polynomials/polynomial.js +4 -13
  87. package/src/polynomials/polynomial.ts +8 -25
  88. package/src/tree/latexParser/latexParse.js +81 -34
  89. package/src/tree/latexParser/latexParse.ts +85 -39
  90. package/src/tree/nodes/functions/functionNode.js +19 -0
  91. package/src/tree/nodes/functions/functionNode.ts +18 -0
  92. package/src/tree/nodes/functions/oppositeNode.js +30 -0
  93. package/src/tree/nodes/functions/oppositeNode.ts +12 -0
  94. package/src/tree/nodes/functions/sqrtNode.js +24 -9
  95. package/src/tree/nodes/functions/sqrtNode.ts +5 -9
  96. package/src/tree/nodes/node.ts +1 -3
  97. package/src/tree/nodes/numbers/numberNode.js +3 -6
  98. package/src/tree/nodes/numbers/numberNode.ts +4 -7
  99. package/src/tree/nodes/operators/addNode.js +23 -8
  100. package/src/tree/nodes/operators/addNode.ts +3 -7
  101. package/src/tree/nodes/operators/divideNode.js +21 -9
  102. package/src/tree/nodes/operators/divideNode.ts +4 -11
  103. package/src/tree/nodes/operators/equalNode.js +20 -8
  104. package/src/tree/nodes/operators/equalNode.ts +3 -9
  105. package/src/tree/nodes/operators/fractionNode.js +34 -0
  106. package/src/tree/nodes/operators/fractionNode.ts +16 -0
  107. package/src/tree/nodes/operators/multiplyNode.js +20 -8
  108. package/src/tree/nodes/operators/multiplyNode.ts +3 -9
  109. package/src/tree/nodes/operators/operatorNode.js +35 -0
  110. package/src/tree/nodes/operators/operatorNode.ts +35 -2
  111. package/src/tree/nodes/operators/powerNode.js +20 -8
  112. package/src/tree/nodes/operators/powerNode.ts +3 -8
  113. package/src/tree/nodes/operators/substractNode.js +20 -8
  114. package/src/tree/nodes/operators/substractNode.ts +3 -9
  115. package/src/tree/nodes/variables/variableNode.js +1 -4
  116. package/src/tree/nodes/variables/variableNode.ts +4 -7
  117. package/src/utils/coin.js +7 -0
  118. package/src/utils/coin.ts +3 -0
  119. package/src/utils/shuffle.js +15 -4
  120. package/src/utils/shuffle.ts +7 -5
  121. package/src/exercises/calcul/priority.ts +0 -33
  122. package/src/expression/expression.js +0 -9
  123. package/src/expression/expression.ts +0 -13
  124. package/src/tree/nodes/operators/oppositeNode.ts +0 -17
@@ -0,0 +1,140 @@
1
+ import { randint } from "../../mathutils/random/randint";
2
+ import { Node } from "../../tree/nodes/node";
3
+ import { NumberNode } from "../../tree/nodes/numbers/numberNode";
4
+ import { Integer } from "../integer/integer";
5
+ import { Nombre, NumberType } from "../nombre";
6
+
7
+ export abstract class DecimalConstructor {
8
+ static randomFracPart(precision: number): string {
9
+ let decimals = "";
10
+ for (let i = 0; i < precision; i++) {
11
+ decimals += randint(i === precision - 1 ? 1 : 0, 10);
12
+ }
13
+ return decimals;
14
+ }
15
+
16
+ static random(min: number, max: number, precision: number): Decimal {
17
+ const int = randint(min, max) + "";
18
+ const decimals = DecimalConstructor.randomFracPart(precision);
19
+ return DecimalConstructor.fromParts(int, decimals);
20
+ }
21
+ static fromParts(intPart: string, decimalPart: string): Decimal {
22
+ return new Decimal(Number("" + intPart + "." + decimalPart));
23
+ }
24
+ }
25
+
26
+ export class Decimal implements Nombre {
27
+ value: number;
28
+ tex: string;
29
+ type = NumberType.Decimal;
30
+ precision: number;
31
+ intPart: number;
32
+ decimalPart: string;
33
+ constructor(value: number) {
34
+ this.value = value;
35
+ this.tex = value + "";
36
+ let [intPartString, decimalPartString] = (value + "").split(".");
37
+ this.intPart = Number(intPartString);
38
+ this.decimalPart = decimalPartString || "";
39
+ this.precision = this.decimalPart.length;
40
+ }
41
+
42
+ /**
43
+ *
44
+ * @param precision 0 = unité, 1 = dizieme, ... , -1 : dizaine
45
+ * @returns
46
+ */
47
+ round(precision: number): Nombre {
48
+ const intPartString = this.intPart + "";
49
+
50
+ if (precision < 0) {
51
+ if (precision < -intPartString.length) throw Error("can't round to higher precision");
52
+ return new Integer(this.intPart).round(-precision);
53
+ }
54
+
55
+ if (precision > this.precision) throw Error("can't round to higher precision");
56
+ if (precision === this.precision) return this;
57
+
58
+ let newFracPart = "",
59
+ newIntPart = "";
60
+
61
+ const shouldRoundUp = Number(this.decimalPart[precision]) > 4;
62
+
63
+ if (shouldRoundUp) {
64
+ let retenue = true;
65
+ let i = precision - 1;
66
+ while (retenue) {
67
+ if (i > -1) {
68
+ const nb = (Number(this.decimalPart[i]) + 1) % 10;
69
+ if (nb || newFracPart) {
70
+ newFracPart = nb.toString() + newFracPart;
71
+ }
72
+ if (nb !== 0) {
73
+ retenue = false;
74
+ for (let j = i - 1; j > -1; j--) {
75
+ newFracPart = this.decimalPart[j] + newFracPart;
76
+ }
77
+ newIntPart = intPartString;
78
+ } else i--;
79
+ } else {
80
+ const nb = (Number(intPartString[i + intPartString.length]) + 1) % 10;
81
+ newIntPart = nb + "" + newIntPart;
82
+ if (nb !== 0) {
83
+ retenue = false;
84
+ for (let j = i + intPartString.length - 1; j > -1; j--) {
85
+ newIntPart = intPartString[j] + newIntPart;
86
+ }
87
+ } else i--;
88
+ }
89
+ }
90
+ } else {
91
+ let retenue = true;
92
+ let i = precision - 1;
93
+ while (retenue) {
94
+ if (i > -1) {
95
+ const nb = Number(this.decimalPart[i]);
96
+ if (nb || newFracPart) {
97
+ newFracPart = nb.toString() + newFracPart;
98
+ }
99
+ if (nb !== 0) {
100
+ retenue = false;
101
+ for (let j = i - 1; j > -1; j--) {
102
+ newFracPart = this.decimalPart[j] + newFracPart;
103
+ }
104
+ newIntPart = intPartString;
105
+ } else i--;
106
+ } else {
107
+ newIntPart = intPartString;
108
+ retenue = false;
109
+ }
110
+ }
111
+ }
112
+ return DecimalConstructor.fromParts(newIntPart, newFracPart);
113
+ }
114
+
115
+ multiplyByPowerOfTen(power: number) {
116
+ let newIntPart = "",
117
+ newFracPart = "";
118
+
119
+ if (power > -1) {
120
+ newIntPart = this.intPart + "";
121
+ for (let i = 0; i < power; i++) {
122
+ newIntPart += i > this.decimalPart.length - 1 ? "0" : this.decimalPart[i];
123
+ }
124
+ newFracPart = this.decimalPart.slice(power);
125
+ } else {
126
+ const intPartString = this.intPart + "";
127
+ newFracPart = this.decimalPart;
128
+ for (let i = intPartString.length - 1; i > intPartString.length - 1 + power; i--) {
129
+ newFracPart = (i < 0 ? "0" : intPartString[i]) + newFracPart;
130
+ }
131
+ if (power + intPartString.length < 1) newIntPart = "0";
132
+ else newIntPart = intPartString.slice(0, power + intPartString.length);
133
+ }
134
+ return DecimalConstructor.fromParts(newIntPart, newFracPart);
135
+ }
136
+
137
+ toTree(): Node {
138
+ return new NumberNode(this.value);
139
+ }
140
+ }
@@ -1,14 +1,71 @@
1
1
  "use strict";
2
2
  exports.__esModule = true;
3
- exports.Integer = void 0;
3
+ exports.Integer = exports.IntegerConstructor = void 0;
4
+ var randint_1 = require("../../mathutils/random/randint");
4
5
  var numberNode_1 = require("../../tree/nodes/numbers/numberNode");
6
+ var nombre_1 = require("../nombre");
7
+ var rational_1 = require("../rationals/rational");
8
+ var IntegerConstructor = /** @class */ (function () {
9
+ function IntegerConstructor() {
10
+ }
11
+ IntegerConstructor.random = function (nbOfDigits) {
12
+ return (0, randint_1.randint)(0, Math.pow(10, nbOfDigits));
13
+ };
14
+ return IntegerConstructor;
15
+ }());
16
+ exports.IntegerConstructor = IntegerConstructor;
5
17
  var Integer = /** @class */ (function () {
6
- function Integer(value) {
18
+ function Integer(value, tex) {
7
19
  this.value = value;
8
- this.tex = value + "";
20
+ this.tex = tex || value + "";
21
+ this.type = nombre_1.NumberType.Integer;
9
22
  }
10
23
  Integer.prototype.toTree = function () {
11
- return new numberNode_1.NumberNode(this.value);
24
+ return new numberNode_1.NumberNode(this.value, this.tex);
25
+ };
26
+ Integer.prototype.round = function (precision) {
27
+ var intString = this.value + "";
28
+ if (precision >= intString.length || precision < 1)
29
+ throw Error("can't round to higher precision");
30
+ var newInt = "";
31
+ var shouldRoundUp = Number(intString[intString.length - precision]) > 4;
32
+ if (shouldRoundUp) {
33
+ for (var i_1 = 0; i_1 < precision; i_1++) {
34
+ newInt += "0";
35
+ }
36
+ var retenue = true;
37
+ var i = intString.length - precision - 1;
38
+ while (retenue) {
39
+ var nb = (Number(intString[i]) + 1) % 10;
40
+ newInt = "" + nb + newInt;
41
+ if (nb === 0) {
42
+ i--;
43
+ }
44
+ else {
45
+ retenue = false;
46
+ for (var j = i - 1; j > -1; j--) {
47
+ newInt = intString[j] + newInt;
48
+ }
49
+ }
50
+ }
51
+ }
52
+ else {
53
+ for (var i = 0; i < intString.length; i++) {
54
+ newInt += i < intString.length - precision ? intString[i] : "0";
55
+ }
56
+ }
57
+ return new Integer(Number(newInt));
58
+ };
59
+ Integer.prototype.divide = function (nb) {
60
+ switch (nb.type) {
61
+ case nombre_1.NumberType.Integer:
62
+ return new rational_1.Rational(this.value, nb.value).simplify();
63
+ case nombre_1.NumberType.Rational:
64
+ var rational = nb;
65
+ return new rational_1.Rational(this.value * rational.denum, rational.num).simplify();
66
+ default:
67
+ throw Error("not implemented");
68
+ }
12
69
  };
13
70
  return Integer;
14
71
  }());
@@ -1,21 +1,72 @@
1
- import { Expression } from "../../expression/expression";
1
+ import { randint } from "../../mathutils/random/randint";
2
2
  import { Node } from "../../tree/nodes/node";
3
3
  import { NumberNode } from "../../tree/nodes/numbers/numberNode";
4
- import { Nombre } from "../nombre";
4
+ import { Nombre, NumberType } from "../nombre";
5
+ import { Rational } from "../rationals/rational";
6
+
7
+ export abstract class IntegerConstructor {
8
+ static random(nbOfDigits: number) {
9
+ return randint(0, 10 ** nbOfDigits);
10
+ }
11
+ }
5
12
 
6
13
  export class Integer implements Nombre {
7
14
  value: number;
8
15
  tex: string;
16
+ type: NumberType;
9
17
 
10
- constructor(value: number) {
18
+ constructor(value: number, tex?: string) {
11
19
  this.value = value;
12
- this.tex = value + "";
20
+ this.tex = tex || value + "";
21
+ this.type = NumberType.Integer;
13
22
  }
14
23
 
15
24
  toTree(): Node {
16
- return new NumberNode(this.value);
25
+ return new NumberNode(this.value, this.tex);
17
26
  }
18
- // add(expression: Expression): Expression {
19
27
 
20
- // }
28
+ round(precision: number) {
29
+ const intString = this.value + "";
30
+
31
+ if (precision >= intString.length || precision < 1) throw Error("can't round to higher precision");
32
+
33
+ let newInt = "";
34
+ const shouldRoundUp = Number(intString[intString.length - precision]) > 4;
35
+ if (shouldRoundUp) {
36
+ for (let i = 0; i < precision; i++) {
37
+ newInt += "0";
38
+ }
39
+ let retenue = true;
40
+ let i = intString.length - precision - 1;
41
+
42
+ while (retenue) {
43
+ const nb = (Number(intString[i]) + 1) % 10;
44
+ newInt = "" + nb + newInt;
45
+ if (nb === 0) {
46
+ i--;
47
+ } else {
48
+ retenue = false;
49
+ for (let j = i - 1; j > -1; j--) {
50
+ newInt = intString[j] + newInt;
51
+ }
52
+ }
53
+ }
54
+ } else {
55
+ for (let i = 0; i < intString.length; i++) {
56
+ newInt += i < intString.length - precision ? intString[i] : "0";
57
+ }
58
+ }
59
+ return new Integer(Number(newInt));
60
+ }
61
+ divide(nb: Nombre): Nombre {
62
+ switch (nb.type) {
63
+ case NumberType.Integer:
64
+ return new Rational(this.value, nb.value).simplify();
65
+ case NumberType.Rational:
66
+ const rational = nb as Rational;
67
+ return new Rational(this.value * rational.denum, rational.num).simplify();
68
+ default:
69
+ throw Error("not implemented");
70
+ }
71
+ }
21
72
  }
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ exports.__esModule = true;
3
+ exports.Power = void 0;
4
+ var numberNode_1 = require("../../tree/nodes/numbers/numberNode");
5
+ var powerNode_1 = require("../../tree/nodes/operators/powerNode");
6
+ var nombre_1 = require("../nombre");
7
+ var integer_1 = require("./integer");
8
+ var Power = /** @class */ (function () {
9
+ function Power(a, b) {
10
+ this.operand = a;
11
+ this.power = b;
12
+ this.value = Math.pow(a, b);
13
+ this.tex = "".concat(a, "^{").concat(b, "}");
14
+ this.type = b < 0 ? nombre_1.NumberType.Rational : nombre_1.NumberType.Integer;
15
+ }
16
+ Power.prototype.simplify = function () {
17
+ if (this.power === 0)
18
+ return new numberNode_1.NumberNode(1);
19
+ if (this.power === 1)
20
+ return new numberNode_1.NumberNode(this.operand);
21
+ if (this.operand === 1)
22
+ return new numberNode_1.NumberNode(1);
23
+ if (this.operand === 0)
24
+ return new numberNode_1.NumberNode(0);
25
+ if (this.operand === -1)
26
+ return new numberNode_1.NumberNode(this.power % 2 === 0 ? 1 : -1);
27
+ return this.toTree();
28
+ };
29
+ Power.prototype.toDecimalWriting = function () {
30
+ if (this.operand !== 10)
31
+ throw Error("only implemented for powers of ten so far");
32
+ var s = "";
33
+ if (this.power > -1) {
34
+ s += "1";
35
+ for (var i = 0; i < this.power; i++) {
36
+ s += 0;
37
+ }
38
+ }
39
+ else {
40
+ s += "1";
41
+ for (var i = 1; i < Math.abs(this.power); i++) {
42
+ s = "0" + s;
43
+ }
44
+ s = "0." + s;
45
+ }
46
+ return new integer_1.Integer(Number(s), s);
47
+ };
48
+ Power.prototype.toTree = function () {
49
+ return new powerNode_1.PowerNode(new numberNode_1.NumberNode(this.operand), new numberNode_1.NumberNode(this.power));
50
+ };
51
+ return Power;
52
+ }());
53
+ exports.Power = Power;
@@ -0,0 +1,49 @@
1
+ import { Node } from "../../tree/nodes/node";
2
+ import { NumberNode } from "../../tree/nodes/numbers/numberNode";
3
+ import { PowerNode } from "../../tree/nodes/operators/powerNode";
4
+ import { Nombre, NumberType } from "../nombre";
5
+ import { Integer } from "./integer";
6
+
7
+ export class Power implements Nombre {
8
+ value: number;
9
+ tex: string;
10
+ type: NumberType;
11
+ operand: number;
12
+ power: number;
13
+ constructor(a: number, b: number) {
14
+ this.operand = a;
15
+ this.power = b;
16
+ this.value = Math.pow(a, b);
17
+ this.tex = `${a}^{${b}}`;
18
+ this.type = b < 0 ? NumberType.Rational : NumberType.Integer;
19
+ }
20
+ simplify(): Node {
21
+ if (this.power === 0) return new NumberNode(1);
22
+ if (this.power === 1) return new NumberNode(this.operand);
23
+ if (this.operand === 1) return new NumberNode(1);
24
+ if (this.operand === 0) return new NumberNode(0);
25
+ if (this.operand === -1) return new NumberNode(this.power % 2 === 0 ? 1 : -1);
26
+ return this.toTree();
27
+ }
28
+ toDecimalWriting(): Nombre {
29
+ if (this.operand !== 10) throw Error("only implemented for powers of ten so far");
30
+ let s = "";
31
+ if (this.power > -1) {
32
+ s += "1";
33
+ for (let i = 0; i < this.power; i++) {
34
+ s += 0;
35
+ }
36
+ } else {
37
+ s += "1";
38
+ for (let i = 1; i < Math.abs(this.power); i++) {
39
+ s = "0" + s;
40
+ }
41
+ s = "0." + s;
42
+ }
43
+
44
+ return new Integer(Number(s), s);
45
+ }
46
+ toTree(): Node {
47
+ return new PowerNode(new NumberNode(this.operand), new NumberNode(this.power));
48
+ }
49
+ }
@@ -4,6 +4,7 @@ exports.NumberType = void 0;
4
4
  var NumberType;
5
5
  (function (NumberType) {
6
6
  NumberType[NumberType["Integer"] = 0] = "Integer";
7
- NumberType[NumberType["Rational"] = 1] = "Rational";
8
- NumberType[NumberType["Real"] = 2] = "Real";
7
+ NumberType[NumberType["Decimal"] = 1] = "Decimal";
8
+ NumberType[NumberType["Rational"] = 2] = "Rational";
9
+ NumberType[NumberType["Real"] = 3] = "Real";
9
10
  })(NumberType = exports.NumberType || (exports.NumberType = {}));
@@ -3,11 +3,13 @@ import { NumberNode } from "../tree/nodes/numbers/numberNode";
3
3
 
4
4
  export enum NumberType {
5
5
  Integer,
6
+ Decimal,
6
7
  Rational,
7
8
  Real,
8
9
  }
9
10
  export interface Nombre {
10
11
  value: number;
11
12
  tex: string;
13
+ type: NumberType;
12
14
  toTree: () => Node;
13
15
  }
@@ -1,37 +1,123 @@
1
1
  "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
10
+ };
2
11
  exports.__esModule = true;
3
- exports.Rational = void 0;
12
+ exports.Rational = exports.RationalConstructor = void 0;
13
+ var coprimesOf_1 = require("../../mathutils/arithmetic/coprimesOf");
4
14
  var gcd_1 = require("../../mathutils/arithmetic/gcd");
15
+ var lcd_1 = require("../../mathutils/arithmetic/lcd");
16
+ var randint_1 = require("../../mathutils/random/randint");
5
17
  var numberNode_1 = require("../../tree/nodes/numbers/numberNode");
6
- var divideNode_1 = require("../../tree/nodes/operators/divideNode");
18
+ var fractionNode_1 = require("../../tree/nodes/operators/fractionNode");
19
+ var random_1 = require("../../utils/random");
20
+ var shuffle_1 = require("../../utils/shuffle");
7
21
  var integer_1 = require("../integer/integer");
22
+ var nombre_1 = require("../nombre");
23
+ var RationalConstructor = /** @class */ (function () {
24
+ function RationalConstructor() {
25
+ }
26
+ /**
27
+ * @param maxGcd max number by which the fraction is simplifiable
28
+ */
29
+ RationalConstructor.randomSimplifiable = function (maxGcd) {
30
+ if (maxGcd === void 0) { maxGcd = 10; }
31
+ var gcd = (0, randint_1.randint)(2, maxGcd);
32
+ var max = (0, randint_1.randint)(3, 11);
33
+ var min = (0, random_1.random)((0, coprimesOf_1.coprimesOf)(max));
34
+ var _a = (0, shuffle_1.shuffle)([gcd * min, gcd * max]), num = _a[0], denum = _a[1];
35
+ if (denum === gcd) {
36
+ //si 10/2 on transforme en 2/10
37
+ return new Rational(denum, num);
38
+ }
39
+ return new Rational(num, denum);
40
+ };
41
+ RationalConstructor.randomIrreductible = function (max) {
42
+ if (max === void 0) { max = 11; }
43
+ var a = (0, randint_1.randint)(2, max);
44
+ var b = (0, random_1.random)(__spreadArray(__spreadArray([], (0, coprimesOf_1.coprimesOf)(a), true), [1], false));
45
+ if (b === 1)
46
+ return new Rational(b, a);
47
+ var _a = (0, shuffle_1.shuffle)([a, b]), num = _a[0], denum = _a[1];
48
+ return new Rational(num, denum);
49
+ };
50
+ return RationalConstructor;
51
+ }());
52
+ exports.RationalConstructor = RationalConstructor;
8
53
  var Rational = /** @class */ (function () {
9
54
  function Rational(numerator, denumerator) {
55
+ if (denumerator === 0)
56
+ throw Error("division by zero");
10
57
  this.num = numerator;
11
58
  this.denum = denumerator;
12
59
  this.value = numerator / denumerator;
13
60
  this.isSimplified = Math.abs((0, gcd_1.gcd)(numerator, denumerator)) === 1;
14
61
  this.tex = "\\frac{".concat(this.num, "}{").concat(this.denum, "}");
62
+ this.type = nombre_1.NumberType.Rational;
15
63
  }
16
64
  Rational.prototype.toTex = function () {
17
65
  return "\\frac{".concat(this.num, "}{").concat(this.denum, "}");
18
66
  };
19
- Rational.prototype.add = function (expression) {
20
- return this;
67
+ Rational.prototype.add = function (nb) {
68
+ switch (nb.type) {
69
+ case nombre_1.NumberType.Integer: {
70
+ var num = this.num + this.denum * nb.value;
71
+ return new Rational(num, this.denum).simplify();
72
+ }
73
+ case nombre_1.NumberType.Rational: {
74
+ var rational = nb;
75
+ var ppcm = (0, lcd_1.lcd)(rational.denum, this.denum);
76
+ var num = this.num * (ppcm / this.denum) + rational.num * (ppcm / rational.denum);
77
+ return new Rational(num, ppcm).simplify();
78
+ }
79
+ }
80
+ throw Error("not implemented yet");
81
+ };
82
+ Rational.prototype.multiply = function (nb) {
83
+ switch (nb.type) {
84
+ case nombre_1.NumberType.Integer: {
85
+ var num = this.num * nb.value;
86
+ var denum = this.denum;
87
+ return new Rational(num, denum).simplify();
88
+ }
89
+ case nombre_1.NumberType.Rational: {
90
+ var rational = nb;
91
+ var num = this.num * rational.num;
92
+ var denum = this.denum * rational.denum;
93
+ return new Rational(num, denum).simplify();
94
+ }
95
+ }
96
+ throw Error("not implemented yet");
21
97
  };
22
- Rational.prototype.multiply = function (expression) {
23
- return this;
98
+ Rational.prototype.divide = function (nb) {
99
+ switch (nb.type) {
100
+ case nombre_1.NumberType.Integer: {
101
+ var denum = this.denum * nb.value;
102
+ return new Rational(this.num, denum).simplify();
103
+ }
104
+ case nombre_1.NumberType.Rational: {
105
+ var rational = nb;
106
+ var num = this.num * rational.denum;
107
+ var denum = this.denum * rational.num;
108
+ return new Rational(num, denum).simplify();
109
+ }
110
+ }
111
+ throw Error("not implemented yet");
24
112
  };
25
113
  Rational.prototype.opposite = function () {
26
114
  return new Rational(-this.num, this.denum);
27
115
  };
28
116
  Rational.prototype.toTree = function () {
29
- return new divideNode_1.DivideNode(new numberNode_1.NumberNode(this.num), new numberNode_1.NumberNode(this.denum));
117
+ return new fractionNode_1.FractionNode(new numberNode_1.NumberNode(this.num), new numberNode_1.NumberNode(this.denum));
30
118
  };
31
119
  Rational.prototype.simplify = function () {
32
- var sign = (this.num > 0 && this.denum > 0) || (this.num < 0 && this.denum < 0)
33
- ? 1
34
- : -1;
120
+ var sign = (this.num > 0 && this.denum > 0) || (this.num < 0 && this.denum < 0) ? 1 : -1;
35
121
  var div = Math.abs((0, gcd_1.gcd)(this.num, this.denum));
36
122
  if (Math.abs(this.denum) === div)
37
123
  return new integer_1.Integer(this.num / this.denum);