# Thread: factorial sum in java

1. Member
Join Date
Mar 2011
Posts
27
Rep Power
0

## factorial sum in java

I am trying to make a program that finds the sum of:

∑ (2n+1)!

In this program the user inputs the upper limit of the program and the lower limit is always 0.

So if the user input the value 2 for the upper limit the sum would be:

[2(0)+1]! + [2(1)+1]! + [2(2)+1]!
= 1! + 3! + 5!
= 1 + 6 + 120
= 127

Since java does not have a factorial function I made the following program:

import java.io.*;
public class factorial
{
public static void main(String args[]) throws IOException
{

int n;

System.out.print("Input a value for the upper limit: ");

int factorial=1;
for (int i=2; i<=n; i++)
{
factorial=factorial*i;
}
System.out.println(factorial);

}

}

This program calculates factorials (ie if the user inputs 5 into the above program it will about the answer of 120)

I don't know how I can incorporate the factorial to find the sum of ∑ (2n+1)!
Last edited by java157; 03-17-2011 at 05:26 AM.

Don't have your code calculating the factorial in another class. Create a method that calculates the factorial in your existing class. Then in the loop that does the summing you call the factorial method passing the result of 2n + 1 as a parameter. Then add the returned value to your sum.
Java Code:
```loop
sum += call to the factorial method(....);
}```

3. Member
Join Date
Mar 2011
Posts
27
Rep Power
0
Originally Posted by Junky

Don't have your code calculating the factorial in another class. Create a method that calculates the factorial in your existing class. Then in the loop that does the summing you call the factorial method passing the result of 2n + 1 as a parameter. Then add the returned value to your sum.
Java Code:
```loop
sum += call to the factorial method(....);
}```
This is my attempt to your suggestion (but it failed):

import java.io.*;
public class factorial
{
public static void main(String args[]) throws IOException
{

int n;

System.out.print("Input value for upper limit: ");

int factorial=1;
for (int i=2; i<=2*n+1; i++)
{
factorial=factorial*i;

}

int sum=0;
for (int i=0; i<=n; i++)
{
sum=sum+factorial;
}
System.out.println(sum);

}

}

4. Create a separate static method which takes an int argument and performs the factorial and returns the answer, then you can simply loop through like in the last problem and find the factorial of it as well.

Btw, doing this step by step by hand may be extremely beneficial.
Last edited by sunde887; 03-17-2011 at 05:57 AM.

5. Member
Join Date
Mar 2011
Posts
27
Rep Power
0
Originally Posted by sunde887
Create a separate static method which takes an int argument and performs the factorial and returns the answer, then you can simply loop through like in the last problem and find the factorial of it as well.
I don't know how to do that. I'm somewhat new to java (2 months java experience, from an intro to java class).

6. Alright, main is a static method, a static method can be called without first creating an instance of the class. So outside of main create a class that is public, static, returns an int and takes an int as an argument. This method finds the factorial of the argument and returns the answer. Make the method and them call it on some number in main and print the result. Once you do that you are nearly done.

I am not going to give you this answer, I want you to really understand what you are doing. Read tutorials if you are stuck, or check your textbook.
Last edited by sunde887; 03-17-2011 at 06:10 AM.

7. Just create a method for factorial calculation and put your logic inside it and return the result( calculated factorial value) to the calling method( that is main method). Somewhat like this:

int fact(int i){

//Use factorial calculation logic here and then return factorial value using
return fact_value; //or the variable used.
}

now in the main method create a object of the class and call the method in the loop as necessary......

8. Member
Join Date
Apr 2009
Posts
49
Rep Power
0

## another approach

hey mate,

just having a look at the mathematics,

when summing factorials (say the first n) you have something of the form

Sum = 1! + 2! + 3! + ... + n!

which from what I've read is to be done using the following styled algorithm
int sum = 0;
for(int i = 1 ; i <=n ; i++) {
sum += Factorial(i);
} //end for

where you would write a separate function to compute the Factorial, that methodology is perfectly fine, however, to reduce the computations, you can have a play with the mathematics involved, i.e.

Sum = 1! + 2! + 3! + ... + n!
Sum = 1 + 2x1 + 3x2x1 + .....

