aspose file tools*
The moose likes Beginning Java and the fly likes Need help in square root finding program. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help in square root finding program." Watch "Need help in square root finding program." New topic
Author

Need help in square root finding program.

omar salem
Ranch Hand

Joined: Aug 22, 2006
Posts: 34
Hi Ranchers

I am trying to make a java program that calculates the square root of a given number. I used a mathematical formula to generate the square root called Bakhshali approximation. It states that if (N square) is the nearest perfect square to the number S. S is the number we are finding the square root for then the square root of S is calculated as follows:
1- D = S-(N square)
2- P = D/2N
3- A = N + P
4- then square root of S is approximately given by A - (p square)/2A

now in my program i have written the above calculations but the problem i am facing now is how to make my program selects the nearest perfect square.

i have tried to take a while loop to find the squares of the numbers less than or equal to the value of S. then comparing these squares one by one with S until i get the nearest square.



i know it my code has a lot of errors but would any one suggest something.
I would appreciate your help.

Omar Salem
[edit]Add code tags. CR[/edit]
[ December 04, 2008: Message edited by: Campbell Ritchie ]

Have a sip of Java !!!
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
Not trying to be smart but there is a method Math.sqrt() to do this for you....I assume you dont want to use that though


<a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">Use Code Tags!!</a>
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Not familiar with the method, but d/2*N should surely read d/2/N.
You have declared N inside the block; it will go out of scope and vanish when you try to use it later.
Sorry I can't help any more.
omar salem
Ranch Hand

Joined: Aug 22, 2006
Posts: 34
Thanks for you both......I already know that there is a Math.sqrt() method but that would calculate the square root for me. what i am trying to do is to make my program calculate the square root without using any built in methods.

Thanks again
Omar Salem
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
if you are using any of the IDE's it should'nt be hard to get inside the Math.sqrt() function, Just right click and "go to declaration"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Originally posted by Sri Anand:
if you are using any of the IDE's it should'nt be hard to get inside the Math.sqrt() function, Just right click and "go to declaration"


Math.sqrt is (indirectly) a native function. It will be very difficult to find its code. If you look in the StrictMath class you find this link where you can get details of the algorithms, however.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
It's not as hard as I thought to find the code; you can download it from the link I quoted a few minutes ago.
omar salem
Ranch Hand

Joined: Aug 22, 2006
Posts: 34
Thank you all. That really helped....I really appreciate it.
Omar Salem
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Only too pleased to be able to help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help in square root finding program.