File size: 12,851 Bytes
4fea3ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
class AdvancedCalculator {
    constructor() {
        this.history = [];
        this.memory = 0;
        this.precision = 10;
        this.constants = {
            PI: 3.141592653589793,
            E: 2.718281828459045,
            GOLDEN_RATIO: 1.618033988749895
        };
    }

    add(a, b) {
        const result = parseFloat((a + b).toFixed(this.precision));
        this.history.push(`${a} + ${b} = ${result}`);
        return result;
    }

    subtract(a, b) {
        const result = parseFloat((a - b).toFixed(this.precision));
        this.history.push(`${a} - ${b} = ${result}`);
        return result;
    }

    multiply(a, b) {
        const result = parseFloat((a * b).toFixed(this.precision));
        this.history.push(`${a} * ${b} = ${result}`);
        return result;
    }

    divide(a, b) {
        if (b === 0) {
            throw new Error('Division by zero is not allowed');
        }
        const result = parseFloat((a / b).toFixed(this.precision));
        this.history.push(`${a} / ${b} = ${result}`);
        return result;
    }

    power(base, exponent) {
        const result = parseFloat(Math.pow(base, exponent).toFixed(this.precision));
        this.history.push(`${base} ^ ${exponent} = ${result}`);
        return result;
    }

    sqrt(number) {
        if (number < 0) {
            throw new Error('Cannot calculate square root of negative number');
        }
        const result = parseFloat(Math.sqrt(number).toFixed(this.precision));
        this.history.push(`${number} = ${result}`);
        return result;
    }

    factorial(n) {
        if (n < 0 || !Number.isInteger(n)) {
            throw new Error('Factorial is only defined for non-negative integers');
        }
        let result = 1;
        for (let i = 2; i <= n; i++) {
            result *= i;
        }
        this.history.push(`${n}! = ${result}`);
        return result;
    }

    fibonacci(n) {
        if (n < 0 || !Number.isInteger(n)) {
            throw new Error('Fibonacci is only defined for non-negative integers');
        }
        if (n <= 1) return n;

        let a = 0, b = 1;
        for (let i = 2; i <= n; i++) {
            [a, b] = [b, a + b];
        }
        this.history.push(`fib(${n}) = ${b}`);
        return b;
    }

    sine(angle, degrees = true) {
        const radians = degrees ? angle * (Math.PI / 180) : angle;
        const result = parseFloat(Math.sin(radians).toFixed(this.precision));
        this.history.push(`sin(${angle}${degrees ? '°' : ' rad'}) = ${result}`);
        return result;
    }

    cosine(angle, degrees = true) {
        const radians = degrees ? angle * (Math.PI / 180) : angle;
        const result = parseFloat(Math.cos(radians).toFixed(this.precision));
        this.history.push(`cos(${angle}${degrees ? '°' : ' rad'}) = ${result}`);
        return result;
    }

    tangent(angle, degrees = true) {
        const radians = degrees ? angle * (Math.PI / 180) : angle;
        const result = parseFloat(Math.tan(radians).toFixed(this.precision));
        this.history.push(`tan(${angle}${degrees ? '°' : ' rad'}) = ${result}`);
        return result;
    }

    logarithm(number, base = Math.E) {
        if (number <= 0) {
            throw new Error('Logarithm is only defined for positive numbers');
        }
        const result = parseFloat((Math.log(number) / Math.log(base)).toFixed(this.precision));
        this.history.push(`log${base === Math.E ? '' : '_' + base}(${number}) = ${result}`);
        return result;
    }

    gcd(a, b) {
        a = Math.abs(a);
        b = Math.abs(b);
        while (b !== 0) {
            [a, b] = [b, a % b];
        }
        this.history.push(`gcd(${arguments[0]}, ${arguments[1]}) = ${a}`);
        return a;
    }

    lcm(a, b) {
        const result = Math.abs(a * b) / this.gcd(a, b);
        this.history.push(`lcm(${a}, ${b}) = ${result}`);
        return result;
    }

    isPrime(n) {
        if (n < 2 || !Number.isInteger(n)) return false;
        if (n === 2) return true;
        if (n % 2 === 0) return false;

        for (let i = 3; i <= Math.sqrt(n); i += 2) {
            if (n % i === 0) return false;
        }
        this.history.push(`isPrime(${n}) = true`);
        return true;
    }

