java - Finding square roots using long division method -
so class assignment. can't seem grasp complexity of long division. quite frankly, i'm not sure how begin solve algorithm make need.
for second part, must take in number user inputs , find square root specific number of decimal places user inputs. algorithm need replicate find square root can found here.
the following have far (clearly not progress made far):
import java.util.*; import java.math.*; import terminalio.*; public class squarerootprogram{ public static float squarerootdiv(string number, int decimals){ string[] test = new string[2]; float answer = 0; string groups = ""; test = number.split("\\."); test = number.split("\\."); system.out.println(test[0]); system.out.println(test[1]); for(int = 0; groups != test[i].tostring(); i++){ test[i].length(); } return answer; } public static void main(string[]args){ keyboardreader reader = new keyboardreader(); string response = " "; string initnumber = " "; float number = 0; int decimals = 0; system.out.println("welcome square root program."); do{ addline(); system.out.print("please enter number of want find square root: "); initnumber = reader.readline(); number = float.valueof(initnumber); system.out.println(number); system.out.print("please enter number of decimal places in want answer: "); decimals = reader.readint(); system.out.println("the answer provided division algorithm " + squarerootdiv(initnumber,decimals)); addline(); system.out.print("would try number?: "); response = reader.readline(); }while(response.compareto("no")!=0); } } an example of required output is:
welcome square root program. please enter number of want find square root: 3.14159 please enter number of decimal places in want answer: 3 answer provided division algorithm 1.772 try number?: no thank using program. obviously needs work number input , amount of decimal places user have square root returned to.
so if can me either explaining me how long division in java, or leaving code or without comments, appreciated!
i have created working version of program. instructions provided give bunch of steps used in code.
as procedure indicates, first locate integer part , try find "closest" square. in given example, "closest" square 40 36. let's go through in steps.
public static double squarerootdiv (string number, int decimals) { if (decimals <= 0) return -1; //sanity check int ipart = integer.parseint(number.split("\\.")[0]); that code give integer part can locate closest square, running loop (you improve binary search) so:
int closestsquare = 0; (closestsquare = 0; closestsquare <= ipart; closestsquare++) { if (closestsquare * closestsquare > ipart) { closestsquare -= 1; break; } } what check every single number integer part found. first number greater integer part indicates have overstepped. step backwards once , closest square. make sure make variable remembers decimal place should go:
int decimalix = ans.length(); step 2: find decimals.
the procedure asks keep track of numbers have found far, remainder of closest square subtracted integer part. in case of finding 40, remainder 40 - 36 = 4. our current answer far 6.
the procedure asks double current answer , multiply 10, multiplying current answer 20. if that, 120. let's call simplicity's sake. then, must find digit add such (a + digit) * digit < remainder * 100. in concrete terms, have find digit 1 through 9 such (120 + (some digit)) * digit < 4*100.
we can use decimals parameter bound our loop, so:
for (int = 0; < decimals; i++) { remainder *= 100; int base = integer.parseint(ans) * 20; //now check digits (int j = 9; j >= 0; j--) { int trial = (base + j) * j; //use digit if (trial < remainder) { //we have found first digit less remainder! remainder -= trial; ans += j; break; } } } now remains send answer:
return double.parsedouble(ans.substring(0, decimalix) + "." + ans.substring(decimalix)); here full code.
public static double squarerootdiv(string number, int decimals){ if (decimals <= 0) return -1; string ans = ""; int ipart = integer.parseint(number.split("\\.")[0]); int first = getnext(ipart); int remainder = ipart - first*first; ans += first; int decimalix = ans.length(); int numdecimalsneeded = decimals - ans.length(); (int = 0; <= numdecimalsneeded; i++) { remainder *= 100; int base = integer.parseint(ans) * 20; (int j = 9; j >= 0; j--) { int trial = (base + j) * j; if (trial < remainder) { remainder -= trial; ans += j; break; } } } return double.parsedouble(ans.substring(0, decimalix) + "." + ans.substring(decimalix)); } public static int getnext (int ipart) { (int = 0; <= ipart; i++) { if (i*i > ipart) { return - 1; } } return -1; }
Comments
Post a Comment