# Need help in square root finding program.

omar salem

Ranch Hand

Posts: 34

posted 7 years ago

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 ]

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

Posts: 142

Campbell Ritchie

Sheriff

Posts: 48652

56

omar salem

Ranch Hand

Posts: 34

Sri Anand

Ranch Hand

Posts: 392

Campbell Ritchie

Sheriff

Posts: 48652

56

posted 7 years ago

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.

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

Posts: 48652

56

omar salem

Ranch Hand

Posts: 34