Guest User

Untitled

a guest
Oct 16th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2. import java.io.*;
  3. import static java.lang.Math.*;
  4. public class BabylonianSqrt6 {
  5. public static void main(String[] args) {
  6.  
  7. String S;
  8. S = JOptionPane.showInputDialog(null, "Enter a Whole Integer");
  9.  
  10. int SigFigs = 0;
  11. for (int i = 0, len = S.length(); i < len; i++) {
  12. if (Character.isDigit(S.charAt(i))) {
  13. SigFigs++;
  14. }
  15. }
  16.  
  17.  
  18. if (SigFigs % 2 == 0)
  19. {
  20.  
  21. double V = log(SigFigs)/log(2);
  22. double G = Math.round(V);
  23.  
  24. double R = Integer.parseInt(S);
  25.  
  26. double x0 = 6*(Math.pow(10,G));
  27. double x1 = (0.5)*((x0)+(R/x0));
  28. double x2 = (0.5)*((x1)+(R/x1));
  29. double x3 = (0.5)*((x2)+(R/x2));
  30. double x4 = (0.5)*((x3)+(R/x3));
  31. double x5 = (0.5)*((x4)+(R/x4));
  32. double x6 = (0.5)*((x5)+(R/x5));
  33. double x7 = (0.5)*((x6)+(R/x6));
  34. double x8 = (0.5)*((x7)+(R/x7));
  35. double x9 = (0.5)*((x8)+(R/x8));
  36. double x10 = (0.5)*((x9)+(R/x9));
  37. String str = Double.toString(x10);
  38. System.out.println("Square root of " + S + " by the Babylonian Guess/iteration method is approximately " +str);
  39. }
  40. else
  41. {
  42.  
  43. double V = log(SigFigs)/log(2);
  44. double G = Math.round(V);
  45.  
  46. double R = Integer.parseInt(S);
  47.  
  48. double x0 = 2*(Math.pow(10,G));
  49. double x1 = (0.5)*((x0)+(R/x0));
  50. double x2 = (0.5)*((x1)+(R/x1));
  51. double x3 = (0.5)*((x2)+(R/x2));
  52. double x4 = (0.5)*((x3)+(R/x3));
  53. double x5 = (0.5)*((x4)+(R/x4));
  54. double x6 = (0.5)*((x5)+(R/x5));
  55. double x7 = (0.5)*((x6)+(R/x6));
  56. double x8 = (0.5)*((x7)+(R/x7));
  57. double x9 = (0.5)*((x8)+(R/x8));
  58. double x10 =(0.5)*((x9)+(R/x9));
  59. String str = Double.toString(x10);
  60. System.out.println("Square root of " + S + " is approximately " +str);
  61. }
  62.  
  63. double M = Integer.parseInt(S);
  64. double B = sqrt(M);
  65.  
  66. System.out.println("Compare this to the java Sqrt() function result: " + B);
  67.  
  68. }
  69. }
Add Comment
Please, Sign In to add comment