Question

In: Computer Science

3. Consider the following recursive algorithm for computing the sum of the following series: S(n) =...

3. Consider the following recursive algorithm for computing the sum of the

following series: S(n) = 1/1! + 2/2! + . . . + n/n!.

ALGORITHM S (n)
//Input: A positive integer n
// Procedure: fact(n) returns the factorial of the number passed

as parameter
//Output: The sum of the series: S(n) = 1/1! + 2/2! + . . . + n/n! if n = 1 return 1
else return S(n − 1) + n/fact(n)

  1. Set up and solve a recurrence relation for the number of times the algo- rithm’s basic operation is executed.

  2. How does this algorithm compare with the straightforward nonrecursive algorithm for computing this sum?

Solutions

Expert Solution

Recurrence relation is:

Now for n=5

S(5)= S(4)+ 5/fact(5)

S(5)= S(4)+ 5/factorial(5);
= S(3)+ 4/factorial(4)+ 5/factorial(5);
= S(2)+ 3/factorial(3)+ 4/factorial(4)+ 5/factorial(5);
= S(1)+ 2/factorial(2)+ 3/factorial(3)+ 4/factorial(4)+ 5/factorial(5);
= 1+ 1 + 3/6 + 4/24+ 5/120 ;
= 2.7083

To calculate and compare running time, see the code

#include <bits/stdc++.h>
using namespace std;
#define ll double

ll factorial(ll n){
    ll fact=1;
    while(n>0){
        fact=fact*n;
        n--;
    }
    return fact;
}

ll recursiveSum(ll n){
    if(n==1)
    return 1;
    
    return recursiveSum(n-1)+ n/factorial(n);
}

ll iterativeSum(ll n){
    ll sum=0;
    
    for(ll i=1;i<=n;i++){
        sum+= i/factorial(i);
    }
    return sum;
}

int main()
{


ll n=30;

clock_t start,end;
start=clock();
cout<<"Sum is: "<<fixed<<setprecision(4)<<recursiveSum(n)<<" ";
end=clock();
 double time_taken = double(end - start) / double(CLOCKS_PER_SEC); 
    cout << "Time taken by recursive program is : " << fixed  
         << time_taken << setprecision(5); 
    cout << " sec " << endl; 

start=clock();    
cout<<"Sum is: "<<fixed<<setprecision(4)<<iterativeSum(n)<<" ";
end=clock();
  time_taken = double(end - start) / double(CLOCKS_PER_SEC); 
    cout << "Time taken by iterative program is : " << fixed  
         << time_taken << setprecision(5); 
    cout << " sec " << endl; 

        return 0;
}

For n=30

Recursive appraoch= 0.0001s

Iterative Approach= 0.0000s


Related Solutions

Consider the following recursive algorithm for computing the sum of the first n squares: S(n) =...
Consider the following recursive algorithm for computing the sum of the first n squares: S(n) = 12 +22 +32 +...+n2 . Algorithm S(n) //Input: A positive integer n //Output: The sum of the first n squares if n = 1 return 1 else return S(n − 1) + n* n a. Set up and solve a recurrence relation for the number of times the algorithm’s basic operation is executed. b. How does this algorithm compare with the straightforward non-recursive algorithm...
a. Design a recursive algorithm for computing 3n for any nonnegative integer n that is based...
a. Design a recursive algorithm for computing 3n for any nonnegative integer n that is based on the formula 3n = 3n−1 + 3n−1 + 3n−1 b. Set up a recurrence relation for the number of additions made by the algorithm and solve it. c. Draw a tree of recursive calls for this algorithm and count the number of calls made by the algorithm. d. Is it a good algorithm for solving this problem?
1) Write an algorithm to calculate the sum of the following series: Sum =x-x3/3! + x5/5!...
1) Write an algorithm to calculate the sum of the following series: Sum =x-x3/3! + x5/5! – x7/7! +……. Stop when the term<0.0001. 2) An internet service provider charges its subscribers per month as follows: Data usage (n), in gbs charges (NIS) 0.0<n<=1.0 250 1.0<n<=2.0 500 2.0<n<=5.0 1000 5.0<n<=10.0 1500 n>10 2000 Write a C program to read the usage(n) from a file and print the charges to be paid by the subscribers. Your program must include the function calculate...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i ← 1 to n do S ← S + i * i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try...
Determine if the following series converge or diverge. If it converges, find the sum. a. ∑n=(3^n+1)/(2n)...
Determine if the following series converge or diverge. If it converges, find the sum. a. ∑n=(3^n+1)/(2n) (upper limit of sigma∞, lower limit is n=0) b.∑n=(cosnπ)/(2) (upper limit of sigma∞ , lower limit is n= 1 c.∑n=(40n)/(2n−1)^2(2n+1)^2 (upper limit of sigma ∞ lower limit is n= 1 d.)∑n = 2/(10)^n (upper limit of sigma ∞ , lower limit of sigma n= 10)
Use the formula for the sum of the first n terms of a geometric series to find S9 for the series 12, 6, 3, 3/2 , …
Use the formula for the sum of the first n terms of a geometric series to find S9 for the series 12, 6, 3, 3/2 , …  
Determine the sum of the series ∑n=1∞ ( 6n)/(n+2) if possible. (If the series diverges, enter...
Determine the sum of the series ∑n=1∞ ( 6n)/(n+2) if possible. (If the series diverges, enter 'infinity', '-infinity' or 'dne' as appropriate.)
Find the sum (n=5, to infinity) of the series (n2 -n)/2n
Find the sum (n=5, to infinity) of the series (n2 -n)/2n
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT