programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

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

Ranch Hand
Posts: 106
2
• Number of slices to send:
Optional 'thank-you' note:

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!

Bartender
Posts: 10780
71
• 1
• Number of slices to send:
Optional 'thank-you' note:

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: 106
2
• Number of slices to send:
Optional 'thank-you' note:
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!

lowercase baba
Posts: 13089
67
• Number of slices to send:
Optional 'thank-you' note:
1 is an int. factorial is an int.

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

Winston Gutkowski
Bartender
Posts: 10780
71
• Number of slices to send:
Optional 'thank-you' note:

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: 106
2
• Number of slices to send:
Optional 'thank-you' note:
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!

Rancher
Posts: 1044
6
• Number of slices to send:
Optional 'thank-you' note:
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

Rancher
Posts: 43081
77
• Number of slices to send:
Optional 'thank-you' note:
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.

Marshal
Posts: 79277
377
• Number of slices to send:
Optional 'thank-you' note:
What about ?

 Don't get me started about those stupid light bulbs.
reply
Similar Threads