Here is my solution to the Project Euler 101 problem (https://projecteuler.net/problem=101)… I know I don’t actually have to implement a full matrix solver but I did it whatsoever, but now my answer is a bit off, can anyone please tell me where I got it wrong?
I created a function to solve the equations. For example, it returns [6, -11, 6] for 6a^2 -11b + c for the (3, n) for n^3. Somehow the final answer is slightly off
// OP's functions
let equationsResult = [];
let equations = [];
let numbersFound = [];
let sumOfNumbers = 0;
let result = 0;
let equationsLength = 0;
let finalResult = 0;
const generateEquations = (seq, length, numOfEquations) => {
equations = [];
for (let a = 1; a < numOfEquations + 1; a++) {
for (let b = length - 1; b >= 0; b--) {
equations.push(a ** b);
};
equations.push(a ** seq);
}
};
const createEquations = (seq, length) => {
generateEquations(seq, length, length);
toSimplifyEquations(length, length);
numbersFound = [];
numbersFound.push(equations[equations.length - 1] / equations[0]);
findNextNumber(seq, length);
};
const simplifyEquations = (arr, cLength, dLength, length) => {
for (let c = 0; c < cLength - 1; c++) {
for (let d = 0; d < dLength + 1; d++) {
equationsResult.push(arr[(c * (dLength + 1)) + d + length + 1] - arr[(c * (dLength + 1)) + d]);
};
};
};
const toSimplifyEquations = (length, eLength) => {
for (let e = 0; e < eLength - 1; e++) {
simplifyEquations(equations, length - e, length, length);
equations = equationsResult;
equationsResult = [];
};
};
const findNextNumber = (seq, length) => {
if (numbersFound.length === length - 1) {
sumOfNumbers = 0;
numbersFound.forEach((num) => sumOfNumbers += num);
numbersFound.push(1 - sumOfNumbers);
} else {
equations = [];
generateEquations(seq, length, length - numbersFound.length);
for (let g = 0; g < equations.length; g++) {
if ((g + 1) % (length + 1) === 0) {
for (let j = 0; j < numbersFound.length; j++) {
equations[g] -= numbersFound[j] * equations[g - length + j];
};
};
};
equationsLength = equations.length / (length + 1);
for (let h = 0; h < equationsLength; h++) {
equations.splice(h * (length + 1 - (length - equationsLength)), length - equationsLength);
};
toSimplifyEquations(length - numbersFound.length, length - numbersFound.length);
numbersFound.push(equations[equations.length - 1] / equations[0]);
findNextNumber(seq, length);
};
};
const optimumPolynomialOfN = (n) => {
result = 1;
for (let i = 2; i < n + 1; i++) {
createEquations(n, i);
for (let j = 0; j < numbersFound.length; j++) {
result += ((i + 1) ** (numbersFound.length - j - 1)) * numbersFound[j];
};
};
return result;
};
const optimumPolynomial = () => {
finalResult = 1;
for (let k = 0; k < 10; k += 2) {
finalResult += optimumPolynomialOfN(k + 2) - optimumPolynomialOfN(k + 1);
};
return finalResult;
};
<!-- Include Mocha CSS for test results styling -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/10.2.0/mocha.min.css">
<!-- Div where Mocha will render the test results -->
<div id="mocha"></div>
<!-- Include Mocha JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/10.2.0/mocha.min.js"></script>
<!-- Include Chai JS for assertions -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.3.7/chai.min.js"></script>
<script>
// Initialize Mocha
mocha.setup('bdd');
const expect = chai.expect;
// Test suite
describe('Polynomial Calculation Tests', function() {
beforeEach(function() {
// Reset variables before each test
equationsResult = [];
equations = [];
numbersFound = [];
sumOfNumbers = 0;
result = 0;
equationsLength = 0;
finalResult = 0;
});
it('should generate correct equation sequences', function() {
generateEquations(2, 3, 3);
expect(equations).to.eql([1, 1, 1, 1, 1, 2, 4, 8, 1, 3, 9, 27, 8]);
});
it('should calculate the correct numbersFound sequence', function() {
createEquations(2, 3);
expect(numbersFound).to.eql([8, 7, -14]);
});
it('should simplify the equations correctly', function() {
generateEquations(2, 3, 3);
simplifyEquations(equations, 3, 3, 3);
expect(equationsResult).to.eql([1, 0, 1, 3, 8, 1, 4, 9, 0, 0, 1]);
});
it('should simplify the full equation set correctly', function() {
generateEquations(2, 3, 3);
toSimplifyEquations(3, 3);
expect(equations).to.eql([1, 3, 8, 1, 4, 9, 0, 0, 1]);
});
it('should calculate the next number in sequence correctly', function() {
createEquations(2, 3);
findNextNumber(2, 3);
expect(numbersFound).to.eql([8, 7, -14, 0]);
});
it('should calculate the correct polynomial result for n = 2', function() {
const result = optimumPolynomialOfN(2);
expect(result).to.equal(15);
});
it('should calculate the correct polynomial result for n = 3', function() {
const result = optimumPolynomialOfN(3);
expect(result).to.equal(25);
});
it('should return the correct final result', function() {
const result = optimumPolynomial();
expect(result).to.equal(1025);
});
});
// Run the tests
mocha.run();
</script>