    generatePrimes(limit) {
        const primes = [];
        const sieve = new Array(limit + 1).fill(true);
        sieve[0] = sieve[1] = false;

        for (let i = 2; i <= Math.sqrt(limit); i++) {
            if (sieve[i]) {
                for (let j = i * i; j <= limit; j += i) {
                    sieve[j] = false;
                }
            }
        }

        for (let i = 2; i <= limit; i++) {
            if (sieve[i]) primes.push(i);
        }

        this.history.push(`Generated ${primes.length} primes up to ${limit}`);
        return primes;
    }

    quadraticSolver(a, b, c) {
        if (a === 0) {
            throw new Error('Coefficient a cannot be zero in quadratic equation');
        }

        const discriminant = b * b - 4 * a * c;

        if (discriminant < 0) {
            const realPart = parseFloat((-b / (2 * a)).toFixed(this.precision));
            const imaginaryPart = parseFloat((Math.sqrt(-discriminant) / (2 * a)).toFixed(this.precision));
            const result = {
                x1: `${realPart} + ${imaginaryPart}i`,
                x2: `${realPart} - ${imaginaryPart}i`
            };
            this.history.push(`Quadratic ${a}x² + ${b}x + ${c} = 0 has complex roots`);
            return result;
        } else if (discriminant === 0) {
            const x = parseFloat((-b / (2 * a)).toFixed(this.precision));
            this.history.push(`Quadratic ${a}x² + ${b}x + ${c} = 0 has double root x = ${x}`);
            return { x1: x, x2: x };
        } else {
            const x1 = parseFloat(((-b + Math.sqrt(discriminant)) / (2 * a)).toFixed(this.precision));
            const x2 = parseFloat(((-b - Math.sqrt(discriminant)) / (2 * a)).toFixed(this.precision));
            this.history.push(`Quadratic ${a}x² + ${b}x + ${c} = 0 has roots x1 = ${x1}, x2 = ${x2}`);
            return { x1, x2 };
        }
    }

    memoryStore(value) {
        this.memory = value;
        this.history.push(`Memory stored: ${value}`);
    }

    memoryRecall() {
        this.history.push(`Memory recalled: ${this.memory}`);
        return this.memory;
    }

    memoryClear() {
        this.memory = 0;
        this.history.push('Memory cleared');
    }

    memoryAdd(value) {
        this.memory += value;
        this.history.push(`Memory added: ${value}, new total: ${this.memory}`);
    }

    statistics(numbers) {
        if (!Array.isArray(numbers) || numbers.length === 0) {
            throw new Error('Input must be a non-empty array of numbers');
        }

        const n = numbers.length;
        const sum = numbers.reduce((acc, num) => acc + num, 0);
        const mean = sum / n;

        const sortedNumbers = [...numbers].sort((a, b) => a - b);
        const median = n % 2 === 0
            ? (sortedNumbers[n / 2 - 1] + sortedNumbers[n / 2]) / 2
            : sortedNumbers[Math.floor(n / 2)];

        const variance = numbers.reduce((acc, num) => acc + Math.pow(num - mean, 2), 0) / n;
        const standardDeviation = Math.sqrt(variance);

        const mode = this.calculateMode(numbers);

        const stats = {
            count: n,
            sum: parseFloat(sum.toFixed(this.precision)),
            mean: parseFloat(mean.toFixed(this.precision)),
            median: parseFloat(median.toFixed(this.precision)),
            mode: mode,
            variance: parseFloat(variance.toFixed(this.precision)),
            standardDeviation: parseFloat(standardDeviation.toFixed(this.precision)),
            min: Math.min(...numbers),
            max: Math.max(...numbers)
        };

        this.history.push(`Statistics calculated for ${n} numbers`);
        return stats;
    }

    calculateMode(numbers) {
        const frequency = {};
        let maxFreq = 0;

        numbers.forEach(num => {
            frequency[num] = (frequency[num] || 0) + 1;
            maxFreq = Math.max(maxFreq, frequency[num]);
        });

        const modes = Object.keys(frequency)
            .filter(key => frequency[key] === maxFreq)
            .map(Number);

        return modes.length === numbers.length ? null : modes;
    }

