Efficient Program to Compute Sum of Series 1/1! + 1/2! + 1/3! + 1/4! + .. + 1/n! - GeeksQuiz
Given a positive integer n, write a function to compute sum of the series 1/1! + 1/2! + .. + 1/n!
Reuse previous calculation.
An Efficient Solution can find the sum in O(n) time. The idea is to calculate factorial in same loop as sum.
Read full article from Efficient Program to Compute Sum of Series 1/1! + 1/2! + 1/3! + 1/4! + .. + 1/n! - GeeksQuiz
Given a positive integer n, write a function to compute sum of the series 1/1! + 1/2! + .. + 1/n!
Reuse previous calculation.
An Efficient Solution can find the sum in O(n) time. The idea is to calculate factorial in same loop as sum.
double
sum(
int
n)
{
double
sum = 0;
int
fact = 1;
for
(
int
i = 1; i <= n; i++)
{
fact *= i;
// Update factorial
sum += 1.0/fact;
// Update series sum
}
return
sum;
}