Results 1 to 10 of 10
Thread: factorial sum in java
 03172011, 05:23 AM #1Member
 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
{
BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
int n;
System.out.print("Input a value for the upper limit: ");
n = Integer.parseInt(keybd.readLine());
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; 03172011 at 05:26 AM.
 03172011, 05:27 AM #2
This is a follow on of your other thread (for those who didn't read it).
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(....); }
 03172011, 05:50 AM #3Member
 Join Date
 Mar 2011
 Posts
 27
 Rep Power
 0
This is my attempt to your suggestion (but it failed):
import java.io.*;
public class factorial
{
public static void main(String args[]) throws IOException
{
BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
int n;
System.out.print("Input value for upper limit: ");
n = Integer.parseInt(keybd.readLine());
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);
}
}
 03172011, 05:52 AM #4
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
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; 03172011 at 05:57 AM.
 03172011, 05:57 AM #5Member
 Join Date
 Mar 2011
 Posts
 27
 Rep Power
 0
 03172011, 06:07 AM #6
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
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; 03172011 at 06:10 AM.
 03172011, 06:25 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......
 03172011, 07:26 AM #8Member
 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(n1) 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 SumOddFactorialsLast edited by DavidG24; 03172011 at 09:09 AM. Reason: realised I had made a mistake with not reading the original question correctly
 03172011, 08:11 AM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
Build a wall around Donald Trump; I'll pay for it.
 03172011, 11:07 AM #10Member
 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(n1);
}
}
Similar Threads

JSP factorial of number and NumberFormatException
By sandraW in forum JavaServer Pages (JSP) and JSTLReplies: 5Last Post: 10122010, 07:46 AM 
Factorial console program
By er1c550n20 in forum New To JavaReplies: 6Last Post: 04132010, 02:07 PM 
Using method to call Factorial
By hydride in forum New To JavaReplies: 2Last Post: 03022010, 03:16 AM 
Factorial
By Anindo in forum New To JavaReplies: 4Last Post: 07282009, 09:46 PM 
[SOLVED] overflow when calculate factorial
By ravinda in forum New To JavaReplies: 6Last Post: 05052009, 05:07 PM
Bookmarks