Skip to content

Commit

Permalink
Code formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasbriones committed Apr 4, 2020
1 parent 0c4ef9b commit 0a479be
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 149 deletions.
215 changes: 108 additions & 107 deletions bisection_method.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,136 +12,137 @@ 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('');
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] === '-';
}

}
62 changes: 31 additions & 31 deletions eratosthenes_sieve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
33 changes: 22 additions & 11 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title>Miscellaneous algorithms in JS</title>
<meta charset="UTF-8">
</head>
<body>
<strong>Check out the results in the console!</strong>
<script src="bisection_method.js"></script>
<script src="eratosthenes_sieve.js"></script>
</body>
<!--
-- Copyright (c) 2019 Tobias Briones.
--
-- This source code is licensed under the MIT license found in the
-- LICENSE file in the root directory of this source tree.
-->

<!doctype html>
<html lang="en">
<head>
<title>Miscellaneous algorithms in JS</title>
<link rel="icon"
type="image/png"
href="https://raw.githubusercontent.com/TobiasBriones/images/master/example-projects/example-projects/favicon.png"
sizes="96x96">
<meta charset="UTF-8">
</head>
<body>
<strong>Check out the results in the console!</strong>
<script src="bisection_method.js"></script>
<script src="eratosthenes_sieve.js"></script>
</body>
</html>

0 comments on commit 0a479be

Please sign in to comment.