• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ranajoy Saha
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 10257
59
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranajoy Saha
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 is an int. factorial is an int.

When you divide an int by an int, you get an int.
 
Winston Gutkowski
Bartender
Pie
Posts: 10257
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic