Skip to content

Commit

Permalink
Update server.js (#156)
Browse files Browse the repository at this point in the history
* Update server.js

Signed-off-by: SkandaBT <9980056379Skanda@gmail.com>

* Update server.js (#158)

---------

Signed-off-by: SkandaBT <9980056379Skanda@gmail.com>
  • Loading branch information
skanda890 authored Nov 18, 2024
1 parent 1c57e45 commit 4e61a28
Showing 1 changed file with 31 additions and 97 deletions.
128 changes: 31 additions & 97 deletions math-calculator/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ const express = require('express');
const math = require('mathjs');
const path = require('path');
const Decimal = require('decimal.js');
const nerdamer = require('nerdamer');
const nerdamerS = require('nerdamer/Algebra'); // Include algebra functions

const app = express();
const port = 4000;
Expand All @@ -28,110 +26,46 @@ app.get('/calculator', (req, res) => {

// Function to handle calculations, including the question, solution, and explanation
function handleCalculation(expression) {
const sqrtRegex = /squareroot(\d+)/;
const squareRegex = /square(\d+)/;
const powerRegex = /(\d+)\^(\d+)/; // Updated regex for power
const factorialRegex = /(\d+)!/;
const permutationRegex = /(\d+)P(\d+)/;
const combinationRegex = /(\d+)C(\d+)/;
const logRegex = /log\((\d+)\)/;
const trigRegex = /(sin|cos|tan)\((\d+)\)/;
const integralPiRegex = /integrate\(1 \/ \(1 - x\^2\), x, -1, 1\)/;
const vietaRegex = /vieta\((\d+)\)/; // Match "vieta(iterations)"
const sqrtRegex = /squareroot(\d+)(\^(-?\d+))?/; // Match "squareroot<number>" and optional "^exponent"
const largePowerRegex = /(\d+)\^(\d+)/; // Match for "base^exponent" form
const assignmentRegex = /\(([^=]+)=([^=]+)\)\^([^=]+)/; // Match for assignment-like expressions (x=10)^999999

try {
let question = `What is the result of: ${expression}?`;
let solution;
let explanation;

// Special case for the integral of 1 / (1 - x^2) from -1 to 1
if (integralPiRegex.test(expression)) {
// Use nerdamer to solve the integral
try {
// Using nerdamer to perform symbolic integration
const result = nerdamer('integrate(1/(1-x^2), x)').evaluate();
solution = result.toString();
explanation = 'This integral is a standard representation for the value of π, and its result is exactly π.';
} catch (err) {
solution = 'Error';
explanation = 'Failed to evaluate the integral.';
}
} else if (vietaRegex.test(expression)) {
// Handle Vieta's formula approximation
const iterations = parseInt(expression.match(vietaRegex)[1], 10);

let product = 1;
let term = Math.sqrt(0.5); // Initial term
for (let i = 1; i <= iterations; i++) {
product *= term;
term = Math.sqrt(0.5 + 0.5 * term); // Generate the next term
}
solution = (2 / product).toFixed(10); // Multiply by 2 and format to 10 decimal places
explanation = `Vieta's formula approximates π as the iterations increase. With ${iterations} iterations, the result is ${solution}.`;
if (assignmentRegex.test(expression)) {
// Handle assignment-like expressions
const match = expression.match(assignmentRegex);
const base = parseInt(match[2], 10); // Extract the base
const exponent = parseInt(match[3], 10); // Extract the exponent
// Evaluate the base raised to the power of the exponent
solution = Math.pow(base, exponent);
explanation = `The result of (${match[1]}=${base})^${exponent} is ${solution}.`;
} else if (sqrtRegex.test(expression)) {
const number = parseFloat(expression.match(sqrtRegex)[1]);
solution = Math.sqrt(number);
explanation = `The square root of ${number} is calculated as √${number}, resulting in ${solution}.`;
} else if (squareRegex.test(expression)) {
const number = parseFloat(expression.match(squareRegex)[1]);
solution = Math.pow(number, 2);
explanation = `The square of ${number} is ${solution}.`;
} else if (powerRegex.test(expression)) {
const match = expression.match(powerRegex);
const base = new Decimal(match[1]);
const exponent = new Decimal(match[2]);
const match = expression.match(sqrtRegex);
const number = parseFloat(match[1]); // Extract the number after "squareroot"
const exponent = match[3] ? parseFloat(match[3]) : 1; // Default to 1 if no exponent is provided

if (exponent.gt(1000)) {
// Handle extremely large exponents
const digits = Math.floor(Math.log10(base.toNumber()) * exponent.toNumber()) + 1;
solution = `1 followed by ${digits - 1} zeros`;
explanation = `${base}^${exponent} is extremely large and has ${digits} digits.`;
} else {
solution = base.pow(exponent).toString();
explanation = `${base}^${exponent} = ${solution}.`;
}
} else if (factorialRegex.test(expression)) {
const number = parseInt(expression.match(factorialRegex)[1], 10);
// Calculate the square root and apply the exponent
const sqrtValue = Math.sqrt(number);
solution = Math.pow(sqrtValue, exponent); // Apply exponent to the square root

explanation = `The square root of ${number} is calculated as √${number}, which is ${sqrtValue}. Then raising this value to the power of ${exponent} gives ${solution}.`;
} else if (largePowerRegex.test(expression)) {
// Handle large power expressions
const match = expression.match(largePowerRegex);
const base = parseInt(match[1], 10);
const exponent = parseInt(match[2], 10);

if (number > 100) {
// Approximate large factorials
const approx = math.log10(math.factorial(number));
const digits = Math.floor(approx) + 1;
solution = `1 followed by ${digits - 1} zeros`;
explanation = `The factorial of ${number} is a very large number with ${digits} digits.`;
// For very large exponents, handle the overflow by estimating the result
if (exponent > 100000) {
solution = `1 followed by ${exponent} zeros`;
explanation = `${base}^${exponent} is extremely large and has ${exponent + 1} digits.`;
} else {
solution = math.factorial(number).toString();
explanation = `${number}! = ${solution}.`;
}
} else if (permutationRegex.test(expression)) {
const match = expression.match(permutationRegex);
const n = parseInt(match[1], 10);
const r = parseInt(match[2], 10);
solution = math.permutations(n, r).toString();
explanation = `nPr = ${n}! / (n - r)! = ${solution}.`;
} else if (combinationRegex.test(expression)) {
const match = expression.match(combinationRegex);
const n = parseInt(match[1], 10);
const r = parseInt(match[2], 10);
solution = math.combinations(n, r).toString();
explanation = `nCr = n! / (r!(n - r)!) = ${solution}.`;
} else if (logRegex.test(expression)) {
const number = parseFloat(expression.match(logRegex)[1]);
solution = math.log10(number).toString();
explanation = `log(${number}) = ${solution}.`;
} else if (trigRegex.test(expression)) {
const match = expression.match(trigRegex);
const func = match[1];
const angle = parseFloat(match[2]);
if (func === "sin") {
solution = math.sin(math.unit(angle, 'deg')).toString();
explanation = `sin(${angle}) = ${solution}.`;
} else if (func === "cos") {
solution = math.cos(math.unit(angle, 'deg')).toString();
explanation = `cos(${angle}) = ${solution}.`;
} else if (func === "tan") {
solution = math.tan(math.unit(angle, 'deg')).toString();
explanation = `tan(${angle}) = ${solution}.`;
solution = Math.pow(base, exponent);
explanation = `${base}^${exponent} = ${solution}.`;
}
} else {
// Default evaluation if no specific case matches
Expand Down Expand Up @@ -159,4 +93,4 @@ app.post('/calculate', (req, res) => {
// Start the server
app.listen(port, () => {
console.log(`Math Calculator API is running at http://localhost:${port}`);
});
});

0 comments on commit 4e61a28

Please sign in to comment.