Results 1 to 10 of 10
  1. #1
    java157 is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default 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; 03-17-2011 at 04:26 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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(....);
    }

  3. #3
    java157 is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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(....);
    }
    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);

    }

    }

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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 04:57 AM.

  5. #5
    java157 is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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 05:10 AM.

  7. #7
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default

    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. #8
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

    Default 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 08:09 AM. Reason: realised I had made a mistake with not reading the original question correctly

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by DavidG24 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    santoshpai1 is offline Member
    Join Date
    Mar 2011
    Location
    Bangalore, India
    Posts
    5
    Rep Power
    0

    Default

    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);
    }
    }

Similar Threads

  1. JSP factorial of number and NumberFormatException
    By sandraW in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 10-12-2010, 07:46 AM
  2. Factorial console program
    By er1c550n20 in forum New To Java
    Replies: 6
    Last Post: 04-13-2010, 02:07 PM
  3. Using method to call Factorial
    By hydride in forum New To Java
    Replies: 2
    Last Post: 03-02-2010, 02:16 AM
  4. Factorial
    By Anindo in forum New To Java
    Replies: 4
    Last Post: 07-28-2009, 09:46 PM
  5. [SOLVED] overflow when calculate factorial
    By ravinda in forum New To Java
    Replies: 6
    Last Post: 05-05-2009, 05:07 PM

Posting Permissions

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