diff --git a/bisection_method.js b/bisection_method.js index ec1f289..04a4a03 100644 --- a/bisection_method.js +++ b/bisection_method.js @@ -12,19 +12,23 @@ console.log('%cBisection method script', 'font-weight:bold;font-size:24px'); const f = new Polynomial(); // f(x) = x^6 - 3x - 1 -f.addTerm("x", 6); -f.addSign("-"); -f.addTerm("3x"); -f.addSign("-"); -f.addTerm("1"); +f.addTerm('x', 6); +f.addSign('-'); +f.addTerm('3x'); +f.addSign('-'); +f.addTerm('1'); // Run the algorithm and print the result! -console.log('Running bisection method for %cf(x) = x^6 - 3x - 1 in [-1, 0] and default iterations (50)', - 'font-weight:bold'); +console.log( + 'Running bisection method for %cf(x) = x^6 - 3x - 1 in [-1, 0] and default iterations (50)', + 'font-weight:bold' +); console.log(bisect(f, [-1, 0])); console.log(''); -console.log('Running bisection method for %cf(x) = x^6 - 3x - 1 in [1, 2] and 8 iterations', - 'font-weight:bold'); +console.log( + 'Running bisection method for %cf(x) = x^6 - 3x - 1 in [1, 2] and 8 iterations', + 'font-weight:bold' +); console.log(bisect(f, [1, 2], 8)); console.log(''); @@ -32,116 +36,113 @@ console.log(''); // Algorithm of bisection function bisect(polynomial, interval, i = 50) { - var a = parseInt(interval[0]); - var b = parseInt(interval[1]); - var c = 0; - var error = 0; - - do { - c = (a + b) / 2; - var fa = polynomial.eval(a); - var fb = polynomial.eval(b); - var fc = polynomial.eval(c); - error = (Math.abs(a - b) / 2); - - if(fc == 0) { - break; - } - if(hasOpositeSigns(fa, fc)) { - b = c; - } - else if(hasOpositeSigns(fc, fb)) { - a = c; - } - else { - return `There aren't roots in the interval or try to give an interval + let a = parseInt(interval[0]); + let b = parseInt(interval[1]); + let c = 0; + let error = 0; + + do { + c = (a + b) / 2; + let fa = polynomial.eval(a); + let fb = polynomial.eval(b); + let fc = polynomial.eval(c); + error = (Math.abs(a - b) / 2); + + if (fc === 0) { + break; + } + if (hasOppositeSigns(fa, fc)) { + b = c; + } + else if (hasOppositeSigns(fc, fb)) { + a = c; + } + else { + return `There aren't roots in the interval or try to give an interval in which only the root that you are looking for of the polynomial is enclosed in it and with a change of signs!`; - } - i--; - } while(i != 0); - return `Root found at c = ${c}, F(c) = ${polynomial.eval(c)}, |a - b| / 2 = ${error}`; -} - -function hasOpositeSigns(a, b) { - return ((a < 0 && b >= 0) || (a >= 0 && b < 0)); + } + i--; + } + while (i !== 0); + return `Root found at c = ${ c }, F(c) = ${ polynomial.eval(c) }, |a - b| / 2 = ${ error }`; } -function isRoot(polynomial, x) { - return polynomial.eval(x) == 0; +function hasOppositeSigns(a, b) { + return ((a < 0 && b >= 0) || (a >= 0 && b < 0)); } // Polynomial class function Polynomial() { - - this.terms = Array(); - - this.addSign = function(sign) { - var last = this.terms[(this.terms.length - 1)]; - - if(isSign(last)) { - var boolSign = last === "+"; - var boolInSign = sign === "+"; - var newBoolSign = boolSign & boolInSign; - var newSign = (newBoolSign) ? "+" : "-"; - - this.terms.pop(); - this.terms.push(newSign); + + this.terms = Array(); + + this.addSign = function(sign) { + let last = this.terms[(this.terms.length - 1)]; + + if (isSign(last)) { + let boolSign = last === '+'; + let boolInSign = sign === '+'; + let newBoolSign = boolSign & boolInSign; + let newSign = (newBoolSign) ? '+' : '-'; + + this.terms.pop(); + this.terms.push(newSign); + } + else { + this.terms.push(sign); + } + }; + + this.addTerm = function(value, exponent = 1) { + if (value[value.length - 1] === 'x') { + let factor = value.substr(0, value.length - 1); + + if (factor.length === 0) { + factor = 1; + } + this.terms.push([value, parseFloat(factor), exponent]); + } + else { + this.terms.push(value); + } + }; + + this.eval = function(value) { + let operation = '+'; + let result = 0; + + this.terms.forEach(item => { + if (isSign(item)) { + operation = item; + } + else if (item.length === 3) { + let factor = item[1]; + let exponent = item[2]; + + if (operation[0] === '+') { + result += factor * Math.pow(value, exponent); } else { - this.terms.push(sign); + result -= factor * Math.pow(value, exponent); } - } - - this.addTerm = function(value, exponent = 1) { - if(value[value.length - 1] === "x") { - var factor = value.substr(0, value.length - 1); - - if(factor.length === 0) { - factor = 1; - } - this.terms.push([value, parseFloat(factor), exponent]); + operation = '+'; + } + else { + if (operation[0] === '+') { + result += parseFloat(item); } else { - this.terms.push(value); + result -= parseFloat(item); } - } - - this.eval = function(value) { - var operation = "+"; - var result = 0; - - this.terms.forEach(item => { - if(isSign(item)) { - operation = item; - } - else if(item.length === 3) { - var factor = item[1]; - var exponent = item[2]; - - if(operation[0] === "+") { - result += factor * Math.pow(value, exponent); - } - else { - result -= factor * Math.pow(value, exponent); - } - operation = "+"; - } - else { - if(operation[0] === "+") { - result += parseFloat(item); - } - else { - result -= parseFloat(item); - } - operation = "+"; - } - }); - return result; - } - - function isSign(value) { - return value[0] === "+" || value[0] === "-"; - } - + operation = '+'; + } + }); + return result; + }; + + function isSign(value) { + return value[0] === '+' || value[0] === '-'; + } + } diff --git a/eratosthenes_sieve.js b/eratosthenes_sieve.js index 5fe5359..3a20b72 100644 --- a/eratosthenes_sieve.js +++ b/eratosthenes_sieve.js @@ -12,45 +12,45 @@ console.log('Getting the first prime numbers up to 101'); console.log(getPrimes()); console.log(''); -function fillCriba(criba, length) { - var value = (criba.length === 0) ? 2 : criba[criba.length - 1] + 1; - - for(let i = 0; i < length; i++) { - criba.push(value); - value++; - } +function fillSieve(sieve, length) { + let value = (sieve.length === 0) ? 2 : sieve[sieve.length - 1] + 1; + + for (let i = 0; i < length; i++) { + sieve.push(value); + value++; + } } -function remove(criba, position) { - criba[position] = null; +function remove(sieve, position) { + sieve[position] = null; } -function parseCriba(criba, startPos) { - var current = criba[startPos]; - var currentPos = startPos; - - while(criba.length > currentPos + current) { - currentPos += current; - - remove(criba, currentPos); - } +function parseSieve(sieve, startPos) { + let current = sieve[startPos]; + let currentPos = startPos; + + while (sieve.length > currentPos + current) { + currentPos += current; + + remove(sieve, currentPos); + } } -function runCriba(criba) { - var primes = Array(); - - for(let i = 0; i < criba.length; i++) { - if(criba[i] != null) { - primes.push(criba[i]); - parseCriba(criba, i); - } +function runSieve(sieve) { + let primes = Array(); + + for (let i = 0; i < sieve.length; i++) { + if (sieve[i] != null) { + primes.push(sieve[i]); + parseSieve(sieve, i); } - return primes; + } + return primes; } function getPrimes(length = 100) { - const criba = Array(); - - fillCriba(criba, length); - return runCriba(criba); + const sieve = Array(); + + fillSieve(sieve, length); + return runSieve(sieve); } diff --git a/index.html b/index.html index 12a3a09..9782a69 100644 --- a/index.html +++ b/index.html @@ -1,12 +1,23 @@ - - - - Miscellaneous algorithms in JS - - - - Check out the results in the console! - - - + + + + + + Miscellaneous algorithms in JS + + + + + Check out the results in the console! + + +