Added optimisations to numbersToExpressions option. Updated tests

This commit is contained in:
sanex3339
2020-07-12 19:05:24 +03:00
parent dd8dcac223
commit 1beb3cd774
8 changed files with 21 additions and 8 deletions
@@ -19,6 +19,11 @@ export class NumberNumericalExpressionAnalyzer implements INumberNumericalExpres
*/
private static readonly additionalParts: number = 3;
/**
* @type {Map<number, number[]>}
*/
private readonly numberFactorsMap: Map<number, number[]> = new Map();
/**
* @type {IRandomGenerator}
*/
@@ -103,11 +108,16 @@ export class NumberNumericalExpressionAnalyzer implements INumberNumericalExpres
private mixWithMultiplyParts (number: number): number | number[] {
const shouldMixWithMultiplyParts: boolean = this.randomGenerator.getMathRandom() > 0.5;
if (!shouldMixWithMultiplyParts) {
if (!shouldMixWithMultiplyParts || number === 0) {
return number;
}
const factors: number[] = NumberUtils.getFactors(number);
let factors: number[] | null = this.numberFactorsMap.get(number) ?? null;
if (!factors) {
factors = NumberUtils.getFactors(number);
this.numberFactorsMap.set(number, factors);
}
if (!factors.length) {
return number;
+2 -1
View File
@@ -82,12 +82,13 @@ export class NumberUtils {
const factors: number[] = [];
const root: number = Math.sqrt(number);
const isEven: boolean = number % 2 === 0;
const incrementValue: number = isEven ? 1 : 2;
for (
let currentFactor = 1;
Math.pow(currentFactor, 2) <= number;
currentFactor <= root;
currentFactor += incrementValue
) {
if (number % currentFactor !== 0) {