    matrixMultiply(matrixA, matrixB) {
        if (!Array.isArray(matrixA) || !Array.isArray(matrixB)) {
            throw new Error('Both inputs must be arrays (matrices)');
        }

        const rowsA = matrixA.length;
        const colsA = matrixA[0].length;
        const rowsB = matrixB.length;
        const colsB = matrixB[0].length;

        if (colsA !== rowsB) {
            throw new Error('Matrix dimensions are incompatible for multiplication');
        }

        const result = Array(rowsA).fill().map(() => Array(colsB).fill(0));

        for (let i = 0; i < rowsA; i++) {
            for (let j = 0; j < colsB; j++) {
                for (let k = 0; k < colsA; k++) {
                    result[i][j] += matrixA[i][k] * matrixB[k][j];
                }
                result[i][j] = parseFloat(result[i][j].toFixed(this.precision));
            }
        }

        this.history.push(`Matrix multiplication: ${rowsA}x${colsA} * ${rowsB}x${colsB}`);
        return result;
    }

    convertBase(number, fromBase, toBase) {
        if (fromBase < 2 || fromBase > 36 || toBase < 2 || toBase > 36) {
            throw new Error('Base must be between 2 and 36');
        }

        const decimal = parseInt(number.toString(), fromBase);
        const result = decimal.toString(toBase).toUpperCase();

        this.history.push(`Base conversion: ${number} (base ${fromBase}) = ${result} (base ${toBase})`);
        return result;
    }

    clearHistory() {
        this.history = [];
    }

    getHistory() {
        return [...this.history];
    }

    setPrecision(precision) {
        if (precision < 0 || precision > 20) {
            throw new Error('Precision must be between 0 and 20');
        }
        this.precision = precision;
        this.history.push(`Precision set to ${precision} decimal places`);
    }
}

function runCalculatorDemo() {
    const calc = new AdvancedCalculator();

    console.log('Advanced Calculator Demo');
    console.log('========================');

    try {
        console.log('Basic operations:');
        console.log('5 + 3 =', calc.add(5, 3));
        console.log('10 - 4 =', calc.subtract(10, 4));
        console.log('6 * 7 =', calc.multiply(6, 7));
        console.log('15 / 3 =', calc.divide(15, 3));

        console.log('\nAdvanced operations:');
        console.log('2^8 =', calc.power(2, 8));
        console.log('16 =', calc.sqrt(16));
        console.log('5! =', calc.factorial(5));
        console.log('fib(10) =', calc.fibonacci(10));

        console.log('\nTrigonometric functions:');
        console.log('sin(30°) =', calc.sine(30));
        console.log('cos(60°) =', calc.cosine(60));
        console.log('tan(45°) =', calc.tangent(45));

        console.log('\nNumber theory:');
        console.log('gcd(48, 18) =', calc.gcd(48, 18));
        console.log('lcm(12, 15) =', calc.lcm(12, 15));
        console.log('isPrime(17) =', calc.isPrime(17));

        console.log('\nQuadratic equation solver:');
        console.log('x² - 5x + 6 = 0:', calc.quadraticSolver(1, -5, 6));

        console.log('\nStatistics:');
        const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        console.log('Data:', data);
        console.log('Stats:', calc.statistics(data));

        console.log('\nMatrix multiplication:');
        const matrixA = [[1, 2], [3, 4]];
        const matrixB = [[5, 6], [7, 8]];
        console.log('Matrix A:', matrixA);
        console.log('Matrix B:', matrixB);
        console.log('A * B =', calc.matrixMultiply(matrixA, matrixB));

        console.log('\nBase conversion:');
        console.log('255 (decimal) to binary:', calc.convertBase(255, 10, 2));
        console.log('FF (hex) to decimal:', calc.convertBase('FF', 16, 10));

        console.log('\nCalculation history:');
        calc.getHistory().forEach((entry, index) => {
            console.log(`${index + 1}. ${entry}`);
        });

    } catch (error) {
        console.error('Error:', error.message);
    }
}

if (typeof module !== 'undefined' && module.exports) {
    module.exports = AdvancedCalculator;
} else {
    runCalculatorDemo();
}