Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- import java.io.*;
- import static java.lang.Math.*;
- public class BabylonianSqrt6 {
- public static void main(String[] args) {
- String S;
- S = JOptionPane.showInputDialog(null, "Enter a Whole Integer");
- int SigFigs = 0;
- for (int i = 0, len = S.length(); i < len; i++) {
- if (Character.isDigit(S.charAt(i))) {
- SigFigs++;
- }
- }
- if (SigFigs % 2 == 0)
- {
- double V = log(SigFigs)/log(2);
- double G = Math.round(V);
- double R = Integer.parseInt(S);
- double x0 = 6*(Math.pow(10,G));
- double x1 = (0.5)*((x0)+(R/x0));
- double x2 = (0.5)*((x1)+(R/x1));
- double x3 = (0.5)*((x2)+(R/x2));
- double x4 = (0.5)*((x3)+(R/x3));
- double x5 = (0.5)*((x4)+(R/x4));
- double x6 = (0.5)*((x5)+(R/x5));
- double x7 = (0.5)*((x6)+(R/x6));
- double x8 = (0.5)*((x7)+(R/x7));
- double x9 = (0.5)*((x8)+(R/x8));
- double x10 = (0.5)*((x9)+(R/x9));
- String str = Double.toString(x10);
- System.out.println("Square root of " + S + " by the Babylonian Guess/iteration method is approximately " +str);
- }
- else
- {
- double V = log(SigFigs)/log(2);
- double G = Math.round(V);
- double R = Integer.parseInt(S);
- double x0 = 2*(Math.pow(10,G));
- double x1 = (0.5)*((x0)+(R/x0));
- double x2 = (0.5)*((x1)+(R/x1));
- double x3 = (0.5)*((x2)+(R/x2));
- double x4 = (0.5)*((x3)+(R/x3));
- double x5 = (0.5)*((x4)+(R/x4));
- double x6 = (0.5)*((x5)+(R/x5));
- double x7 = (0.5)*((x6)+(R/x6));
- double x8 = (0.5)*((x7)+(R/x7));
- double x9 = (0.5)*((x8)+(R/x8));
- double x10 =(0.5)*((x9)+(R/x9));
- String str = Double.toString(x10);
- System.out.println("Square root of " + S + " is approximately " +str);
- }
- double M = Integer.parseInt(S);
- double B = sqrt(M);
- System.out.println("Compare this to the java Sqrt() function result: " + B);
- }
- }
Add Comment
Please, Sign In to add comment