Results 1 to 4 of 4
  1. #1
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default Help please, stuck on for statement problem.

    Hey everyone. i have been trying to figure this out for quite some time now and i am just stuck on using the for loop. i need to write an application that determines the value for mathematical constant e by using the following formula . allow the user to enter the number of terms to calculate..

    e=1+1/1!+1/2!+1/3!...

    i am just stuck on this, i know i need to first make a method that calculates n! which i am able to do, this code works..

    Java Code:
    import java.util.Scanner;
    public class Factorials {
    
    	public static void main(String[] args) {
    		
    		Scanner input = new Scanner(System.in);
    		
    		int num;
    		int nFact;
    		
    		System.out.println("Please enter a number: ");
    		num=input.nextInt();
    		
    		nFact = fact(num);		
    	
    		System.out.println(num + "! = " + nFact + ".");	
    	}
    	 static int fact(int num){
    		if(num<=1)
    			return 1;
    		
    		else{
    		 return num*fact(num-1);
    	 }
    	 
    	 
    	 }
    }
    i dont know how or where to put the for loop. anybody who cares to clear this up for me is appreciated. thanks!

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    This sample now asks you for the value of n of which to iterate up to. The compute e function does the for loop of invoking 1/n! and summing it with the previous terms.

    Likely didn't need to make the factorial function into a loop to be non-recursive as well, but this way should use less heapspace for larger N.

    Java Code:
    import java.util.Scanner;
    
    public class Factorial {
    
      public static void main(String[] args) {
    
        Scanner input = new Scanner(System.in);
    
    
    // that original just compute n! 
    //    System.out.println("Please enter a number: ");
    //    in num = input.nextInt();
    //    int nFact = fact(num);
    //    System.out.println(num + "! = " + nFact + ".");
       
        
        // now compute e= 1/1! + 1/2! + ...
        
        System.out.prints("Enter n for e=1/1! + ... + 1/n! :");
        int n = input.nextInt();
        
        double e = computeE(n);
    		
    		System.out.println("e=" + e);
      }
    
    	/**
    	* computes e = 1/1! + ... + 1/n!
    	*/
      public static double computeE(int n) {
    		double result = 0;
    		for (int i = 1; i <=n; i++) {
    			double term = (double)1 / fact(i);
    			result += term;
    		}
    		return result;
    	}
    	
    	
      /**
       * computes n factorial using a for loop (not recursive method call)
       * @param num
       * @return
       */
      static int fact(int num) {
        int result = num;
        for (int i = num-1; i > 0; i --) {
          int newResult = result * i;  
          // could do result *= i, but do this additional variable for debug output clarity. 
          //System.out.println("doing " + result + " * " + i + " = " + newResult);
          result = newResult;
        }
        return result;
      }
    
    }

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

    Default

    There is no need to compute those factorials over and over again. Suppose you have the value for the n-th term == 1/n!. If you want to know the n+1st term (which is 1/(n+1)!) simply do this: (value of n-th term)/(n+1) == 1/(n!*(n+1)) == 1/(n+1)!. In code:

    Java Code:
    double e= 1;
    double term= 1;
    for (int n= 1; n <= N; n++) {
       term/= n;
       e+= term;
    }
    System.out.println("e: "+e);
    kind regards,

    Jos

  4. #4
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

Similar Threads

  1. [SOLVED] Problem with an If Statement.
    By ChrisMayhew in forum New To Java
    Replies: 8
    Last Post: 01-08-2009, 04:58 AM
  2. problem while using return statement
    By shaluchandran in forum New To Java
    Replies: 10
    Last Post: 12-12-2008, 07:29 PM
  3. If-Else statement problem
    By MomenT in forum New To Java
    Replies: 9
    Last Post: 10-23-2008, 09:06 AM
  4. ForLoop statement problem
    By MomenT in forum New To Java
    Replies: 4
    Last Post: 10-01-2008, 02:01 PM
  5. Please help me with this Problem Statement
    By KMS in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 09:44 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
  •