Horje
Find the Sum of the Series: 1/1! + 1/2! + 1/3! + ... + 1/n! using JavaScript ?

We are going to discuss how we can find the sum of series: 1/1! + 1/2! + 1/3! + …… till n terms.

Below are the approaches to find the sum of series:

Using for loop

We will first define a function to calculate factorial of a given number. It checks if the number is 0 or 1 it returns 1 else, it recursively calculates factorial. Now we define another function and initialize a variable sum to 0. Use a for loop to iterate from 1 to n. For each iteration, calculate the reciprocal of the factorial of the current number i and add it to the sum. Return sum.

Example: To demonstrate calculation of sum of given series using for loop

JavaScript
// Using for loop

function factorial(num) {
    if (num === 0 || num === 1) {
        return 1;
    } else {
        return num * factorial(num - 1);
    }
}

function sumSeries(n) {
    let sum = 0;
    for (let i = 1; i <= n; i++) {
        sum += 1 / factorial(i);
    }
    return sum;
}

console.log(sumSeries(3)); 

Output
1.6666666666666667

Time Complexity: O(n)

Space Complexity: O(n)

Using Recursion

We will first define a function to calculate factorial of a given number. It checks if the number is 0 or 1 it returns 1 else, it recursively calculates factorial. We will create another recursive function. The base case of this recursive function is : If n is 1, return 1. Else, calculate the reciprocal of the factorial of n and add it to the result of recursively calling the function with n – 1.

Example: To demonstrate calculation of sum of given series using recursion.

JavaScript
// Using recursion

function factorial(num) {
    if (num === 0 || num === 1) {
        return 1;
    } else {
        return num * factorial(num - 1);
    }
}

function sumSeries(n) {
    if (n === 1) {
        return 1;
    }
    return 1 / factorial(n) + sumSeries(n - 1);
}


console.log(sumSeries(3)); 

Output
1.6666666666666667

Time Complexity: O(n)

Space Complexity: O(n)

Using Memoization for Factorial Calculation

In this approach, we use memoization to store previously calculated factorials to optimize the calculation of the sum of the series. This reduces the number of redundant calculations and improves the efficiency of the program.

Example: This JavaScript code demonstrates how to calculate the sum of the series using memoization for factorial calculation.

JavaScript
// Memoization object to store previously calculated factorials
const factorialMemo = {};

// Function to calculate factorial using memoization
function factorial(n) {
  if (n === 0 || n === 1) return 1;
  if (factorialMemo[n]) return factorialMemo[n];
  factorialMemo[n] = n * factorial(n - 1);
  return factorialMemo[n];
}

// Function to calculate sum of the series using memoized factorial
function sumSeries(n) {
  let sum = 0;
  for (let i = 1; i <= n; i++) {
    sum += 1 / factorial(i);
  }
  return sum;
}

// Example usage
const n = 5;
const result = sumSeries(n);
console.log(`Sum of the series till ${n} terms is ${result}`);
// Output: Sum of the series till 5 terms is 1.7166666666666668

Output
Sum of the series till 5 terms is 1.7166666666666668






Reffered: https://www.geeksforgeeks.org


JavaScript

Related
Check if Two Numbers are Coprime using JavaScript Check if Two Numbers are Coprime using JavaScript
Find the Sum of the Series: 1 + 1/3 + 1/5 + ... + 1/(2n-1) using JavaScript ? Find the Sum of the Series: 1 + 1/3 + 1/5 + ... + 1/(2n-1) using JavaScript ?
JSON Interview Questions JSON Interview Questions
How to Remove HTML Tags from a String in JavaScript? How to Remove HTML Tags from a String in JavaScript?
JavaScript program to find volume of cylinder JavaScript program to find volume of cylinder

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12