and we observe that the terms in the sum are changing by a multiplication of a integer scalar (i.e. 2 , 3 , 4 , ... , n)

So when calculating the sum, you could have a separate dummy variable to avoid doing unneccesary multiplications, i.e.

int sum = 0, fact = 1;

for(int i = 1; i <=n; i++) {
fact *= i;
sum += fact;
} // end for

or as a separate static function in your class

public static int SumFactorials(int n) {

int sum = 0; fact = 1;

for(int i = 1; i <=n; i++) {
fact *= i;
sum += fact;
} // end for

return sum;

} //end SumFactorials

and then to call in main just use

public static void main(String[] args) {
int n = ... (whatever value you want to give it - can be user defined)
int sumOfNFactorials = SumFactorials(n);
//then print to screen or whatever
} //end main

As I said at the start, the responses given are spot on, just thought I'd through a different method in.

Hope this helps,

David

Edit - I should really read the actual question before I begin, I notice you are summing the first n odd factorials, i.e.

Sum = 1! + 3! + 5! + .... + (2*n + 1)!

The modification is rather simple, but I'll write out in case detail is required ( I know when I started with this stuff I wanted worked detail)

Sum = T(0) + T(1) + ... .T(n)

where T(j) = (2j + 1)! , i.e. the 3rd term T(2) = (2*2 + 1)! = 5! = 5 x 4 x 3! = (3 + 2) x ( 3 + 1) x T(1) ---> T(2) = (3 + 2)(3 + 1)T(1)
similarly, the 4th term T(3) = (2*3 + 1)! = 7! = 7 x 6 x 5! = ( 5 + 2) * ( 5 + 1) * T(2) ---> T(3) = (5 + 2)(5 + 1)T(2)
and for the 5th term if would be
T(4) = (7 + 2)(7 + 1)T(3)
So putting them all together we have,
T(1) = (1 + 2)(1 + 1)T(1)
T(2) = (3 + 2)(3 + 1)T(1)
T(3) = (5 + 2)(5 + 1)T(2)
T(4) = (7 + 2)(7 + 1)T(3)

which clearly follows a pattern of (and can easily be verified using mathematical induction):

T(n) = (r + 2)(r + 1)T(n-1) where r = 2*n + 1

So to modify the algorithm we are going to introduce another variable, which I will call r.

public static int SumOddFactorials(int n) {

int sum = 0; fact = 1, r;

for(int i = 0; i < n; i++) {
sum += fact;
r = 2*i + 1;
fact *= (r + 2)*(r + 1);
} // end for

return sum;

} //end SumOddFactorials

Now whilst this algorithm works perfectly, in the interest in reducing computations, take a look at how r changes

r = 1 , 3 , 5 , 7 , ...

I'm sure you'll observe the simple pattern - it differs by 2, so instead of calculating r using r = 2*i + 1, we can replace with r = r + 2 or r += 2

Thus we finally arrive at,

public static int SumOddFactorials(int n) {

int sum = 0; fact = 1, r=1;

for(int i = 0; i < n; i++) {
sum += fact;
fact *= (r + 2)*(r + 1);
r += 2;
} // end for

return sum;

} //end SumOddFactorials
Last edited by DavidG24; 03-17-2011 at 09:09 AM. Reason: realised I had made a mistake with not reading the original question correctly

9. Originally Posted by DavidG24
As I said at the start, the responses given are spot on, just thought I'd through a different method in.
You method is much more efficient: it uses n multiplications and n additions while the other methods (that recalculate the factorial of a number over and over again) use n*(n+1)/2 multiplications and n additions.

kind regards,

Jos

10. Member
Join Date
Mar 2011
Location
Bangalore, India
Posts
5
Rep Power
0
public class Simple {
public static void main(String[] e){
int n=2;
//(2n+1)!
int formula = 2*n + 1 ;
int sum=factorial(formula);
while( formula != 0 )
{
sum += factorial(--formula);
}
System.out.println(sum);
}

public static int factorial(int n)
{
if(n==0 || n==1) return 1;
else return n*factorial(n-1);
}
}

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•