Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding small program  RSS feed

 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello every one i need to help me in understanding this small program
...you can approximate by using the following series
∏=4(1-1/3+1/5-1/7+1/9-1/11+1/13-.....-1/(2i-1)+1/(2i+1))
write a program that displays the value for i=10000,20000,....,and 100000
I've developed this code to solve the problem


but i don't know how can i compute the value when i is equal to 10000,20000,.....,and 100000
note that the output of my program is
The value of p is 3.1415926517266946
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean that double doesn't have enough precision to handle the larger values of i? In that case, you would have to use java.math.BigDecimal
 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean that double doesn't have enough precision to handle the larger values of i

no this isn't my question
is this code I've written solve the problem?
and what about "compute the value when i is equal to 10000,20000,.....,and 100000"
this is supposed to be implemented in my code
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, got it. The second part of your for statement should be the limit. Comparing i to a value computed from i isn't a good idea. For any non-negative value of i, i < 2 * i + 1 will always be true. You want something more like:



That's not exactly right though, because your i isn't being used exactly the same way it's used in the formula. I'd advise changing the code so it matches the formula more closely and see if you can dispense with the count variable entirely. Try a low limit of i and put in some print statements at first to help you get the code right.
 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd advise changing the code so it matches the formula more closely

I take your advice and I've improved my code to the following

here I've used nested loops..........the outer to control the value of i
and the inner to control the value of x(denominator) and gave me the result i really want.
what is your opinion???
thanks.
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I was trying to nudge you towards a simpler program, and you made it more complex. Keep in mind that when i = 1, then 2*i - 1 is 1 and 2*i + 1 is 3. When i = 2, then 2*i - 1 is 3, and 2*i+1 is 5. you should be able to generate your whole series just by changing the value of i. The only variables you should need then are i and a sum to keep a running total.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!