File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes WAP to find out the series 1 + 1/2! + 1/3! + ......... + 1/n! (n! = Factorial) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "WAP to find out the series 1 + 1/2! + 1/3! + ......... + 1/n! (n! = Factorial)" Watch "WAP to find out the series 1 + 1/2! + 1/3! + ......... + 1/n! (n! = Factorial)" New topic
Author

WAP to find out the series 1 + 1/2! + 1/3! + ......... + 1/n! (n! = Factorial)

Ranajoy Saha
Greenhorn

Joined: Jan 17, 2013
Posts: 18


I am trying to find out the solution for the series 1 + 1/2! + 1/3! + ......... + 1/n! (n! = Factorial)! But when I store 1/factorial of 5 or suppose 6 in a variable the value of the variable is 0.0 but I should be 0.16666666666. So there lie the problem. I dry ran
my program and could not find any problem in my logic but in line 31 the problem is rising! The data type of the variable is double and it should store decimal values but! Please help!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

Ranajoy Saha wrote:I dry ran my program and could not find any problem in my logic but in line 31 the problem is rising! The data type of the variable is double and it should store decimal values but! Please help!

Yes, but the problem is actually occurring at line 30.

Q: What is the result of 1/factorial? (Tip: look at the types carefully).

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ranajoy Saha
Greenhorn

Joined: Jan 17, 2013
Posts: 18
Thank you Winston! Thank you very much! I just changed the data type of factorial variable and the work was done! But I need an explanation. when the data type of factorial variable is int and when I change line 30 to series_calc =(double) (1/factorial); then why isn't the same answer coming as previous one. (The answer is 1.0) I am confused on this part! Dont be angry on me if my question seems silly 'cause I am in school and I have just stated using Java language!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

1 is an int. factorial is an int.

When you divide an int by an int, you get an int.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

Ranajoy Saha wrote:I just changed the data type of factorial variable and the work was done! But I need an explanation. when the data type of factorial variable is int and when I change line 30 to series_calc =(double) (1/factorial); then why isn't the same answer coming as previous one. (The answer is 1.0) I am confused on this part! Dont be angry on me if my question seems silly 'cause I am in school and I have just stated using Java language!

No probs. The answer is basically as fred said, so the result of 1/factorial is an int. Adding the cast as you have done simply the changes the type of that result; it doesn't change its value.

BTW, you didn't need to change the type of factorial. What do you think the result of 1.0/factorial is?

Winston
Ranajoy Saha
Greenhorn

Joined: Jan 17, 2013
Posts: 18
I did change 1 to 1.0 while I was trying to find a answer to the query. The answer was correct! Thanks all for solving my problem. A big thanks to all!
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
It is problematic too as factorial is of type int, and the factorial function grows pretty fast, so it can overgrow the integer range quickly.





2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800
11 39916800
12 479001600
13 1932053504
14 1278945280
15 2004310016
16 2004189184
17 -288522240
18 -898433024
19 109641728
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41123
    
  45
You can try to improve matters by using long instead of int, but that doesn't go much further. You may want to resort to using the BigInteger class.


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38020
    
  22
What about ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: WAP to find out the series 1 + 1/2! + 1/3! + ......... + 1/n! (n! = Factorial)
 
Similar Threads
If else condition problem
Binary to Decimal conversion
Help with Value Returning Methods
Perfect Number Generation
Problem with arrays