Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need to find the iteration for this java calculation  RSS feed

 
jin Otanashi
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder how can i find the number of iterations it takes to converge to the square root?
here i got so far:


I was thinking of using the For loop to find the iteration but somehow that doesn't work when i try it to use in the approx
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can put some print statement and count them. Or use some static counter and increment it in the function. So, any time method is called
one value is incremented and then you can print the value as a last statement in main.
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have written that loop wrongly; you can remove about ⅝ of it.Why are you asking for doubles in your square root method and taking ints from the keyboard? I would suggest you change the loop to do…while (num >= 0);
Don't start method names with CapitalLetters.
 
jin Otanashi
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You have written that loop wrongly; you can remove about ⅝ of it.Why are you asking for doubles in your square root method and taking ints from the keyboard? I would suggest you change the loop to do…while (num >= 0);
Don't start method names with CapitalLetters.

I know i can use Int but the problem is i'm using approximation of the square root that why im using double. by they way if i were to use Int then i won't need this calculation

what i trying to do is like a loop of calculation that output each calculation that the program had done instead of just give me the result or answer without showing the calculation. I think the calculation would look like a right triangle
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're approximation algorithm is flawed:

(N*N)/(2N) -> infinity. Your method is divergent.
 
jin Otanashi
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:You're approximation algorithm is flawed:

(N*N)/(2N) -> infinity. Your method is divergent.

you mean i need to do like (high * low )/2 ?
 
Carey Brown
Bartender
Posts: 2980
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you multiplying by one?
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know. It is such a long time since I saw that algorithm that I have forgotten how to use it. Is that Newton's Approximation? It is at least 45 years since I last saw it. Read about it on Wikipedia or here or here or here or lots of other links

I meant, if your square root method takes a double, why are you getting an int from the keyboard? Why not use nextDouble?
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jin Otanashi wrote:
Les Morgan wrote:You're approximation algorithm is flawed:

(N*N)/(2N) -> infinity. Your method is divergent.

you mean i need to do like (high * low )/2 ?


You need to have an algo that converges to your approximation, the formula you have does not.

I did a quick and dirty little algo and this worked for me if I don't be too crazy with it--just put the target number in for the guess and it brings thing around to a good approximation:



dTarget is the number you want the squareroot
dTrial is the guess (put dTarget in here for a start)
dTolerance is the error you are willing to accept (0.0001 seems to work well)
 
jin Otanashi
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:
jin Otanashi wrote:
Les Morgan wrote:You're approximation algorithm is flawed:

(N*N)/(2N) -> infinity. Your method is divergent.

you mean i need to do like (high * low )/2 ?


You need to have an algo that converges to your approximation, the formula you have does not.

I did a quick and dirty little algo and this worked for me if I don't be too crazy with it--just put the target number in for the guess and it brings thing around to a good approximation:



dTarget is the number you want the squareroot
dTrial is the guess (put dTarget in here for a start)
dTolerance is the error you are willing to accept (0.0001 seems to work well)

i see thank you for the correction, but somehow the calculation isnt right and return isnt giving my the result if i want sqrt of 25 just give me 1.0 same if i wants to use a number that isnt in the square root like 12 still giving the result of 1.0, i guess this code is wrong, i guess i will go back to the old code
 
jin Otanashi
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Why are you multiplying by one?

bad habit
 
jin Otanashi
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
silly me, already found how to do the loop thank everyone for helping and guide me
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us what you achieved in the end.
 
jin Otanashi
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Please show us what you achieved in the end.

startapprox is where the calculation and loop does the job
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!