Java Programming Help - Overclock.net - An Overclocking Community
Forum Jump: 

Java Programming Help

 
Thread Tools
post #1 of 4 (permalink) Old 11-04-2010, 03:46 PM - Thread Starter
BGP is my favorite
 
-Inferno's Avatar
 
Join Date: Aug 2005
Location: Portland, OR
Posts: 1,068
Rep: 83 (Unique: 78)
I have a problem figuring out this question

Write an application that estimated the value of the 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 figured out how to get just the factorial part, but I cannot figure out how to do this part.

This is the code I have for just the factorial part

Quote:
import java.util.Scanner;
public class factorial {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n;
int result = 1;

System.out.print("Enter Number: ");
n = input.nextInt();

while ( n >= 1){
result = result * n;
n--;
}
System.out.printf("Answer is: %d", result );
}
}
I believe I have to use a while loop, so if anyone can help me I would greatly appreciate it.
-Inferno is offline  
Sponsored Links
Advertisement
 
post #2 of 4 (permalink) Old 11-04-2010, 04:38 PM
New to Overclock.net
 
nevets1219's Avatar
 
Join Date: Aug 2010
Posts: 116
Rep: 13 (Unique: 12)
I would keep a variable that keeps track of the factorial instead of re-calculating it each time around.

Without just giving you the answer the process would look something like:
Code:
  // keep track of the current factorial
  // keep track of the current sum
  // let n be the user input

  // for n times do:
  //   0 is a special cases - handle it
  //   if >= 1 then do
  //     add 1/factorial to the sum AND
  //     find new factorial
A couple of things to be aware of:
- numeric type
- 0 is a special case because of 0! = 1

nevets1219 is offline  
post #3 of 4 (permalink) Old 11-04-2010, 06:46 PM - Thread Starter
BGP is my favorite
 
-Inferno's Avatar
 
Join Date: Aug 2005
Location: Portland, OR
Posts: 1,068
Rep: 83 (Unique: 78)
Code:
import java.util.Scanner;
public class proj3 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double fact = 1;
double sum = 0;
int n;

System.out.print("Enter # of Terms: ");
n = input.nextInt();

while ( n >= 1){
fact = fact * n;
sum = (1/fact) + sum;
n--;
}
System.out.printf("Sum: %f", sum);
}
}
I got that and my output was:
Enter # of Terms: 5
Sum: 0.283333

Not sure if I did it right..
-Inferno is offline  
Sponsored Links
Advertisement
 
post #4 of 4 (permalink) Old 11-05-2010, 10:52 AM
New to Overclock.net
 
nevets1219's Avatar
 
Join Date: Aug 2010
Posts: 116
Rep: 13 (Unique: 12)
You should be able to add those numbers up and determine if that's right or not. You can tell that your answer is not right from the equation.

Something you may want to verify is that valid input should be greater than 0. An input of zero should return 1.

As your inputs gets larger you should be approaching "2.71828". Look at the wiki for more information about "e".

This is what I got, hopefully it's correct:
Code:
0 => 1.0
1 => 2.0
2 => 2.5
3 => 2.6666666666666665
4 => 2.6904761904761902
5 => 2.6910299003322256

nevets1219 is offline  
Reply

Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the Overclock.net - An Overclocking Community forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
User Name:
If you do not want to register, fill this field only and the name will be used as user name for your post.
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off