user9181740
user9181740

Reputation:

Power and factorial series sum Explanation

Question:

A class SeriesSum is designed to calculate the sum of the following series:

question

Class name : SeriesSum

Data members/instance variables:

x : to store an integer number

n : to store number of terms

sum : double variable to store the sum of the series

Member functions:

SeriesSum(int xx, int nn) : constructor to assign x=xx and n=nn

double findfact(int m) to return the factorial of m using recursive technique.

double findpower(int x, int y) : to return x raised to the power of y using recursive technique.

void calculate( ) : to calculate the sum of the series by invoking the recursive functions respectively

void display( ) : to display the sum of the series

(a) Specify the class SeriesSum, giving details of the constructor(int, int), double findfact(int), double findpower(int, int), void calculate( ) and void display( ).

Define the main( ) function to create an object and call the functions accordingly to enable the task.

Code:

class SeriesSum
    {
    int x,n;
    double sum;
    SeriesSum(int xx,int nn)
    { x=xx;
    n=nn;
    sum=0.0;
    }
    double findfact(int a)
    { return (a<2)? 1:a*findfact(a-1);
    }
    double findpower(int a, int b)
    { return (b==0)? 1:a*findpower(a,b-1);
    }
    void calculate()
    { for(int i=2;i<=n;i+=2)
    sum += findpower(x,i)/findfact(i-1);
    }
    void display()
    { System.out.println("sum="+ sum);
    }
    static void main()
    { SeriesSum obj = new SeriesSum(3,8);
    obj.calculate();
    obj.display();
    }
}

MyProblem:

I am having problems in understanding that when i= any odd number (Taking an example such as 3 here)then it value that passes through findfact is (i-1)=2 then how am I getting the odd factorials such as 3!

Any help or guidance would be highly appreciated.

Optional:

If you can somehow explain the recursion taking place in the findpower and findfactorial,it would be of great help.

Upvotes: 0

Views: 2959

Answers (4)

Morchul
Morchul

Reputation: 2037

I'm not sure I understand your question correctly, but I try to help you the best I can.

I am having problems in understanding that when i= any odd number

In this code i never will be any odd number

for(int i=2;i<=n;i+=2)

i will be: 2 , 4 , 6 , 8 and so on because i+=2

The Recursion

The findfact() function in a more readable version:

  double findfact(int a){
    if(a < 2 ){
      return 1;
    } else {
      return a * findfact(a - 1);
    }
  }

you can imagine it as a staircase, every call of findfact is a step: enter image description here

We test: if a < 2 then return 1 else we call findfact() again with a-1 and multiply a with the result of findfact()

The same function without recursion:

  double findfact(int a){
    int sum = 1;
    for(int i = a; i > 0; i--){
      sum *= i;
    }
    return sum;
  }


Same by the findpower function: if b == 0 then return 1 else call findpower() with a, b-1 and multiply the return value of findpower() with a

So the last called findpower() will return 1 (b = 0)
The second last findpower() will return a * 1 (b = 1)
The third last findpower() will return a * a * 1 (b = 2)

so you can see findpower(a, 2) = a * a * 1 = a^2

Hope I could help you

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

You can simplify the summation and get rid of power and factorial. Please notice:

  1. The very first term is just x * x
  2. If you know term item == x ** (2 * n) / (2 * n - 1)! the next one will be item * x * x / (2 * n) / (2 * n + 1).

Implementation:

private static double sum(double x, int count) {
  double item = x * x; // First item
  double result = item;

  for (int i = 1; i <= count; ++i) {
    // Next item from previous
    item = item * x * x / (2 * i) / (2 * i +1);       

    result += item;   
  }

  return result;
}

In the real world, you can notice that

sinh(x) = x/1! + x**3/3! + x**5/5! + ... + x**(2*n - 1) / (2*n - 1)! + ...

and your serie is nothing but

x * sinh(x) = x**2/1! + x**4 / 3! + ... + x**(2*n) / (2*n - 1)! + ...

So you can implement

private static double sum(double x) {
  return x * (Math.exp(x) - Math.exp(-x)) / 2.0;
}

Upvotes: 0

Ajit Kamble
Ajit Kamble

Reputation: 153

Try to run below code, it will clear all your doubts (i have modified some access specifier and created main method)

public class SeriesSum
    {
    int x,n;
    double sum;
    SeriesSum(int xx,int nn)
    { x=xx;
    n=nn;
    sum=0.0;
    }
    double findfact(int a)
    { return (a<2)? 1:a*findfact(a-1);
    }
    double findpower(int a, int b)
    { return (b==0)? 1:a*findpower(a,b-1);
    }
    void calculate()
    { 
    System.out.println("x ="+x);    
    System.out.println("n ="+n);    
    for(int i=2;i<=n;i+=2){

        System.out.println(x+"^"+i+"/"+(i-1)+"!" +" = " +(findpower(x,i)+"/"+findfact(i-1)) );
        //System.out.println(findpower(x,i)+"/"+findfact(i-1));

        sum += findpower(x,i)/findfact(i-1);
    }
    }
    void display()
    { System.out.println("sum="+ sum);
    }
    public static void main(String arg[])
    { SeriesSum obj = new SeriesSum(3,8);
    obj.calculate();
    obj.display();
    }
}


// -----  output ----
    x =3
    n =8
    3^2/1! = 9.0/1.0
    3^4/3! = 81.0/6.0
    3^6/5! = 729.0/120.0
    3^8/7! = 6561.0/5040.0
    sum=29.876785714285713

Upvotes: 0

ttzn
ttzn

Reputation: 2613

Take a closer look a the loop. i starts at 2 and is incremented by 2 every iteration, so it is never odd. It corresponds to the successive powers of x, each of which is divided by the factorial of i -1 (which IS odd).

As for the recursion in findfact, you just need to unwrap the first few calls by hand to see why it works :

findfact(a) = a * findfact(a -1) 
    = a * (a - 1) * findfact(a -2)
    = a * (a - 1) * (a - 2) * findfact(a - 3)
    ...
    = a * (a - 1) * (a - 2) * ... * 2 * findfact(1)
    = a * (a - 1) * (a - 2) * ... * 2 * 1
    = a!*

The same reasoning works with findpower.

As a side note, while it may be helpful for teaching purposes, recursion is a terrible idea for computing factorials or powers.

Upvotes: 1

Related Questions