Data update
This commit is contained in:
parent
4d5544505c
commit
4924dd0264
3073 changed files with 55820 additions and 4408 deletions
35
Task/Continued-fraction/JavaScript/continued-fraction.js
Normal file
35
Task/Continued-fraction/JavaScript/continued-fraction.js
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
// Function to calculate the continued fraction
|
||||
function calc(func, n) {
|
||||
let temp = 0;
|
||||
for (; n > 0; --n) {
|
||||
const [a, b] = func(n);
|
||||
temp = b / (a + temp);
|
||||
}
|
||||
const [a, b] = func(0);
|
||||
return a + temp;
|
||||
}
|
||||
|
||||
// Function to compute coefficients for sqrt(2)
|
||||
function sqrt2(n) {
|
||||
return [n > 0 ? 2 : 1, 1];
|
||||
}
|
||||
|
||||
// Function to compute coefficients for Napier's constant
|
||||
function napier(n) {
|
||||
return [n > 0 ? n : 2, n > 1 ? n - 1 : 1];
|
||||
}
|
||||
|
||||
// Function to compute coefficients for pi
|
||||
function pi(n) {
|
||||
return [n > 0 ? 6 : 3, (2 * n - 1) * (2 * n - 1)];
|
||||
}
|
||||
|
||||
// Main function to execute calculations
|
||||
function main() {
|
||||
console.log(calc(sqrt2, 20));
|
||||
console.log(calc(napier, 15));
|
||||
console.log(calc(pi, 10000));
|
||||
}
|
||||
|
||||
// Run the main function
|
||||
main();
|
||||
Loading…
Add table
Add a link
Reference in a new issue