# Recursive series

### Vinay Singh

Computer Teacher at Saint John's Academy
I am a GNU/Linux enthusiast and a computer science teacher by profession based at Allahabad, India. My hobbies include reading (mostly computer related stuff) and photography.

## ISC Computer Science 2014

### Question 9

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

Some of the members of the class are given below:

`Class name: SeriesSum`

Data members/instance variables:

```x    : to store an integer number

n    : to store number of terms

sum  : double variable to store sum of 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```

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.

The Java implementation of SeriesSum for calculating the recursive series is as follows:

```class SeriesSum{
private int x, n;
private float sum;
public SeriesSum(int xx, int nn ){
x = xx;
n = nn;
}
public double findfact( int m ){
if( m==0 || m==1 ) return 1;
else return m * findfact( m - 1 );
}
public double findpower( int x, int y ){
if( y==0 ) return 1;
else return x * findpower( x, y-1 );
}
public void calculate( ){
sum=0;
for( int i = 1; i<= n; i++ ){
sum += findpower( x, i*2 ) / findfact( 2 * i - 1 );
}
}
public void display(){
System.out.println( sum );
}
public static void main( String args[] ){
SeriesSum s = new SeriesSum( 1, 2 );
s.calculate( );
s.display();
}
}```

From the technical standpoint, there is no need to return a double value from the functions, findfact() and findpower, respectively, at line number 8 and 12. Note that neither power nor factorial would have a fractional component as both are accepting integer values. Assuming that it’s being declared double so as to force each term to fractional value, a better option would be to typecast one of the values returned by the function findpower() or findfact() to double or float, at line number 19. Also note that there is a discrepancy in the number of terms ( that is n). It is not correct as per the series given in the question. The last term is xn, since the numerator is starting from 2 and increasing in steps of 2, the number of terms are n/2.