• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Need to find the iteration for this java calculation

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76406
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 1049
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Saloon Keeper
Posts: 9556
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you multiplying by one?
 
Campbell Ritchie
Marshal
Posts: 76406
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1049
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76406
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Beauty is in the eye of the tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic