Guest User

Untitled

a guest
Feb 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. /* 992 OOPL Lab#1 Due: March 13, 2011 */
  2. /*===========================================================================================*/
  3. /* The Babylonian algorithm to compute the square root of a number n is as follows:
  4. /* 1. Make a guess at the answer (you can pick n/2 as your initial guess).
  5. /* 2. Compute r = n /guess
  6. /* 3. Set guess = (guess + r) / 2
  7. /* 4. Go back to step 2 for as many iterations as necessary. The more you repeat
  8. /* steps 2 and 3, the closer guess will become to the square root of n.
  9. /*
  10. /* Write a program that inputs an integer for n, iterates through the Babylonian
  11. /* algorithm until the value of guess is the same as the value of Math.sqrt(n) to the
  12. /* fifth decimal place. For each iteration, print out the value of guess and the difference
  13. /* between guess and Math.sqrt(n) as double to six decimal places. At the end of your
  14. /* program, print out the number of guesses as an int and the value of guess as a
  15. /* double to five decimal places. */
  16. /*===========================================================================================*/
  17.  
  18.  
  19. import java.util.Scanner; // for claas Scanner
  20.  
  21.  
  22. public class Lab1_4099013054 { // The first line and Name of File/class.
  23.  
  24. public static void main(String[] args){ // Entry point
  25. Scanner cin = new Scanner(System.in); // make new object of Scanner
  26. System.out.println(""); //Line 26 - 28: Just some file and help.
  27. System.out.println("學號:4099013054 系級:歷史一 姓名:林書緯 作業編號:Lab#1 ");
  28. System.out.println("本程式可重複輸入,若要離開請輸入Ctrl+z (!EOF) ");
  29. System.out.println("______________________________________________________________");
  30. System.out.println("This is a program that use The Babylonian algorithm to compute the square root.");
  31. System.out.print("Please input the number : "); //For asking user to enter the number
  32.  
  33. while(cin.hasNext()){ // The loop for entering different number. And key in.
  34.  
  35. int n = cin.nextInt(); // Set "Key in" to the Variable 'n'
  36. int i = 0; // declare Variable counter 'i' of guesses
  37. double guess = n*0.5; // the first guess number // Babylonian algorithm: 3. Set guess = (guess + r) / 2
  38. double r; // declare double Variable 'r' for computing
  39. double diff=0.0; // declare double Variable 'diff' for saving difference between guess and Math.sqrt(n)
  40. System.out.printf("The answer from Math.sqrt(n): %10.6f\n\n", Math.sqrt(n)); // Print the answer from Math.sqrt
  41.  
  42. while(Math.floor( guess*1E+5) *1E-5 != Math.floor( Math.sqrt(n)*1E+5) *1E-5) {
  43. // The loop for checking if the two are the same. If not ,keep guessing.
  44. diff = Math.floor( guess*1E+6) *1E-6 - Math.floor( Math.sqrt(n)*1E+6) *1E-6; // Compute Difference
  45. System.out.printf("%d. The guess is %10.6f. ", i, guess); // Print the number of guess
  46. System.out.print("The Difference is "); // Line45-46 Print the number of Difference
  47. System.out.printf("%10.6f\n", diff);
  48. r=n/guess; // Babylonian algorithm: 2. Compute r = n /guess
  49. guess=(guess + r)/2; // Babylonian algorithm: 3. Set guess = (guess + r) / 2
  50. i = i+1; // counter 'i' of guesses plus 1
  51. }
  52. diff = Math.floor( guess*1E+6) *1E-6 - Math.floor( Math.sqrt(n)*1E+6) *1E-6; // Compute Difference
  53. System.out.printf("%d. The guess is %10.6f. ", i, guess); // Print the number of guess. Last time.
  54. System.out.print("The Difference is "); // Line53-54 Print the number of Difference. Last time.
  55. System.out.printf("%10.6f\n", diff );
  56. System.out.println(" ");
  57. System.out.printf("The number of guesses is %3d. The guess is %10.5f\n\n\n",i, Math.floor( guess*1E+5) *1E-5); // End
  58. System.out.print("Please input the number : "); // Next Enter Line.
  59. }
  60.  
  61. }
  62.  
  63. }
Add Comment
Please, Sign In to add comment